question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

minPresenceAhead - calculating the meaning of 'presence' on next sibling element

See original GitHub issue

Hello,

Great library! Thanks for maintaining it. Using

@react-pdf/renderer": "2.0.0-beta.5

I have a problem with minPresenceAhead, which is advertised as a way of ensuring that headings do not get rendered at the bottom of a page

Here’s a screenshot.

image

The header text ‘1.6 Effect of Covid-19 on the fetus’ is produced with the following component

const createH2ViewElement = (node, bodyTextColorString) => {
    
    return createElement(
        View,
        {
            //stops headers appearing at the bottom of the page
            minPresenceAhead: 1,
            key: uuidv4(),
            debug:true
        },
        createElement(
            Text,
            //add the special style props to the element
            {
                //add the custom styles
                style: {...dynamicStyles.styledHeader2, color: bodyTextColorString},
                //add the unique key
                key: uuidv4()
            },
            //then we pass in the children of the json object
            node.childNodes.map(child => JsonToPdfComponent(child, bodyTextColorString))
        )
    );
    
};

and then the following text starting ‘There are currently no data…’ is produced with the following component, which has no borders, padding or margins at the top.

const createParagraphTextElement = (node, bodyTextColorString) => {
    
    //first we define the function that will return the standard paragraph element when called
    const paragraphElement = (input) => {
        return createElement(
            Text,
            //add the special style props to the element
            {
                //add the custom styles
                style: {...dynamicStyles.styledParagraph, color: bodyTextColorString},
                //add the unique key
                key: uuidv4(),
                debug:true
            },
            //then we pass in the children of the json object
            input.childNodes.map(child => JsonToPdfComponent(child, bodyTextColorString))
        );
    };
    //then we have to check for nested images
    const nestedImage = findNestedImage(node);
    //if there's no nested image then we return the standard paragraph element
    if (!nestedImage) { return paragraphElement(node); }
    //otherwise we need to know if we're looking at an image intended to be inlined in the paragraph or if it just has a P / SPAN wrapper but should stand alone
    //to do this, we need to check the attributes of the image - the text editor adds these properties directly to the image rather than via style
    const attrObject = createAttributesObject(nestedImage);
    //then we create the standalone marker by seeing if the width property has been set and the width property has the ending 'vw' or '%'
    const standalone = attrObject.hasOwnProperty('width') && /[[vw]|[%]$/.test(attrObject.width);
    //if we have a standalone image then we return the image element with the standalone property set to true
    if (standalone) { 
        return createImageElement(nestedImage, true);
    //if the nested image looks inline then we return the standard paragraph element and let the image element do the work
    } else { return paragraphElement(node); }
    
};

My understanding is that minPresenceAhead should ensure that no page wrapping should occur between all sibling elements following the element within n points.

I have set minPresenceAhead to 1, so I would expect the title to be moved to the next page.

In fact, you can see from the debug highlighting that the renderer is correct in a sense, in that it can see the top of the paragraph element at the bottom of the page.

But surely this is not the intended functionality?

Any help much appreciated.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
cdelacombazcommented, Feb 16, 2021

@diegomura I tried already for several hours to make minPresenceAhead, widow and orphans work. It seems that this is also the case for tomgallagher and others (some other issues are opend regarding this).

The link on the website with the example is broken.

Could you please tell us if these features are working and if yes, on which version of react-pdf? I tried:

  • 1.6.13
  • 2.0.0-beta.6
  • 2.0.0-beta.18

If it is working, are there any examples? (If not and you tell me where and how it should work, I could try it out and create a PR with example)

If it is not working currently, is it planned for v2.0? Any time estimate when this is going to be launched?

Thanks in advance! BR, Cedric

1reaction
cdelacombazcommented, Feb 15, 2021

I now tried a similar example than yours and my suggestion doesn’t work… You kind of have to know how big the next component is in advance… @tomgallagher Did you actually solve this issue by now? How did you do it? 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Element.nextElementSibling - Web APIs - MDN Web Docs
The Element.nextElementSibling read-only property returns the element immediately following the specified one in its parent's children list, ...
Read more >
MOVE statement - IBM
If you include a PARENT, PREVIOUSSIBLING, NEXTSIBLING, FIRSTCHILD, or LASTCHILD clause, the MOVE statement attempts to move the target reference ...
Read more >
Page wrapping - React-pdf
React-pdf also enables you to transform breakable elements into their opposite, ... occur between all sibling elements following the element within n points ......
Read more >
Beautifulsoup - nextSibling - GeeksforGeeks
The nextSibling property is used to return the next node of the specified node ... Example 1: Finding all the next siblings of...
Read more >
Finding the next and previous sibling elements that match a ...
Earlier this week, we looked at how to get the next and previous sibling of an element with vanill JS. But what if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found