Nutrient

Home

SDK

Software Development Kits

Low-Code

IT Document Solutions

Workflow

Workflow Automation Platform

DWS API

Document Web Services

T
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Company

About

Team

Careers

Contact

Security

Partners

Legal

Resources

Blog

Events

Try for free

Contact Sales
Contact sales
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

products

Web

Web

Document Authoring

AI Assistant

Salesforce

Mobile

iOS

Android

visionOS

Flutter

React Native

MAUI

Server

Document Engine

Document Converter Services

.NET

Java

Node.js

AIDocument Processing

All products

solutions

USECASES

Viewing

Editing

OCR and Data Extraction

Signing

Forms

Scanning & Barcodes

Markup

Generation

Document Conversion

Redaction

Intelligent Doc. Processing

Collaboration

Authoring

Security

INdustries

Aviation

Construction

Education

Financial Services

Government

Healthcare

Legal

Life Sciences

All Solutions

Docs

Guides overview

Web

AIAssistant

Document Engine

iOS

Android

visionOS

Java

Node.js

.NET

Document Converter Services

Downloads

Demo

Support

Log in

Resources

Blog

Events

Pricing

Try for free

Free Trial

Company

About

Security

Partners

Legal

Contact Sales
Contact Sales
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

products

Products overview

Document Converter

Document Editor

Document Searchability

Document Automation Server

Integrations

SharePoint

Power Automate

Nintex

OneDrive

Teams

Window Servers

solutions

USECASES

Conversion

Editing

OCR Data Extraction

Tagging

Security Compliance

Workflow Automation

Solutions For

Overview

Legal

Public Sector

Finance

All Solutions

resources

Help center

Document Converter

Document Editor

Document Searchability

Document Automation Server

learn

Blog

Customer stories

Events

Support

Log in

Pricing

Try for free

Company

About

Security

Partners

Legal

Contact Sales
Contact Sales
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Product

Product overview

Process Builder

Form Designer

Document Viewer

Office Templating

Customization

Reporting

solutions

Industries

Healthcare

Financial

Manufacturing

Pharma

Education

Construction

Nonprofit

Local Government

Food and Beverage

Departments

ITServices

Finance

Compliance

Human Resources

Sales

Marketing

Services

Overview

Capex-accelerator

Process Consulting

Workflow Prototype

All Solutions

resources

Help center

guides

Admin guides

End user guides

Workflow templates

Form templates

Training

learn

Blog

Customer stories

Events

Support

Pricing

Support

Company

About

Security

Partners

Legal

Try for Free
Contact Sales
Try for Free
Contact Sales
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Services

Generation

Editing

Conversion

Watermarking

OCR

Table Extraction

Pricing

Docs

Log in

Try for Free
Try for Free

Free trial

Blog post

HTML to PDF in React: Convert HTML to PDF using html2pdf.js

Hulya Masharipov Hulya Masharipov

Table of contents

  • Why convert HTML to PDF in React?
  • Prerequisites
  • Setting up a React application
  • Installing html2pdf.js
  • Creating the PDF converter component
  • Integrating the PDF converter component
  • Running your React application
  • Pros and cons of using html2pdf for converting HTML to PDF
  • Alternatives to html2pdf.js
  • Conclusion
  • FAQ
Illustration: HTML to PDF in React: Convert HTML to PDF using html2pdf.js

TL;DR

In this tutorial, you’ll build a PDF converter in React using html2pdf.js. You’ll create a reusable component, apply conversion options, and enable users to generate downloadable PDFs from HTML content. For more complex or enterprise-grade workflows, we also showcase alternatives like Nutrient’s HTML-to-PDF API and Nutrient PDF generation SDK.

Why convert HTML to PDF in React?

Generating downloadable PDFs from dynamic content like invoices, quotes, reports, and documentation is a common use case in modern web apps. While server-side tools exist, a client-side approach like html2pdf.js allows you to deliver this feature without backend dependencies.

If you’re building a React app and need to convert HTML to PDF, this tutorial will show you exactly how to do it using html2pdf.js, a lightweight JavaScript library that enables HTML-to-PDF generation in the browser.

Prerequisites

Before starting, make sure you have the following prerequisites:

  • Node.js and npm (Node Package Manager) installed on your computer.

Setting up a React application

Begin by creating a new React application if you don’t already have one. You’ll use Create React App to set up your project:

npx create-react-app html2pdf-react-app
cd html2pdf-react-app

Installing html2pdf.js

html2pdf.js is a powerful JavaScript library that simplifies the process of converting HTML content to a PDF document. To install it, run the following command inside your project directory:

npm install html2pdf.js

With html2pdf.js in place, you can start building your PDF converter.

Creating the PDF converter component

