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.

Generating two blank pages

See original GitHub issue

Describe the bug When i try to generate a pdf file on browser, the content of my file are two blank pages, no matter what i try to put inside.

To Reproduce This is the code what i call to generate

<PDFDownloadLink document={<RelFatura dados={this.state.excel} titulo={'Voucher a faturar'} />} fileName="Vouchers.pdf">
    {({ blob, url, loading, error }) => (loading ? <i className="fa fa-file-pdf-o"> Loading document...</i> : <i className="fa fa-file-pdf-o"> Gerar pdf </i>)}
</PDFDownloadLink>

this is my RelFatura

export const RelFatura = (props) => (
  <Document>
    <Page size="A4" style={styles.page}>
      <Text style={styles.text}> hi</Text>
    </Page>
  </Document>
);

and these are my styles

const styles = StyleSheet.create({
  page: {
    paddingTop: 35,
    paddingBottom: 65,
    paddingHorizontal: 35,
    paddingRight: 35,
  },
  text: {
    margin: 10,
    fontSize: 12,
    textAlign: 'justify',
    fontFamily: 'Times-Roman'
  }
});

i have two pages that use the pdf generator, in one of them he works perfectly i take the RelFatura and put there where it is working and the output was what i expected ( the page with hi written ) but in the other page he give the blank page bug, i try put the same code on both pages nevertheless it did the same bug (one of them still working and the other give me the bug)

Expected behavior A single page with hi written

Screenshots capturar

Desktop (please complete the following information):

  • OS: Ubuntu, Windows
  • Browser: chorme
  • React-pdf version: 1.4.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:9

github_iconTop GitHub Comments

6reactions
lucas-wilmartcommented, Mar 12, 2020

I was facing the same issue with the PDFDownloadLink Component. Thanks to the suggestions above, I was able to figure a solution out and resolved it by memoizing it with React’s hook useMemo

const stuff = useMemo(
  () => (
    <PDFDownloadLink document={<AccountingPDF />} fileName="somename.pdf">
      {({ blob, url, loading, error }) => (loading ? 'Loading document...' : 'Download now!')}
    </PDFDownloadLink>
  ), [])

I think the documentation should mention this behavior more clearly as it’s confusing.

2reactions
birch-jaytoncommented, Jan 9, 2020

This was happening to me due to my component triggering a re-render from a state change before my document viewer was finished mounting. This happened because the state change happened on mount.

function MyPdfAppComponent(){
   const [dataForPdf, setDataForPdf] = React.useState()
   React.useEffect(()=>{
      goFetchTheData().then(res=> setDataForPdf(res))
   },[])
   return(
      <PDFViewer>
         <MyDocument data={dataForPdf}/>
      </PDFViewer>
   )
}

this can be fixed by making sure your PDF viewer has everything it needs before its initial render:

function MyPdfAppComponent(){
   const [dataForPdf, setDataForPdf] = React.useState()
   React.useEffect(()=>{
      goFetchTheData().then(res=> setDataForPdf(res))
   },[])
   return{
      dataForPdf && (
      <PDFViewer>
         <MyDocument data={dataForPdf}/>
      </PDFViewer>
      )
   }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I insert double blank pages - LaTeX Stack Exchange
Depending on what you're actually trying to do, I'd use \cleardoublepage . You should just be able to add another \newpage in the...
Read more >
How to Add Blank Page in Microsoft Word - YouTube
How to add a blank page in Microsoft Word? In this tutorial, I show you how to add a blank page to a...
Read more >
Adding and Removing Blank Pages - Atticus
Begin on Right Side of Page. One of the most common reasons for adding a blank page is to have the first page...
Read more >
blank pages appearing as page numbers added to document
Use INSERT tab to add two blank pages in front of the text for the TOC and the LOF; Precede each chapter header...
Read more >
How to Insert and Delete a Blank Page in Microsoft Word (PC ...
Select Blank Page in the Pages group. Blank Page button in Word 365. Figure 2. Blank Page button. Your blank ...
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