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.

Long Render Time

See original GitHub issue

Describe the bug The below code is taking about 10 seconds to render a three page document using the <PDFViewer>. The code currently iterates over a ~80 item array of objects and as the number of items in the array increases this render time gets longer.

To Reproduce I am using the following code:

import React, { Component } from 'react'
import { PDFViewer, Document, Page, View, Text } from '@react-pdf/renderer'
import { flatStrips } from './flatStrips'

class App extends Component {
  
  render() {
    return (
      <div className="App">
      
        <PDFViewer>
          <Document>
            <Page>
              {flatStrips.map((strip,i)=>(
                <View key={i}>
                  {strip.scene && <Text>{strip.scene}</Text>}
                  {strip.ie && <Text>{strip.ie}</Text>}
                  {strip.set && <Text>{strip.set}</Text>}
                  {strip.dn && <Text>{strip.dn}</Text>}
                  {strip.desc && <Text>{strip.desc}</Text>}
                </View>
              ))}
            </Page>
          </Document>
        </PDFViewer>

      </div>
    )    
  }
}

export default App;

REPL Here This contains a shorter version of the data array, as the REPL couldn’t share a link with the full data. All styles were removed to ensure that the slowness wasn’t caused by any other factors.

The REPL works fine but when using <PDFViewer> on localhost the render times are extremely slow. A full build running on a live server shaves about one second from this render time.

I have tested the render time of the Document taking <PDFViewer> out of the code and it renders in a few milliseconds. I can’t seem to work out what is causing the delay. Also of note, the browser seems to freeze as this delay is happening. When trying to use the dev tools console during the delay, it is unresponsive until the render is complete. I have tried different versions of the above code for a few days now, simplifying it in each iteration. The above code is very simple and running in its own brand new create-react-app application to ensure that no outside issues from the rest of the site are affecting it.

This issue is a concern as this sample document is only three pages long and my users will be creating documents that could be exponentially longer.

Expected behavior A<PDFViewer> render time in the ms range

Desktop (please complete the following information):

  • OS: MacOS 10.14.3
  • Browser Chrome 72.0.3626.119
  • React-pdf version 16.8.5

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
wyqydsyqcommented, Aug 5, 2020

I’ve also ran into this issue, I’m generating a PDF which has a dynamic number of pages, 10+ pages causes the browser to lock up entirely for 5+sec which is pretty terrible UX.

Is there no way to defer / promisify the render process so it does not block the main UI thread? I’ve tried using a pattern like this by rendering the Document into a ref which then gets passed to both BlobProvider and the PDFViewer:

  <BlobProvider
    document={pdfRef.current}
  >
    {({ url, loading }) =>
      loading ? (
        <LoadingStatus />
      ) : (
        <Box padding={2}>
          <PDFViewer
            style={{
              display: 'flex',
              width: '100%',
              minHeight: '500px',
              border: 'none',
            }}
          >
            {pdfRef.current}
          </PDFViewer>
          <WrapperLink
            href={url || ''}
            download={`${handle}-tables.pdf`}
            type="application/pdf"
          >
            <FullButton fullWidth endIcon={<DownloadIcon />}>
              Download
            </FullButton>
          </WrapperLink>
        </Box>
      )
    }
  </BlobProvider>

Which kind of seems to work in that I see a loading spinnter initially, but the whole window still locks up when it comes to rendering the PDF in PDFViewer, seemingly because react-pdf completely regenerates the PDF from the given VDOM passed to PDFViewer, instead of reusing the blob already generated by BlobProvider. I think PDFViewer needs to be able to accept pre-baked PDF blobs.

I feel like this could maybe be solved nicely by allowing PDFViewer to accept a blob from a loaded BlobProvider, rather than only accepting a Document VDOM as children

1reaction
modemmutecommented, Mar 23, 2019

Thanks for the quick reply @diegomura! Answers below:

Document taking out of the code

What do you mean by that. Rendering the <Document> straight away?

As a test I rendered the<Document> directly without wrapping it in the <PDFViewer>. It was raw, but rendered nearly instantly. Just so I understand the rendering process correctly, I’m guessing that the heavy lifting is being done by the <PDFViewer> and not when the <Document> is created?

Also of note, the browser seems to freeze as this delay is happening.

Yes. This is something we should definitely try to improve. #464 discusses the idea of using node/web workers for the rendering process, but I couldn’t find the way to make it work yet.

Is this a known issue then?

I’ll try your snippet by adding more rows and let you know what I can figure out

Many thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How Long Does It Take To Render a Video? (It depends)
1080p Footage and Project – No Effects, just editing, basic Audio: 1 Minute · Add some transitions, Title Effects, Graphics: 2 Minutes ·...
Read more >
What Is Render Time? - Sematext
Render time is the metric that refers to the time it takes for a website or web app to load enough that the...
Read more >
3D Render Time Calculator
Set Total render time to 1084 days. The 3D render calculator tells us that the average render time per frame for Toy Story...
Read more >
I seem to have excruciatingly long render times for relatively ...
A 15:27 minute video took 7:36 to render. A 16:03 minute video took 7:55 to render. Then a 19:12 minute video took 10:05...
Read more >
What is Start Render Time, and How Can You Improve It?
A good start render time is less than 750 milliseconds, and a bad start render time is anything over 2 seconds. How Do...
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