In your React application, you’ll create a component named PdfConverter, which is responsible for converting HTML content to PDF.

  1. Start by importing the useRef hook from the React library, which will help you reference the HTML content you want to convert. Additionally, import the html2pdf library, which enables HTML-to-PDF conversion:

import { useRef } from 'react';
import html2pdf from 'html2pdf.js';
  1. To customize the PDF conversion process, define an options object. This object will hold various settings that affect how the PDF is generated. You have the flexibility to specify options such as the PDF file name, margins, image quality, paper size, and more.

Here’s an example of defining the options object:

const options = {
	filename: 'my-document.pdf',
	margin: 1,
	image: { type: 'jpeg', quality: 0.98 },
	html2canvas: { scale: 2 },
	jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' },
};

In this example, you set the file name to ‘my-document.pdf’, use JPEG images with 98 percent quality to generate the PDF, and configure other settings like margins and paper size. However, feel free to adjust these options according to your requirements.

html2pdf.js provides many more configuration options and customization features you can explore in its documentation.

  1. Now, define a function named convertToPdf. This function will be responsible for converting the HTML content to a PDF when triggered by a button click. Inside this function, you’ll retrieve the HTML content you want to convert using the useRef hook:

const contentRef = useRef(null);

const convertToPdf = () => {
	const content = contentRef.current;
};

contentRef is a reference to the HTML content you want to convert, and content contains that content.

  1. Use the html2pdf library to initiate and configure the PDF conversion process within the convertToPdf function. Call the html2pdf() function, chain it with the .set(options) method to apply the conversion options, specify the HTML content to convert using .from(content), and then trigger the conversion and save the PDF using .save():

const contentRef = useRef(null);

const convertToPdf = () => {
	const content = contentRef.current;

	html2pdf().set(options).from(content).save();
};

This function takes care of setting up the conversion, specifying the content, and generating the PDF.

  1. Return the JSX structure for your component. Include the HTML content you wish to convert and a button labeled Convert to PDF, and set up an onClick event handler to call the convertToPdf function when the button is clicked:

return (
	<div>
		<div ref={contentRef}>
			{/* Your HTML content that you want to convert to PDF */}
			<h1>Hello, PDF!</h1>
			<p>
				This is a simple example of HTML-to-PDF conversion using
				React and html2pdf.
			</p>
		</div>
		<button onClick={convertToPdf}>Convert to PDF</button>
	</div>
);

Here’s the full code:

import { useRef } from 'react';
import html2pdf from 'html2pdf.js';

const PdfConverter = () => {
	const contentRef = useRef(null);

	const convertToPdf = () => {
		const content = contentRef.current;

		const options = {
			filename: 'my-document.pdf',
			margin: 1,
			image: { type: 'jpeg', quality: 0.98 },
			html2canvas: { scale: 2 },
			jsPDF: {
				unit: 'in',
				format: 'letter',
				orientation: 'portrait',
			},
		};

		html2pdf().set(options).from(content).save();
	};

	return (
		<div>
			<div ref={contentRef}>
				{/* Your HTML content that you want to convert to PDF */}
				<h1>Hello, PDF!</h1>
				<p>
					This is a simple example of HTML-to-PDF conversion using
					React and html2pdf.
				</p>
			</div>
			<button onClick={convertToPdf}>Convert to PDF</button>
		</div>
	);
};

export default PdfConverter;

Integrating the PDF converter component

To use the PdfConverter component in your React application, import and render it in your main component (src/App.js):

import './App.css';
import PdfConverter from './PdfConverter';

function App() {
	return (
		<div>
			<header>
				<h1>HTML-to-PDF Conversion with React</h1>
			</header>
			<main>
				<PdfConverter />
			</main>
		</div>
	);
}

export default App;

Running your React application

You’re now ready to run your React application:

npm start

Open your browser, and you’ll see your HTML content, along with a Convert to PDF button. Clicking this button will generate a PDF version of the content and open it in a new browser window, and it’ll be ready for download or printing.

Pros and cons of using html2pdf for converting HTML to PDF

As with anything, there are both pros and cons to this conversion solution.

Pros

  • Easy to use — html2pdf is a straightforward library that allows you to convert HTML to PDF using a few lines of JavaScript code.

  • No server-side processing required — Since html2pdf is entirely client-side, you don’t need to rely on any server-side processing or dependencies. This makes it a great option if you don’t have access to server-side resources or you want to reduce server load.

  • Customizable options — html2pdf provides many customization options, such as paper size, page orientation, margins, image quality, and more. This allows you to generate PDFs that meet your specific needs.

  • Open source — html2pdf is an open source library, which means you can use it for free and contribute to its development if you wish.

