DEV Community

Creating PDF Files Without Slowing Down Your App

Simon Hessel on February 19, 2023

In many web applications, there are times when we need to generate and download PDF files on the client-side. However, for the sake of showing a li...
Collapse
 
sriram_vemparala_e7c93924 profile image
sriram vemparala

Hi @simonhessel ,

Thank you for this great article and I have a few questions about the rendering process.

When I enter some text into the document the PDF blinks and this is due to the rendering process i guess and this gives a bad user experience. is there a way to make the document not to blink ?

Thank you.

Collapse
 
simonhessel profile image
Simon Hessel

Hi,

Thank you!

You can prevent the flashing during PDF rendering by replacing the default browser renderer (e.g., iframe) with a solution like react-pdf, which uses canvas and pdfjs to render the file. It includes an onLoadSuccess prop that calls a function once rendering is complete.

For a smoother experience, you can display the previous PDF version until the new one is fully pre-rendered. You can see an example in the official react-pdf REPL, and here’s a relevant code snippet from the repo.

Best,

Simon

Collapse
 
radandevist profile image
Andrianarisoa Daniel • Edited

I had some issues after upgrading some packages in our project. Thanktfully, the example repo is also up to date (worker shim)!! Nice work and thank you.

Collapse
 
michele_buzzoni_4ab920134 profile image
Michele Buzzoni

i implemented this but when i click the download button the ui is freezing, can you tell me why?

Collapse
 
simonhessel profile image
Simon Hessel

Sorry no idea, do you get this error with the above demo?

Collapse
 
rajivchaulagain profile image
Rajiv Chaulagain

I think the issue is worker is being used for rendering the pdf into the browser but in case of download it is working on the main thread so it is giving issue

Collapse
 
davidflr1 profile image
David Flores

if someone can figure out how to implement this on next would be awesome, no succes from my side yet

Collapse
 
simonhessel profile image
Simon Hessel

@davidflr1 please find the same example using next here github.com/shkreios/nextjs-pdf-ren...

Collapse
 
nuruddin999 profile image
Nuruddin999

Nice article ! If i implement this approach, will my bundle chunk became smaller ? Now now i have 1.6 mb size

Collapse
 
simonhessel profile image
Simon Hessel

Sadly, no, since Vite triggers a second Rollup build when bundling code for a worker. See this issue for more info.

This may be different when using another bundler.

Collapse
 
bdamor5 profile image
Bhavesh Damor • Edited

hey Simon, i am facing an error saying - DOMException: Failed to execute 'postMessage' on 'Worker': Symbol(react.element) could not be cloned. How to resolve this? Thank you

Collapse
 
simonhessel profile image
Simon Hessel

@bdamor5 to me this sounds like that react is not available inside the webworker. Did you use the example setup from the blogpost?

If not make sure that whatever bundler you are using that react is part of both the main & worker bundle.

Collapse
 
krzysztof_wolny_b6796f358 profile image
Krzysztof Wolny

I am getting error that window is not defined. How I can fix this?

Collapse
 
rajivchaulagain profile image
Rajiv Chaulagain

it seems the issue is with workerShim.js as it helps to make the window and document accessible