Home Web Front-end Front-end Q&A What should I do if react does not display PDF generation information?

What should I do if react does not display PDF generation information?

Jan 06, 2023 am 11:47 AM
react pdf

The solution to the problem that react does not display the PDF generation information: 1. Get the url address of the pdf from the background, and then request the pdf file; 2. Set the responseType to blob when requesting the pdf file; 3. Change the PDF Convert the file to base64 format; 4. Use canvas to render the PDF.

What should I do if react does not display PDF generation information?

The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.

What should I do if react does not display PDF generation information?

Solving the problem of PDF display and printing in react projects

There is such a requirement in the recent project:

  • PDF can be displayed on the page

  • If you don’t want to print out the entire page and only print the part that displays the PDF, you can use the browser’s built-in printing function

Display of PDF files

I was really confused when I got this requirement. Since I have never done anything similar, I don’t know where to start. In the process of checking the information, I found that there are many jQuery plug-ins that can display PDF, but we are a react single-page application project. It seems that these plug-ins are not suitable and we can only find other methods.

Later I found the react-pdf-js component on npmjs.com and thought it would be possible to display pdf. I can't wait to install the react-pdf-js dependency into the project through the cnpm install react-pdf-js --save-dev command, and introduce it into the project through import PDF from 'react-pdf-js'. Insert into render.

During the debugging process, it was found that static pdf files can be displayed, but online pdf files cannot. Through the control error message, we know that the react-pdf-js component requires the file address to be in URL or base64 format. Since the URL does not work, we can only rely on base64.

Get the PDF file

At the beginning, I directly converted the online address URL of the PDF to base64, but it could not be displayed. Later I figured out that it is useless to just convert the url to base64 format. You need to convert the pdf file content into base64. Next, it is logical to request the pdf file again through the url address of the pdf obtained from the background.

I encountered a small problem when doing this part: the request was successful, but the pdf file could not be obtained. I struggled with this for a long time and didn’t know how to solve it. I described the problem to our company’s architect. , we analyzed that this was caused by a cross-domain problem, and he configured the nginx server to solve the cross-domain problem.

'Content-Type':  'application/x-www-form-urlencoded;charset=utf-8',
'Access-Control-Allow-Origin': 'https://www.nurse-go.cn:9091',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'X-Custom-Header',
'Access-Control-Allow-Credentials': true,
Copy after login

Convert PDF files to base64 format

base64 can store files in any format, a great storage method. Convert the file to base64 format.

It should be noted here that when requesting a pdf file, the responseType must be set to blob. Why the blob type is used is explained below. At this point, I got the pdf file and converted it into base64 format.

let reader  = new FileReader()
let fileParts = []
fileParts.push(this.props.pdfFile)
let blob = new Blob(fileParts, {type : 'application/pdf'})
if (blob) {
    reader.readAsDataURL(blob)
}
let base64
let that = this      // 处理this的指向  
reader.addEventListener("load", function () {
    base64 = reader.result
   that.setState({
          base64: base64,
 })
}, false);
Copy after login

Conversion of base64 format, if necessary, blob format will be converted into pdf in base64 format, and will be displayed on the browser in file={file}. In fact, it is ultimately a PDF presented in canvas.

The pdf display has come to an end, and the next step is printing.

Printing of PDF files

On the browser, printing is divided into full page printing and specified part printing. The project needs to print part of the content. There are many ways to achieve printing. I used traditional CSS control. Use @media print to hide the parts that do not need to be printed, and then the rest is the part that needs to be printed.

PDF file printing debugging

Here is a little debugging tip: because the style in @media print will be called only when the browser's print is called, so You can put this part of the style outside. When the parts that don't need to be printed are hidden, then remove these external styles and print them to @media.

Call the window.print() used by the browser for printing. Although it is not compatible with all browsers, common advanced browsers are compatible and meet our project needs. I will not continue to dig deeper here. .

The display and printing of pdf have encountered many problems. The above flow is a summary.

Recommended learning: "react video tutorial"

The above is the detailed content of What should I do if react does not display PDF generation information?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to merge PDFs on iPhone How to merge PDFs on iPhone Feb 02, 2024 pm 04:05 PM

