Case StudiesBlogAbout Us
Get a proposal

How to generate PDF from HTML in React / Node.js app

Eugene Zolotarenko

May 20, 20216 min read

ReactNode.js

Table of Content

  • Using native browser printing with CSS Print Rules

  • Making a Screenshot from the DOM (HTML => Canvas => Image => PDF)

  • Using PDF / JavaScript libraries

  • Using Puppeteer, Headless Chrome with Node.js

  • Conclusion

  • FAQs

Generating PDF from a web page would seem a rather simple action requiring little time and effort. However the reality is somewhat different, and finding the best solution can often be challenging.

Let's consider this hypothetical scenario:

We have a React App through which we'd like to create a PDF either from the entire page or just in part. Our PDF document could contain charts, tables, images and/or plain text and should be structured without cuts or overlappings. We also want a button on the page allowing us to save our document.

In this article, I will walk you through some different solutions whilst outlining the pros and cons of each. We will start with the simplest method then graduate to the most complex.

Using native browser printing with CSS Print Rules

Generally speaking, a browser can already save and print PDFs from our pages:  just press Ctrl/Cmd + P for the adjustable document pop-up by which you can customize its appearance. 

Creating a button to perform the same action is as follows:

const SavePdfButton=()=>{
	return (
		<button onClick={window.print()}>Download PDF</button>
	)
}

Should we wish to change its appearance, hide certain items or change the elements' size in the PDF, we can write CSS print rules:

@media print {
	.save-button {
		display: none;
	}
	.content {
		width: 100%;
	}
}

We also might want to manage page breaks and/or eliminate overlappings. This can be achieved with some specific style properties as shown in this example:

@media print {
	h1 {
		break-before: always;
	}
	table, img, svg {
		break-inside: avoid;
	}
}

Here's a free excellent article describing more of what you can do using these print rules in CSS.

For something small and simple this is an ideal solution and one that would be over-engineered by the use of libraries.  But it is not so ideal when access to code-generated documents is required. 

Pros: 

There are no external libraries

It is simple to implement, including adjustments for page orientation.

It does not overload the user's machine

It allows the user to select and search text

Cons:

It can be problematic rendering identical results in different browsers

Save buttons can be difficult to find owing to browser-rendering discrepancies

There is no access to code-generated PDFs

The PDF document content is dependent upon the size of the browser window

Making a Screenshot from the DOM (HTML => Canvas => Image => PDF)

Here's another straightforward solution: just take a screenshot of the page or element and convert it to a PDF document with Canvas and image transformation:

html2canvas for creating a screenshot from HTML and generating Canvas from it

jsPDF to transform an image to PDF

We can make the Canvas => Image part with vanilla JavaScript.  Accordingly, the function will look like this:

import html2canvas from 'html2canvas'
import jsPdf from 'jspdf';

function savePdf(){
	const domElement=document.querySelector('#container')
	html2canvas(domElement, { onclose: (document)=>{
	document.querySelector('#save-button').style.visibility='hidden'
	}})
.then((canvas)=>{
	const img=canvas.toDataURL('image/jpeg')
	const pdf=new jsPdf()
	pdf.addImage(imgData, 'JPEG',0,0,width,height)
	pdf.save('filename.pdf')
})
}

As you can see, it is possible to add some styles before the HTML => Canvas transformation.

However, if the HTML is lengthy you might want to relegate different elements to separate pages. To do this, create a screenshot from multiple elements and combine them into one PDF document:

import html2canvas from 'html2canvas'
import jsPdf from 'jspdf'

