DEV Community

Turning React apps into PDFs with Next.js, NodeJS and puppeteer

Jordy Koppen on March 21, 2022

Hey everyone, let me preface this by saying: This is NOT a production ready implementation. There's several things we can implement to make this mo...
Collapse
 
ecyrbe profile image
ecyrbe • Edited

You should take a look at react-pdf.

It works in nodejs as well as in browsers.

It's ultra light, a lot faster than using puppeteer.

Edit: fix URL

Collapse
 
agnel profile image
Agnel Waghela • Edited

Currently your link to react-pdf points to 'https%20//react-pdf.org'. I think it should be npmjs.com/package/react-pdf.

Also, this post is about generating PDFs whereas react-pdf is meant for displaying existing PDFs only.

If you meant to point at @react-pdf/renderer, then too, using puppeteer seems better, as you cannot render custom react components within the pdf using @react-pdf/renderer.

Collapse
 
ecyrbe profile image
ecyrbe • Edited

Actually, no. React pdf Can generate pdf on fly. I use it on production on Big applications since more than 2 years.
It's not meant to render existing pdf at all.
Take a Closer look at the documentation.

You can of course create and render custom components with react-pdf. We do it all the time. What you can't do, is use react components that target the browser DOM.

Thread Thread
 
agnel profile image
Agnel Waghela

The updated link is pointing to the official website for the npm package @react-pdf/renderer. This does have a feature to generate a pdf on the fly, no doubt. It does support PDF generation, unlike the npm package react-pdf.

I wasn't successful in using mui with @react-pdf/renderer. May be I'm missing something.

Thread Thread
 
ecyrbe profile image
ecyrbe • Edited

Mui is only targetting the DOM. So it can not work. I said you can use custom components, but not use ones that target the DOM.

Let me expand on why i suggest anyone to use react-pdf instead of react+pupppeteer.

What are the issues with puppeteer

The author made some disclaimers about his solution not being production ready. And it's for a good reason. Not just because of paginations issues.

  • puppeeter is executed on backend only.
  • It does not scale : one instance of pdf generating will use at least 2GO and more
  • one instance will use a lot of CPU
  • huge PDF size: generating documentation for hundreds of pages is not practical as you end up with pdf size that no sane user will want to download
  • rendering your pdf will be slow
  • no interactive pdf because your pdf are based on images, not actual searchable text in pdf, no clickable links...
  • you will not be able to control pagination without trial and error.

What are the advantages of using puppeteer

  • you develop your components only once
  • you will be able to set it up fast. in fact, you can set this up in a day.

What are the issues with react-pdf

  • if you need to share components with the DOM and react-pdf :
    • you'll need to implement your own components that targets both the DOM and PDF (aka : TextField, CheckBox, etc). i would not recommend that path. A lot of DOM components do not make sense in the pdf world (ie: Search bar, menu, etc)
  • you'll need more time than puppeteer solution as you'll need to create custom components that match your design.

What are the advantages of using react-pdf

  • ultra light (only a few megabytes of RAM)
  • blazing fast pdf rendering
  • works on both frontend and backend
  • scale to serve thousands/hundreds of thousands users , even in severless environments
  • small generated PDF size (few Kbytes)
  • interactive pdf with searchable text, links, pdf annotations
  • pagination out of the box that just work

From an engineering point of view, using puppeteer for a small project that have really few users is ok, because you'll not have to invest too much of your time. But for large scaling projects, i would recommand using react-pdf.

Thread Thread
 
agnel profile image
Agnel Waghela

Thank you @ecyrbe for elaborating on the issues and the advantages.

Collapse
 
biomathcode profile image
Pratik sharma

I would recommend that too!

Collapse
 
abrahammzansie profile image
Abraham Mzansie Nkomo

HI , Can l use react-pdf on node js backend as well ? . If yes please show me how

Collapse
 
ecyrbe profile image
ecyrbe

Yes you can.
Take a look at the docs. On backend, use renderToStream to generate pdf on the fly.

Thread Thread
 
abrahammzansie profile image
Abraham Mzansie Nkomo

l checked the documentation and l still do not understand how to generate on backend side . Please @ecyrbe assist me

Collapse
 
khadetou profile image
khadetou

Does it work on nextjs though ?

Collapse
 
ecyrbe profile image
ecyrbe

Yes, no worries.

Collapse
 
luudv profile image
Luu Dao

Current version doesn't seem to support react 18 and next 1.3, I tried but it deosn't work.

Collapse
 
hassnainabass profile image
Hassnain Abass • Edited

i am using phantomjs always to generate pdf, built a service in nodejs, puppeteer and Phantomjs which i use on all my apps, Nice write +1

Collapse
 
eliasfloreteng profile image
Elias

Phantomjs is nice although it is no longer maintained, an alternative is playwright.dev which I like very much :)

Collapse
 
neeshsamsi profile image
Neesh Samsi

This is a great article. I was stuck trying to figure this out, I almost succeeded trying to use react server to render a component to html and then use some html to pdf rendered but then the tailwind styles weren't being applied. This is a life saver. Would absolutely love a follow up for a production ready implementation with next & tailwind!

Collapse
 
cednore profile image
cednore

Actually, I used to rely on puppeteer in production level to generate the PDF files based on the content we have on our product. Also, we did see some of limitations you mentioned here, especially we we had to deal with different page sizes on a single document. Nice explanation btw, Kudos!

Collapse
 
donovanh profile image
Donovan

Oh wow, this is just what I was looking for. Nice write up too +1

Collapse
 
vjr2nd profile image
Vijay Roy 🇨🇮

Never seen such easy explanation before. keep it up bro 👍

Collapse
 
jordykoppen profile image
Jordy Koppen

Thanks, I appreciate it!

Collapse
 
skruzic1 profile image
Stanko Kružić

Hi,
the article is great, however, it seems that generated PDF is not searchable and the text in it is not selectable. However, chromium (and puppeteer for that matter) by default produces searchable PDF. Do you know how to resolve this? Thanks!

Collapse
 
katiefuwang profile image
Katie / World Class Code

My resume looks identically to yours? I used React, not Next JS. When printing to PDF I just hit CMD+P on my mac and click Print to PDF. That's it. It does not do page breaks very well... perhaps I can have that with your code? Can we have the source code?

Collapse
 
katiefuwang profile image
Katie / World Class Code

I found your repo. How about page breaks? Your CV is only one page :)

Collapse
 
dspsolves profile image
Dhawal Singh Panwar

Concisely explained. ⭐

Though, I've faced issues deploying a puppeteer based solution in Docker. Stackoverflow-ing my way, I found out that you need to build the package in the Dockerfile's commands and then use it somehow.

I'm showing interest in discussing the how-tos on making this production ready and make it work as a separately deployed microservice. ✌🏻

Collapse
 
jordykoppen profile image
Jordy Koppen

Thank! I’ll see what I can come up with for a next post!

Collapse
 
nxmxgoldxx profile image
Calenté Cardwell

Is this posted in a repository ? We think we are overthinking it and erasing components we actually needed.

Collapse
 
ajinspiro profile image
Arun Kumar

Thanks for sharing..!!
Also thanks for the insights @ecyrbe!!