When working with multiple documents or multiple pages of the same document, you may want to combine them into a single file to share with others. For easy sharing, Apple allows you to merge multiple PDF files into one file to avoid sending multiple files. In this post, we will help you know all the ways to merge two or more PDFs into one PDF file on iPhone. How to Merge PDFs on iPhone On iOS, you can merge PDF files into one in two ways – using the Files app and the Shortcuts app. Method 1: Using Files App The easiest way to merge two or more PDFs into one file is to use the Files app. Open on iPhone

3 Ways to Get Text from PDF on iPhone 3 Ways to Get Text from PDF on iPhone Mar 16, 2024 pm 09:20 PM

Apple's Live Text feature recognizes text, handwritten notes and numbers in photos or through the Camera app and allows you to paste that information onto any other app. But what to do when you're working with a PDF and want to extract text from it? In this post, we will explain all the ways to extract text from PDF files on iPhone. How to Get Text from PDF File on iPhone [3 Methods] Method 1: Drag Text on PDF The easiest way to extract text from PDF is to copy it, just like on any other app with text . 1. Open the PDF file you want to extract text from, then long press anywhere on the PDF and start dragging the part of the text you want to copy. 2

How to verify signature in PDF How to verify signature in PDF Feb 18, 2024 pm 05:33 PM

We usually receive PDF files from the government or other agencies, some with digital signatures. After verifying the signature, we see the SignatureValid message and a green check mark. If the signature is not verified, the validity is unknown. Verifying signatures is important, let’s see how to do it in PDF. How to Verify Signatures in PDF Verifying signatures in PDF format makes it more trustworthy and the document more likely to be accepted. You can verify signatures in PDF documents in the following ways. Open the PDF in Adobe Reader Right-click the signature and select Show Signature Properties Click the Show Signer Certificate button Add the signature to the Trusted Certificates list from the Trust tab Click Verify Signature to complete the verification Let

How to convert pdg files to pdf How to convert pdg files to pdf Nov 14, 2023 am 10:41 AM

Methods include: 1. Use professional document conversion tools; 2. Use online conversion tools; 3. Use virtual printers.

How to import and annotate PDFs in Apple Notes How to import and annotate PDFs in Apple Notes Oct 13, 2023 am 08:05 AM

In iOS 17 and MacOS Sonoma, Apple added the ability to open and annotate PDFs directly in the Notes app. Read on to find out how it's done. In the latest versions of iOS and macOS, Apple has updated the Notes app to support inline PDFs, which means you can insert PDFs into Notes and then read, annotate, and collaborate on the document. This feature also works with scanned documents and is available on both iPhone and iPad. Annotate a PDF in Notes on iPhone and iPad If you're using an iPhone and want to annotate a PDF in Notes, the first thing to do is select the PDF file

How to build a reliable messaging app with React and RabbitMQ How to build a reliable messaging app with React and RabbitMQ Sep 28, 2023 pm 08:24 PM

How to build a reliable messaging application with React and RabbitMQ Introduction: Modern applications need to support reliable messaging to achieve features such as real-time updates and data synchronization. React is a popular JavaScript library for building user interfaces, while RabbitMQ is a reliable messaging middleware. This article will introduce how to combine React and RabbitMQ to build a reliable messaging application, and provide specific code examples. RabbitMQ overview:

How to export xmind files to pdf files How to export xmind files to pdf files Mar 20, 2024 am 10:30 AM

xmind is a very practical mind mapping software. It is a map form made using people's thinking and inspiration. After we create the xmind file, we usually convert it into a pdf file format to facilitate everyone's dissemination and use. Then How to export xmind files to pdf files? Below are the specific steps for your reference. 1. First, let’s demonstrate how to export the mind map to a PDF document. Select the [File]-[Export] function button. 2. Select [PDF document] in the newly appeared interface and click the [Next] button. 3. Select settings in the export interface: paper size, orientation, resolution and document storage location. After completing the settings, click the [Finish] button. 4. If you click the [Finish] button

Solve the problem of downloading PDF files in PHP7 Solve the problem of downloading PDF files in PHP7 Feb 29, 2024 am 11:12 AM

Solve the problems encountered in downloading PDF files with PHP7 In web development, we often encounter the need to use PHP to download files. Especially downloading PDF files can help users obtain necessary information or files. However, sometimes you will encounter some problems when downloading PDF files in PHP7, such as garbled characters and incomplete downloads. This article will detail how to solve problems you may encounter when downloading PDF files in PHP7 and provide some specific code examples. Problem analysis: In PHP7, due to character encoding and H

See all articles