function savePdf(){
	const domElements=document.querySelectorAll('.container')
	const pdf=new jsPdf()
	
	domElements.forEach((element,i)=>{
	const img=canvas.toDataURL('image/jpeg')
	pdf.addImage(imgData,'JPEG',0,0,width,height)
	const isSectionLast=domElements.length===i+1;
	
	if(isSectionLast){
		pdf.save('filename.pdf')
	}else{
		doc.addPage()
	}
	}
}

This way, you can create decent PDFs looking just like the original HTML, and now have control over both the document's appearance and the elements that can be included in it.  The downside, however, is that there is still no capacity to select and search the text.  

Pros:

It is highly similar to HTML

Easy implementation

It has access to generated PDF from code

Cons:

The user is unable to select and search text, especially with cookie notices present.

The PDF document content is dependent upon the size of the browser window

External packages are required

Using PDF / JavaScript libraries

jsPDF (as mentioned), PDFKit, React-pdf are all libraries you can use to create PDF in React, however, a problem remains: that all  HTML and CSS must be specifically created for your PDF document.

In our scenario, then, this solution is also insufficient since we prefer simply to copy our HTML with minor changes, not rewrite it with variations on the same design. Still, it is a useful option if you want to create a PDF from scratch using information from another source.

Here's how it looks with React-pdf:

import {Page,Text,View,Document,StyleSheet} from "@reat-pdf/renderer';

const styles=StyleSheet.create({
	page:{
		backgroundColor: "#E4E4E4"
	},
	section:{
		margin: 10,
	}
})

const MyDocument=()=>{
	<Document>
		<Page size="A4" style={styles.page}>
			<View style={styles.section}>
				<Text>Section</Text>
			</View>
		</Page>
	</Document>
}

Pros:

It gives access to generated PDF from code

The PDF document content is not dependent upon on the browser window size or the date of creation.

The result is identical in different browsers

 It is able to select and search text

Cons:

It is unable to copy the existing on-page HTML

It can be quite time-consuming.

The code is likely to contain two different variations of the same design

Using Puppeteer, Headless Chrome with Node.js

Given its code is written on the back-end, this solution is the most complex and unlike any of the fully client-side ones mentioned above. 

In other words, the Puppeteer is a browser you can run from Node.js.  And from the documentation, we see that it can be used to generate screenshots and PDFs of pages. 

Here's an example that navigates directly to the URL, changes some styles and generates a PDF file:

const puppeter=require('puppeteer')

async function savePdf(){
	const browser=await puppeteer.launch({headless: true})
	const page=await browser.newPage();
	await page.goto('https://start-up.house',{waitUntil:'networkidle0'});
	await page.addStyleTag({content: '#save-button {display: none}'})
	const pdf=await page.pdf({format: 'A4'});
	await browser.close();
	return pdf;
}

Once created, the document is sent back to the front-end.  On the client-side, it is then fetched, transformed to the blob and saved. 

Like so:

function savePdf(){
	return fetchPdfData().then((pdfData)=>{
		const blob=new Blob([pdfData],{type: 'application/pdf'})
		const link=document.createElement('a')
		link.href=window.URL.createObjectURL(blob)
		link.download='file-name.pdf'
		link.click()
	}
}

This seems the most comprehensive solution as it affords the greatest number of benefits and can address even the most difficult of cases.  Moreover, the document it will generate allows the selecting and searching of text and can also be saved on the server without any additional API calls.  

Pros:

It has access to generated PDF from code

The PDF document content is not dependent upon the size of the browser window

It allows you to select and search text

It is very similar to HTML

Does not overload the user's machine

Cons: 

It requires client and server-side code

Implementation can be complicated in some cases

Conclusion

As we've seen, generating PDFs from HTML can be problematic.  But it need not be, and the examples above are just a few options for you to consider when tackling the issue.  With a bit of trial and error you'll breach the impasse, so do try tinkering with some different options to determine which solution works best for you. 

And... good luck!

If you would like to know more, contact us - 

FAQs

How can I save my entire HTML page as a PDF document using React?

  • Using various methods discussed in the article, you can convert your HTML page to a PDF document. One straightforward way is to use native browser printing with CSS print rules.

What libraries can assist in converting HTML to PDF in a React app?

  • React PDF, PDFKit, and jsPDF are some popular libraries for this purpose. However, if you're specifically looking at the React PDF library, it's crucial to use the appropriate React PDF components to achieve the desired result.

Does exporting an HTML page to a PDF file retain the background color and custom fonts?

  • It largely depends on the method you use. Some methods may not retain the background color and custom fonts, but others, especially those using Puppeteer or dedicated libraries, often do.

When I try to create a PDF document from my React app, the background color differs from my original HTML page. Why?

  • The exact replication of the background color when converting an HTML page to a PDF document can be influenced by the method or library you're using. It's essential to ensure that the library or method you choose supports the preservation of background color.

Is it possible to export default app content from my React project to a PDF file?

  • Yes, depending on the content and structure of your React app, you can use tools and libraries to export default app content into a PDF file seamlessly.

Can I convert an HTML string to a PDF document using jsPDF in a React app?

  • Yes, with jsPDF, you can convert HTML, including HTML strings, to PDF documents. Pairing it with other tools or libraries can further streamline the conversion process in a React app.

What are the main differences between converting HTML to PDF and taking a screenshot using the DOM method in a React app?

  • When converting HTML to PDF directly, the resultant PDF often allows text selection and searching. However, when using the screenshot method (HTML => Canvas => Image => PDF), the PDF is essentially an image, so text selection might not be possible.

How can I ensure consistent background color when converting my HTML page to a PDF document?

  • Using dedicated libraries or tools that prioritize preserving styles, like Puppeteer or React PDF, can help ensure the background color remains consistent during the conversion.

Between creating a PDF file from scratch using React PDF and converting HTML to PDF, which method is more time-efficient?

  • Converting existing HTML to PDF is generally faster as you're using existing content. Creating a PDF file from scratch using React PDF might be more time-consuming, especially if the design is intricate.

How do I handle large PDF files when converting a lengthy HTML page using React?

  • If you're dealing with a lengthy HTML page, it's advisable to divide the content into sections or pages for better readability. Libraries like Puppeteer provide functionalities to manage and paginate long content effectively.

What is the 'html to pdf react' process in web development? 

  • The 'html to pdf react' process refers to the method of converting HTML content within a React application into a PDF document. This is typically done using libraries that capture the HTML structure and styling to create a PDF file that can be saved, printed, or shared.

Which libraries are commonly used for 'html to pdf react' conversions? 

  • Popular libraries for 'html to pdf react' conversions include jsPDF and html2canvas. These libraries work together to capture the HTML rendered by React components and convert it into a PDF format, maintaining the layout and styles as closely as possible.

Are there any challenges in the 'html to pdf react' conversion process? 

  • One of the main challenges in the 'html to pdf react' process is ensuring that the converted PDF accurately reflects the original HTML content in terms of layout, styles, and interactivity. Handling dynamic data and large documents efficiently is also a common challenge developers face.

How can I ensure high-quality output in the 'html to pdf react' process? 

  • To ensure high-quality output in the 'html to pdf react' process, choose reliable libraries, test the conversion with various types of content, and consider implementing custom solutions for complex layouts or dynamic data. Regular updates and optimization of the code can also contribute to better quality PDFs.

Is it possible to handle interactive content in 'html to pdf react' conversions? 

  • While basic interactivity can be handled in the 'html to pdf react' process, complex interactive elements like hover effects or clickable links may not be fully replicated in the PDF. The focus is usually on accurately capturing the visual presentation rather than the interactive aspects.

Published on May 20, 2021

Share


Eugene Zolotarenko

Front-End Developer

Digital Transformation Strategy for Siemens Finance

Cloud-based platform for Siemens Financial Services in Poland

See full Case Study
Ad image
How to generate PDF from HTML in React / Node.js app
Don't miss a beat - subscribe to our newsletter
I agree to receive marketing communication from Startup House. Click for the details

You may also like...

Node.js developers building scalable backend services for modern web applications
Node.jsBack-end developmentProgressive Web Apps

Node.js Development Service

Node.js enables fast, scalable, and real-time applications. Our Node.js development services help businesses build secure, high-performance backends designed for growth.

Alexander Stasiak

Feb 05, 202610 min read

Finding the Best Node.js Development Agency in 2023: A Comprehensive Guide
Node.jsDigital products

Finding the Best Node.js Development Agency in 2023: A Comprehensive Guide

Node.js has become an instrumental technology for developing robust, scalable web applications. In this context, the role of a Node.js development agency is more crucial than ever. This article will guide you through choosing the top Node.js development companies for your specific needs.

Marek Pałys

Feb 02, 20236 min read

How we implemented SWR in a project and why we loved it
Next.jsReact

How we implemented SWR in a project and why we loved it

SWR, a React Hooks library for remote data fetching, provides a simple yet powerful solution for caching, revalidating, and retrieving data in React applications. Learn how SWR eliminates the complexities of managing state slices, speeds up development, and enhances UI performance. Explore examples of SWR implementation and discover its integrations.

Kamil Polok

Jun 09, 20216 min read

4 reasons to use Chakra UI in your next project
Chakra UIReact

4 reasons to use Chakra UI in your next project

Chakra UI is a simple, modular, and accessible component library for building React apps with speed. With its easy customization, dark mode support, responsive design capabilities, and focus on accessibility, Chakra UI stands out among other UI libraries, making it an excellent choice for React developers.

Mateusz Wójcik

Jan 19, 20214 min read

Recently added

A cloud operations team monitoring infrastructure health, resource provisioning, and security dashboards across multiple screens
Cloud OptimizationFinOpsInfrastructure

Cloud Infrastructure Management

What it takes to run cloud infrastructure that's scalable, secure, and cost-efficient — the core pillars, FinOps, AI-driven ops, and how to pick a partner.

Alexander Stasiak

Jun 12, 20268 min read

A compliance dashboard displaying SOC2, ISO 27001, GDPR, and HIPAA controls with real-time drift detection in a cloud environment
GDPR complianceSOC2Cloud Compliance

Cloud Security Compliance

A step-by-step path to SOC2, ISO 27001, GDPR, and HIPAA in the cloud — including the move to compliance-as-code for scaling safely.

Alexander Stasiak

Jun 09, 202610 min read

A solar farm with PV panel rows under a clear sky overlaid with a translucent analytics dashboard showing performance ratio, irradiance forecasts, and fault-detection alerts
Data Analysis Renewable energy optimizationPredictive Analytics

Data Analytics in Solar Energy

Global solar PV capacity passed 1,500 GW in 2025, and with hardware costs at historic lows, the next competitive edge isn't installing more panels — it's squeezing more value out of the ones already in the field. Modern solar plants generate millions of data points daily from SCADA, IoT sensors, weather APIs, and market feeds, but only operators with the right analytics layer convert that data into yield gains, lower O&M costs, and smarter market participation. This guide breaks down how data analytics is reshaping every stage of the solar lifecycle in 2026 — from site selection and design to predictive maintenance, grid integration, and financial modeling — with concrete benchmarks, KPIs, and implementation timelines.

Alexander Stasiak

May 03, 20268 min read

A smartphone screen displaying multiple value-added service icons — carbon tracking, smart home control, telemedicine, and AI assistant — layered above a banking app interface
Customer experienceFinancial TechnologyFintech

Value-Added Services (VAS) Examples

By 2026, most core services — data plans, current accounts, cloud hosting — have become fully commoditized, and the companies winning customer loyalty aren't the ones cutting prices. They're the ones layering smart value-added services (VAS) on top: carbon footprint trackers in banking apps, smart-home bundles from ISPs, AI copilots inside SaaS platforms, and Amazon Prime-style subscriptions that turn one-time buyers into long-term subscribers. This guide breaks down concrete VAS examples across telecom, banking, retail, and SaaS, explains why operators offering VAS see up to 30% ARPU uplift, and gives you a practical 5-step framework to identify which value-added services will actually move the needle for your product.

Alexander Stasiak

May 01, 202611 min read

A developer working with an AI assistant interface that displays retrieved context sources, conversation memory, and connected tool integrations in a clean dark-mode dashboard
AI AgentsEnterprise AIEnterprise Innovation

AI Agents Use Cases 2026

AI agents are no longer a research demo — they're now reading customer history in real CRMs, monitoring thousands of transactions per second for fraud, drafting pull requests against production codebases, and rebalancing logistics fleets without human input. The shift from reactive chatbots to autonomous, tool-using, multi-step agents is why 2024–2026 marks the inflection point for enterprise adoption. This guide breaks down concrete AI agent use cases across customer service, sales and marketing, software engineering, finance, logistics, healthcare, HR, and retail — plus the architecture decisions, governance practices, and implementation tips that separate production-ready agents from clever prototypes.

Alexander Stasiak

Apr 29, 202611 min read

Architecture diagram of a real-time fraud detection system with streaming ingestion, feature store, model scoring, and decision engine
Tech LeadershipSoftware Engineering PracticesSoftware development

Tech Lead Roles and Responsibilities

The tech lead has become one of the most indispensable — and most misunderstood — roles in modern software teams. Often confused with engineering managers, tech leads are senior individual contributors who own technical direction, delivery quality, and team enablement, all while staying hands-on with code. This guide breaks down what the role actually entails in 2026: core responsibilities, essential skills, a realistic day-in-the-life, how the role differs across startups, enterprises, and agencies, and a practical roadmap for engineers ready to grow into it.

Alexander Stasiak

Apr 28, 202612 min read

Ready to centralize your know-how with AI?

Start a new chapter in knowledge management—where the AI Assistant becomes the central pillar of your digital support experience.

Book a free consultation

Work with a team trusted by top-tier companies.

Rainbow logo
Siemens logo
Toyota logo

We build what comes next.

Company

Startup Development House sp. z o.o.

Aleje Jerozolimskie 81

Warsaw, 02-001

VAT-ID: PL5213739631

KRS: 0000624654

REGON: 364787848

Contact Us

hello@startup-house.com

Our office: +48 789 011 336

New business: +48 798 874 852

Follow Us

Award
logologologologo

Copyright © 2026 Startup Development House sp. z o.o.

EU ProjectsPrivacy policy