简体   繁体   中英

Rendering pdf with pdf js

Im trying to render pdf using pdfjs library but I got stuck: Im using it in an IONIC app. Below is my code :

import { PDFJS } from "pdfjs-dist";

renderPDF(url, canvasContainer) {


var options = options || { scale: 1 };

function renderPage(page) {
  var viewport = page.getViewport(options.scale);
  var canvas = document.createElement('canvas');
  var ctx = canvas.getContext('2d');
  var renderContext = {
    canvasContext: ctx,
    viewport: viewport
  };

  canvas.height = viewport.height;
  canvas.width = viewport.width;
  canvasContainer.appendChild(canvas);

  page.render(renderContext);
}

function renderPages(pdfDoc) {
  for(var num = 1; num <= pdfDoc.numPages; num++)
    pdfDoc.getPage(num).then(renderPage);
}
console.log(PDFJS)
PDFJS.disableWorker = true;
PDFJS.getDocument(url).then(renderPages);

}

I am calling it in NgOnInit, the problem Im having is with PDFJS field, as it says Cannot set property 'disableWorker' of undefined at DrawingPage.webpackJsonp.155.DrawingPage.renderPDF. Im thinking its something with import but can get it done right..

Doing your import like the way you're doing it means that the file you're importing must have an export called PDFJS , which it might not. To import the whole file and place whatever is exported into a variable, you can use this import format: import * as PDFJS from 'pdfjs-dist';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM