If we use the fetch method then it will open a new window for displaying the PDF file and let users download the PDF. But if you don’t want that then there is a way to do so. You can use the package called react-pdf, by using this package you can render the PDF in your React app by using the PDF URL.
- Your project needs to use React 16.3 or later.
- Basic knowledge of packages
React-pdf: It lets you display PDF in your React app as easily as if they were images. It helps to create custom components that you can use to create and structure your PDF documents.
Step 1: Create React App
npx create-react-app appname
Step 2: Install react-pdf package.
npm install react-pdf
Step 3: First make a separate component PDF (name of the component, can be anything) and render the PDF component in App.js.
After creating a pdf component your project directory will look like this.
Step 4: In this section, we load the PDF and render it on your app.
Document: Loads a document passed using file prop.
File Prop: It tells what PDF should be displayed, In the above code, we pass URL to it.
URL: The URL consists of two parts here.
- The 1st part is due to preventing cors error, you may refer docs to read more about the core.
1st part: https://cors-anywhere.herokuapp.com/
- The 2nd part is our actual URL of PDF.
2nd part: http://www.pdf995.com/samples/pdf.pdf
One more thing we need to do is ENABLE PDF.JS WORKER, you could use pdf.worker.js from an external CDN.
onDocumentLoadSuccess: When the document gets successfully loaded we set the state of page number to tells on which page number of pdf the user is.
Pdf.js: Now open the PDF component.
Step 5: Now the last thing add NEXT and PREVIOUS buttons to PDF file.
Pdf.js: Here we added two buttons NEXT AND PREVIOUS and their functions named previousPage() and nextPage() which change the state of the current page.
- What is the difference between React Native and React?
- Android & iOS App development using React Native with Expo
- How to Create Button in React-Native App ?
- How to deploy React app to Heroku?
- How React Native Make Mobile App Development Simpler?
- Deploying Your Web App using Azure App Service
- How to convert a PDF document to a preview image in PHP?
- What is the difference between display: inline and display: inline-block in CSS?
- Introduction to Xamarin | A Software for Mobile App Development and App Creation
- Progressive Web App - A Combination of Native and Web App
- Display a Resized and Cropped Image using CSS
- How to Upload Image into Database and Display it using PHP ?
- How to open a PDF files in web browser using PHP?
- How to make PDF file downloadable in HTML link using PHP ?
- How to get the number of pages in a PDF document using PHP ?
- PHP | FPDF-PDF Generator
- Generate PDF in ElectronJS
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.