diegomura / diegomura/react-pdf

Allow a (simpler) way to reference page numbers from other pages when using Node API

Open
#2,405 0 comments 0 reactions 0 assignees View on GitHub
new feature
Dominant language
TypeScript
Stars
16.8k
Forks
1.3k
Avg merge
5h 6m
Merged PRs (30d)
52

Description

I am generating a PDF that requires referencing which page numbers particular sections start on, e.g. Section B: page 5.

Currently I use the `render` callback on a `View` in the Page I want to know the page number of, and store it in state, so I can display it elsewhere. Here is a very simplified version of this:
```
const [somePageNumber, setSomePageNumber] = useState(0);

return (


Section B: page: {somePageNumber}

{/* Other stuff in between... */}


Section B
{
setSomePageNumber(pageNumber);

return ;
}}
/>


);
```

This works fine in the browser environment (using the `PDFViewer` component). But I need to be able to generate the file using the Node API, which I'm using `renderToStream` for.

The problem here is that the PDF generates before the state update has time happen, so the pageNumber will always be 0.
To get around this I am doing something along the lines of:

```
const onPagesCalculated = (pages) => {
const finalPDFStream = await renderToStream();
}

await renderToStream();
```

Which means I have to generate the PDF twice.

**The solution**
One solution could be some mechanism to make sure that the `renderToStream` doesn't return until the state updates have finished, not sure how it would know that this has occurred though so maybe this isn't feasible.
Or another could be some way to reference page numbers from other pages, maybe using an id or something.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.