Cons

  • Limited support for advanced PDF features — html2pdf may not support some advanced PDF features, such as embedded fonts, annotations, or form fields. For more complex PDF generation tasks, a server-side library like wkhtmltopdf or a more powerful client-side library like pdfmake might be more suitable.

  • Performance issues — Since html2pdf relies on html2canvas to take screenshots of webpages and convert them to a canvas, the conversion process can be slow and resource-intensive, especially for large or complex documents.

  • Dependence on external libraries — html2pdf relies on other libraries (html2canvas and jsPDF) for its functionality. While this approach simplifies the code and reduces maintenance, it can also introduce potential compatibility issues or limitations from the underlying libraries.

  • Incomplete support for CSS and JavaScript — html2pdf may not render certain CSS styles or JavaScript-driven content accurately. This limitation can be problematic if your HTML content relies heavily on advanced CSS or dynamic content generated through JavaScript.

Alternatives to html2pdf.js

While html2pdf.js is a popular option for converting HTML to PDF, there are several other libraries and tools available for this purpose. Below are some of the alternatives to consider.

  • wkhtmltopdf — This is a command-line tool that uses the WebKit rendering engine to convert HTML to PDF. It supports advanced features such as headers and footers, tables of contents, and page numbering. However, it requires server-side processing and may not be as customizable as html2pdf.js.

  • Puppeteer — This is a Node.js library that provides a high-level API for controlling headless Chrome or Chromium browsers. It can be used to generate PDFs from HTML content, and it supports advanced features such as page breaks, headers and footers, and tables of contents. However, it requires server-side processing and may not be as lightweight as html2pdf.js.

Nutrient products for HTML-to-PDF conversion

We offer a couple of options for converting HTML to PDF.

HTML-to-PDF API

Our HTML-to-PDF API is a REST API and hosted solution that provides 100 free conversions per month and offers additional packages for a per-document fee. This option allows you to integrate HTML-to-PDF conversion into your applications without the need for self-hosting or a server setup.

Nutrient PDF generation SDK

The Nutrient PDF generation SDK enables developers to create high-quality PDFs from scratch using HTML and CSS. It’s ideal for building custom, context-aware PDF documents across a wide range of platforms.

Key features

  • Programmatic HTML-to-PDF generation using clean, maintainable templates

  • Full support for style sheets, responsive layout, dynamic content, and embedded fonts

  • Native SDKs for seamless integration in mobile and web environments

  • Optimized for use cases like invoices, reports, exportable forms, and styled documents

