Adding these lines seems to fix the code for me:
const bytes = new Uint8Array( pdfBytes );
const blob = new Blob( [ bytes ], { type: "application/pdf" } );
const docUrl = URL.createObjectURL( blob );
setPdfInfo( docUrl );
Or use it directly:
return <iframe src={docUrl} type="application/pdf" />
Answer from Saed Jaber on Stack Overflow Top answer 1 of 4
9
Adding these lines seems to fix the code for me:
const bytes = new Uint8Array( pdfBytes );
const blob = new Blob( [ bytes ], { type: "application/pdf" } );
const docUrl = URL.createObjectURL( blob );
setPdfInfo( docUrl );
Or use it directly:
return <iframe src={docUrl} type="application/pdf" />
2 of 4
1
What I did and worked: I added a reference to the iframe instance:
import React, { useState, useEffect, useRef } from 'react';
const LoadPDF = () => {
...
const viewer = useRef(null);
const modifyPDF = async () => {
...
};
return (
<>{<iframe title="test-frame" src={pdfInfo} ref={viewer} type="application/pdf" />}</>
);
}
PDF-LIB
pdf-lib.js.org
PDF-LIB · Create and modify PDF documents in any JavaScript environment.
Works in any JavaScript runtime, including browsers, Node, Deno, and even React Native. Add, insert, and remove pages. Split a single PDF into separate ones. Or merge multiple PDFs into a single document.
Videos
11:14
Build a React.js Canvas PDF Editor & Renderer in Browser Using ...
11:58
Build a React.js PDF-LIB PDF Viewer & Editor to Add Watermark Using ...
03:44
Build a React.js jsPDF & PDF-LIB PDF Editor & Renderer With Live ...
React.js PDF-LIB Project to Export Multiple Images to PDF ...
03:20
Build a React.js PDF Renderer & Editor in Browser Using PDF-LIB ...
03:40
React.js PDF-LIB PDF Editor Project to Merge Multiple PDF ...
React-pdf
react-pdf.org
React-pdf
React renderer for creating PDF files on the browser and server
Reddit
reddit.com › r/reactjs › best library for showing a pdf in react
r/reactjs on Reddit: Best library for showing a pdf in react
December 2, 2022 -
Hey, I wanted to find out a library in react which can display a pdf from a url. So far I have tried react-pdf from wojtekmaj, it works just fine but I wanted a free library which can provide toolbars also. Let me know if you know any library or a way to build it.
Thanks
Top answer 1 of 5
38
If you don't need direct control over the PDF in any way, then just use an IFrame. It'll make the browser use their default viewer, which is generally a pretty decent one. There's also one made by Mozilla ( PDF.js ), but any PDF viewer is going to be pretty heavy in terms of code size.
2 of 5
6
SimplePDF embed for React will allow you to not only display PDFs but also edit them: Add text, pictures, signatures Add, remove, rotate pages Disclosure: I’m the developer behind SimplePDF
npm
npmjs.com › package › react-pdf
react-pdf - npm
Display PDFs in your React app as easily as if they were images.. Latest version: 10.2.0, last published: 2 months ago. Start using react-pdf in your project by running `npm i react-pdf`. There are 986 other projects in the npm registry using ...
» npm install react-pdf
Published Oct 09, 2025
Version 10.2.0
Author Wojciech Maj
Repository https://github.com/wojtekmaj/react-pdf
npm
npmjs.com › package › pdf-lib
pdf-lib - npm
Designed to work in any modern JavaScript runtime. Tested in Node, Browser, Deno, and React Native environments. ... pdf-lib was created to address the JavaScript ecosystem's lack of robust support for PDF manipulation (especially for PDF ...
» npm install pdf-lib
Published Nov 06, 2021
Version 1.17.1
Author Andrew Dillon
Repository https://github.com/Hopding/pdf-lib
Homepage https://pdf-lib.js.org/
GitHub
github.com › Hopding › pdf-lib
GitHub - Hopding/pdf-lib: Create and modify PDF documents in any JavaScript environment
Designed to work in any modern JavaScript runtime. Tested in Node, Browser, Deno, and React Native environments. ... pdf-lib was created to address the JavaScript ecosystem's lack of robust support for PDF manipulation (especially for PDF ...
Starred by 8.1K users
Forked by 827 users
Languages TypeScript 80.9% | HTML 9.9% | JavaScript 8.5% | Objective-C 0.3% | CSS 0.2% | Starlark 0.1% | Java 0.1%
npm
npmjs.com › package › @react-pdf › pdfkit
react-pdf/pdfkit
A JavaScript PDF generation library for Node and the browser. This project is a fork of pdfkit by @devongovett and continued under the scope of this project since it has react-pdf specific features.
» npm install @react-pdf/pdfkit
Published Sep 23, 2025
Version 4.0.4
Author Devon Govett
Repository https://github.com/diegomura/react-pdf
Top answer 1 of 5
25
I spent too much time today, piecing together fragments of other answers to this question. So here is a complete answer.
First you install pdfjs-dist:
npm install pdfjs-dist
And here is how to use it in an actual viewer component:
import React, { useEffect, useState, useRef, useCallback } from 'react';
import pdfjsLib from "pdfjs-dist/build/pdf";
import pdfjsWorker from "pdfjs-dist/build/pdf.worker.entry";
export default function PdfViewer({url}){
const canvasRef = useRef();
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker;
const [pdfRef, setPdfRef] = useState();
const [currentPage, setCurrentPage] = useState(1);
const renderPage = useCallback((pageNum, pdf=pdfRef) => {
pdf && pdf.getPage(pageNum).then(function(page) {
const viewport = page.getViewport({scale: 1.5});
const canvas = canvasRef.current;
canvas.height = viewport.height;
canvas.width = viewport.width;
const renderContext = {
canvasContext: canvas.getContext('2d'),
viewport: viewport
};
page.render(renderContext);
});
}, [pdfRef]);
useEffect(() => {
renderPage(currentPage, pdfRef);
}, [pdfRef, currentPage, renderPage]);
useEffect(() => {
const loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(loadedPdf => {
setPdfRef(loadedPdf);
}, function (reason) {
console.error(reason);
});
}, [url]);
const nextPage = () => pdfRef && currentPage < pdfRef.numPages && setCurrentPage(currentPage + 1);
const prevPage = () => currentPage > 1 && setCurrentPage(currentPage - 1);
return <canvas ref={canvasRef}></canvas>;
}
2 of 5
9
This import will clear the undefined issue:
import * as pdfjsLib from "pdfjs-dist/build/pdf";
npm
npmjs.com › package › react-native-pdf-lib
react-native-pdf-lib - npm
This library would not be possible without the following projects: https://github.com/galkahana/PDF-Writer is used for handling PDFs on iOS. (The static binaries for v3.6 are built with hummus-ios-build). https://github.com/TomRoush/PdfBox-Android is used for handling PDFs on Android. Create PDFs from HTML: https://github.com/christopherdro/react-native-html-to-pdf
» npm install react-native-pdf-lib
Published Feb 09, 2020
Version 1.0.0
Author Andrew Dillon
GitHub
github.com › Hopding › react-native-pdf-lib
GitHub - Hopding/react-native-pdf-lib: Library for creating and editing PDFs in React Native.
If you're using React Native <= 59, you want to use v0.2.1 of this library. See here for manual installation instructions (manual installation should not be necessary). ... // Import from 'react-native-pdf-lib' import PDFLib, { PDFDocument, PDFPage } from 'react-native-pdf-lib'; // Create a PDF page with text and rectangles const page1 = PDFPage .create() .setMediaBox(200, 200) .drawText('You can add text and rectangles to the PDF!', { x: 5, y: 235, color: '#007386', }) .drawRectangle({ x: 25, y: 25, width: 150, height: 150, color: '#FF99CC', }) .drawRectangle({ x: 75, y: 75, width: 50, height: 50, color: '#99FFCC', }); // Create a PDF page with text and images const jpgPath = // Path to a JPG image on the file system...
Starred by 200 users
Forked by 78 users
Languages C 75.5% | C++ 22.6% | Objective-C++ 0.9% | Java 0.7% | JavaScript 0.3% | Ruby 0.0%
React PDF Viewer
react-pdf-viewer.dev
A React component to view PDF documents - React PDF Viewer
import '@react-pdf-viewer/core/lib/styles/index.css'; import '@react-pdf-viewer/default-layout/lib/styles/index.css'; // Create new plugin instance · const defaultLayoutPluginInstance = defaultLayoutPlugin(); <Viewer · fileUrl='/assets/pdf-open-parameters.pdf' plugins={[ // Register plugins ·
GitHub
github.com › Shogobg › rn-pdf-lib
GitHub - Shogobg/rn-pdf-lib: Library for creating and editing PDFs in React Native.
If you see any issue and you're using an old React Native version, plase upgrade and try again. This library supports Android devices >= API 31, and iOS devices >= iOS 8.0. Latest version that supported Android API 18 was 1.0.3 · See here for manual installation instructions (manual installation should not be necessary). ... import PDFLib, { PDFDocument, PDFPage } from '@shogobg/react-native-pdf'; // Create a PDF page with text and rectangles const page1 = PDFPage .create() .setMediaBox(200, 200) .drawText('You can add text and rectangles to the PDF!', { x: 5, y: 235, color: '#007386', }) .drawRectangle({ x: 25, y: 25, width: 150, height: 150, color: '#FF99CC', }) .drawRectangle({ x: 75, y: 75, width: 50, height: 50, color: '#99FFCC', }); // Create a PDF page with text and images const jpgPath = // Path to a JPG image on the file system...
Starred by 10 users
Forked by 2 users
Languages C 75.4% | C++ 22.6% | Objective-C++ 0.9% | Java 0.8% | JavaScript 0.3% | Ruby 0.0%
CloudDefense.ai
clouddefense.ai › code › javascript › example › pdf-lib
Top 10 Examples of pdf-lib code in Javascript
Since this is a Node script, we can just // save the `pdfBytes` to the file system, where it can be opened with a PDF // reader. const filePath = `${__dirname}/modified.pdf`; fs.writeFileSync(filePath, pdfBytes); console.log(`PDF file written to: ${filePath}`); ... // JavaScript runtime (e.g. ...
GitHub
github.com › diegomura › react-pdf
GitHub - diegomura/react-pdf: 📄 Create PDF files using React
Starred by 16.2K users
Forked by 1.3K users
Languages TypeScript 83.1% | JavaScript 16.8%