Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference pdf.js library in React?

I am trying to reference PDF.js (by Mozilla) into my React project. However, it is throwing 'Unexpected identifier' error.

I have placed the PDF.js in the public folder and referenced it in my index.html.

File structure:

public
  - index.html
  - pdftojs
    - parsejs.js // < parseFile method in this file will be called
    - pdf-parse.js
    - misc..
src
  - pdftotext
    - parsepdf.js // < page to parse PDF

pdf-parse.js

var PDFJS = null
function render_page(pageData) { ... } // Untouched
async function PDF(...) { ... } // Untouched

exports.pdf = PDF; // Changed this line

parsejs.js from the original library:

8    import pdf from 'pdf-parse.js';
9    const pdfjsLib = require('pdfjs-dist'); // 'require' is undefined too so I don't know what is the correct way
10    
11   function parseFile(file) {

... 
45   }

This file throws Unexpected identifier on Line 8

Parse PDF Page (parsepdf.js)

  process(file) {
     parseFile(file); // calling method in parsejs.js
     ...
  }

which gives 'parseFile' is not defined

like image 539
xtotheawn Avatar asked Mar 27 '19 06:03

xtotheawn


4 Answers

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>;
}
like image 111
Arntor Avatar answered Oct 20 '22 16:10

Arntor


This import will clear the undefined issue:

import * as pdfjsLib from "pdfjs-dist/build/pdf";
like image 40
jagadishreddy kovvuri Avatar answered Oct 20 '22 16:10

jagadishreddy kovvuri


checkout these examples from installing Pdf.js from webpack.

pdf.js

Then this is how you reference and bring in the information into your own project.

    import pdfjsLib from 'pdfjs-dist/webpack';
like image 1
Dan Avatar answered Oct 20 '22 15:10

Dan


I couldn't start my app, untill I had installed the npm module of previous version npm i [email protected]

like image 1
user16877035 Avatar answered Oct 20 '22 17:10

user16877035