Platform-specific guides

  • JavaScript (Web)

  • iOS

  • Android

  • React Native

  • .NET (C#)

  • Flutter

Use it when your application demands precision layout, mobile-native control, or production-grade PDF creation.

If you’re interested in Nutrient, contact our Sales team.

Conclusion

Overall, html2pdf.js is a useful client-side library for generating PDFs from HTML content, offering simplicity and customization. However, for advanced features and a more comprehensive solution, consider Nutrient as a robust alternative. Nutrient offers server-side and hosted options, flexible PDF generation, and API integrations.

To get started, you can:

  • Start your free trial to test the library and see how it works in your application.

  • Launch our demo to see the viewer in action.

FAQ

How can I convert HTML to PDF in a React application using html2pdf? Install html2pdf.js, import it into your React component, and use html2pdf().from(element).save() to convert HTML content into a PDF.
What are the steps to install and use html2pdf in a React project?
  1. Install with npm install html2pdf.js.
  2. Import using import html2pdf from 'html2pdf.js';.
  3. Use html2pdf().from(element).save() to convert and download the PDF.
How can I customize the layout and styling of the PDF with html2pdf in React? Style your HTML content with CSS. html2pdf will apply these styles in the generated PDF. You can also configure PDF options like margins and paper size.
What are common uses for converting HTML to PDF in React applications? Typical uses include generating invoices, reports, user manuals, and downloadable content.
What are the benefits of using html2pdf for PDF conversion in React? html2pdf is easy to use, operates client-side, supports various customizations, and is open source.
Author
Hulya Masharipov
Hulya Masharipov Technical Writer

Hulya is a frontend web developer and technical writer at Nutrient who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

Explore related topics

Web JavaScript How To html2pdf React
Free trial Ready to get started?
Free trial

Related articles

Explore more
SDKTUTORIALSWebJavaScriptHow ToPDFRenderingViewing

What is a vector PDF? Understanding the difference between vector, raster, and text elements in PDF documents

SDKTUTORIALSWebJavaScriptHow ToPDFRenderingViewing

The ultimate guide to PDF rendering vs. PDF viewing (and when each is applicable)

SDKTUTORIALSWebPDF.jsJavaScriptNutrient Web SDK

PDF.js vs. Nutrient Web SDK: A comprehensive PDF viewer comparison

Company
About
Security
Team
Careers
We're hiring
Partners
Legal
Products
SDK
Low-Code
Workflow
DWS API
resources
Blog
Events
Customer Stories
Tutorials
News
connect
Contact
LinkedIn
YouTube
Discord
X
Facebook
Popular
Java PDF Library
Tag Text
PDF SDK Viewer
Tag Text
React Native PDF SDK
Tag Text
PDF SDK
Tag Text
iOS PDF Viewer
Tag Text
PDF Viewer SDK/Library
Tag Text
PDF Generation
Tag Text
SDK
Web
Tag Text
Mobile/VR
Tag Text
Server
Tag Text
Use Cases
Tag Text
Industries
Tag Text
Resources
Blog
Tag Text
Events
Customer Stories
Tag Text
Tutorials
Tag Text
Features List
Tag Text
Compare
Tag Text
community
Free Trial
Tag Text
Documentation
Tag Text
Nutrient Portal
Tag Text
Contact Support
Tag Text
Company
About
Tag Text
Security
Tag Text
Careers
Tag Text
Legal
Tag Text
Pricing
Tag Text
Partners
Tag Text
connect
Contact
Tag Text
LinkedIn
Tag Text
YouTube
Tag Text
Discord
Tag Text
X
Tag Text
Facebook
Tag Text
low-code
Document Converter
Tag Text
Document Editor
Tag Text
Document Automation Server
Tag Text
Document Searchability
Tag Text
Use Cases
Tag Text
Industries
Tag Text
Resources
Blog
Tag Text
Events
Customer Stories
Tag Text
Support
Help Center
Tag Text
Contact Support
Tag Text
Log In
Tag Text
Company
About
Tag Text
Careers
Tag Text
Security
Tag Text
Legal
Tag Text
Pricing
Tag Text
Partners
Tag Text
connect
Contact
Tag Text
LinkedIn
Tag Text
YouTube
Tag Text
Discord
Tag Text
X
Tag Text
Facebook
Tag Text
Popular
Approvals matrix
Tag Text
BPMS
Tag Text
Budgeting process
Tag Text
CapEx approval
Tag Text
CapEx automation
Tag Text
Document approval
Tag Text
Task automation
Tag Text
workflow
Overview
Tag Text
Services
Tag Text
Industries
Tag Text
Departments
Tag Text
Resources
Blog
Tag Text
Events
Customer Stories
Tag Text
Support
Help Center
Tag Text
FAQ
Tag Text
Troubleshooting
Tag Text
Contact Support
Tag Text
Company
About
Tag Text
Careers
Tag Text
Security
Tag Text
Legal
Tag Text
Pricing
Tag Text
Partners
Tag Text
connect
Contact
Tag Text
LinkedIn
Tag Text
YouTube
Tag Text
Discord
Tag Text
X
Tag Text
Facebook
Tag Text
DWS api
PDF Generator
Tag Text
Editor
Tag Text
Converter API
Tag Text
Watermark
Tag Text
OCR
Tag Text
Table Extraction
Tag Text
Resources
Log in
Tag Text
Help Center
Tag Text
Support
Tag Text
Blog
Tag Text
Company
About
Tag Text
Careers
Tag Text
Security
Tag Text
Pricing
Tag Text
Legal
Privacy
Tag Text
Terms
Tag Text
connect
Contact
Tag Text
X
Tag Text
YouTube
Tag Text
Discord
Tag Text
LinkedIn
Tag Text
Facebook
Tag Text

Copyright 2025 Nutrient. All rights reserved.

Thank you for subscribing to our newsletter!

We’re thrilled to have you join our community. You’re now one step closer to receiving the latest updates, exclusive content, and special offers directly in your inbox.

This builtin is not currently supported: DOM

PSPDFKit is now Nutrient. We've consolidated our group of trusted companies into one unified brand: Nutrient. Learn more

This builtin is not currently supported: DOM

PSPDFKit is now Nutrient. We've consolidated our group of trusted companies into one unified brand: Nutrient. Learn more

This builtin is not currently supported: DOM

New Feature Release. Tap into revolutionary AI technology to instantly complete tasks, analyze text, and redact key information across your documents. Learn More or View Showcase

This builtin is not currently supported: DOM

Aquaforest and Muhimbi are now Nutrient. We've consolidated our group of trusted companies into one unified brand: Nutrient. Learn more

This builtin is not currently supported: DOM

Integrify is now Nutrient. We've consolidated our group of trusted companies into one unified brand: Nutrient. Learn more

This builtin is not currently supported: DOM

Join us on April 15th. Join industry leaders, product experts, and fellow professionals at our exclusive user conference. Register for conference