PC browsers do not have much problem with PDF file display. Given a PDF link, you can display and render the content of the PDF file with the browser’s default display style. For example, an “www.baidu.com/test/pdf”. How to display this file on mobile. In order to display and render PDF files on mobile devices, this article implements a simple React component based on PDFJS to display and render PDF files.

  • Distribute the React component as an NPM package.

  • The project address for this component is: github.com/fortheallli… (To see examples in action, download the code or clone directly, then NPM install and NPM start.)

React-read-pdf

Components written with Act 16.5 for displaying and rendering PDF files on mobile devices and PCS

The characteristics of

  • Simple: The React component is Simple to use
  • Mobile-friendly: adaptive to a variety of Mobile devices, including Mobile phones, tablets, and other Mobile office devices

Browser support

  • IE 10+
  • Firefox 3.6 +
  • Chrome 6+
  • Safari 6+
  • Opera 11.5 +
  • IOS Safari 6.1 +
  • Android Browser 3+

Quick start

1. Incorporate react-read-pdf into your React project

(In your project, for example, introduce React first, and make sure react version is above 15.0.)

Install the react – read – PDF package

npm install --save react-read-pdf
Copy the code

Recommended use of PDFReader on PC:

import React from 'react';
import { PDFReader } from 'react-read-pdf';
Copy the code

It is recommended to use MobilePDFReader on mobile terminals, which can adapt to various mobile devices:

import React from 'react';
import { MobilePDFReader } from 'react-read-pdf';

Copy the code

2. After introduction, let’s look at simple usage:

import { MobilePDFReader } from 'react-read-pdf';
export default class Test extends Component{
  render(){
    return <div style={{overflow:'scroll',height:600}} >
            <MobilePDFReader url="http://localhost:3000/test.pdf"/>
           </div>}}Copy the code
import ReactDOM from 'react-dom';
import Test from './test'
ReactDOM.render(<Test />, document.getElementById('root'));
Copy the code

React-read-pdf ADAPTS to a variety of mobile devices, including phones, tablets, and other mobile office devices. Here is an example of react-read-pdf on the iphoneX.

The document

The react-read-pdf NPM package contains two different types of components: PDFReader and MobilePDFReader.

🌱 PDFReader

import { PDFReader } from 'react-read-pdf'. <PDFReader url={"http://localhost:3000/test.pdf"} ...>
Copy the code

Properties in the PDFReader component

The attribute name type describe
url String or object If it is a string, then the URL represents the absolute or relative address of the PDF file. If it is an object, you can see the description of the object attributes.url object type
data string PDF files described by binary. In javascript, we can convert base64 encoded PDF files into binary encoded files through “ATOB”.
page digital The default value is 1, indicating which page of the PDF file should be rendered
scale digital Determine the viewport size during rendering
width digital Determines the viewport width during rendering
showAllPage Boolean value The default is false, which means that the page specified by the page value will not be rendered at once. If this value is true, all pages of the PDF file are rendered at once
onDocumentComplete function Once the PDF file is loaded, you can use this function to print the PDF file details. The details of this function are shown below.function type

Url **PDFReader** The URL property of the component

Type:

  • String: indicates the absolute or relative address of the PDF file
  • Object: an object that has the following properties

Properties:

The property name type describe
url string The value is a string of absolute or relative addresses of PDF files
withCredentials Boolean value Determines whether the request carries a cookie

OnDocumentComplete **PDFReader** onDocumentComplete property

Type:

  • function(totalPage)

The type of onDocumentComplete is a function whose first argument represents the total number of pages in the PDF file.

Matters needing attention

The URL property of the PDFReader component can be a string or an object.

Both of the following ways are allowed.

The first is:

 <MobilePDFReader url="http://localhost:3000/test.pdf"/>

Copy the code

Another way is:

 <MobilePDFReader url={url:"http://localhost:3000/test.pdf"} / >Copy the code

🌱 MobilePDFReader

import { MobilePDFReader } from 'react-read-pdf'. <MobilePDFReader url={"http://localhost:3000/test.pdf"} ...>
Copy the code

Properties in the MobilePDFReader component

The attribute name type describe
url string If it is a string, the URL represents the absolute or relative address of the PDF file
page digital The default value is 1, indicating which page of the PDF file should be rendered
scale Number or auto The default value is “Auto”, which determines the viewport size during rendering. It is recommended to set “Auto” according to the adaptive scale of mobile devices
minScale digital The default value is 0.25, the minimum scale is desirable
maxScale digital The default value is 10, the maximum that scale can take
isShowHeader Boolean value The default value is true, and for illustration purposes, when true, there is a default header style. Setting it to false removes this default style.
isShowFooter Boolean value The default value is true, and for illustration purposes, when true, there is a default tail style. Setting it to false removes this default style.
onDocumentComplete function Once the PDF file is loaded, you can use this function to print the PDF file details. The details of this function are shown below.function type for details

OnDocumentComplete **MobilePDFReader** onDocumentComplete property

Type: function

  • function(totalPage,title,otherObj)

Function arguments:

The parameter name type describe
totalPage digital Represents the total number of pages in a PDF file
title string The title of the PDF file
otherObj object Additional extension or encoding information for PDF files

Matters needing attention

The default value of scale is “auto”, and it is highly recommended to set the value of scale to “auto” so that it can be adjusted to the size of the mobile device.

Developer options

  • React (16.x)
  • Webpack (4.x)
  • Typescript (3.x)
  • Hot Module Replacement (HMR) using React Hot Loader (4.x)
  • Babel (7.x)
  • Less
  • React-css-modulesusing css-modules
  • Jest – Testing framework for React applications
  • Production build script
  • Image loading/minification using Image Webpack Loader
  • Typescript compiling using Typescript Loader (5.x)
  • Code quality (linting) for Typescript and LESS/CSS.

The installation

  1. Clone/download repo
  2. yarn install (or npm install for npm)

use

Development

yarn run start-dev

  • Build app continuously (HMR enabled)
  • App served @ http://localhost:8080

Production

yarn run start-prod

  • Build app once (HMR disabled)
  • App served @ http://localhost:3000

Instruction list

Command Description
yarn run start-dev Build app continuously (HMR enabled) and serve @ http://localhost:8080
yarn run start-prod Build app once (HMR disabled) and serve @ http://localhost:3000
yarn run build Build app to /dist/
yarn run test Run tests
yarn run lint Run Typescript and SASS linter
yarn run lint:ts Run Typescript linter
yarn run lint:sass Run SASS linter
yarn run start (alias of yarn run start-dev)

Note: replace yarn with npm if you use npm.