Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How am I supposed to use the "pdf" package from typescript

I have installed pdf including types using

npm install --save pdf @types/pdf

Unfortantly I cannot find any documentation on how to use this. If I do:

import {PDFJS} from 'pdf';
PDFJS.getDocument(fileName)

I get:

TyeError: Cannot read 'getDocument' of undefined

So how should I initialize this?

like image 265
Nathan Avatar asked Jan 06 '17 10:01

Nathan


People also ask

What is the use of PDF JS?

The project was created to provide a way for viewing PDF documents natively in the web browser, which prevents potential security risks when opening PDF documents outside a browser, as the code for displaying the document is sandboxed in a browser.

What is Pdfjs Express?

PDF. js Express Plus is a commercial PDF web viewer that wraps around the PDF. js open-source rendering engine. It offers developers a way to quickly add annotation, e-signatures, and form filling to their PDF viewer.


1 Answers

Update:

Since writing this answer, the type definitions have been renamed to match the NPM package, and a proper export definition has been added, so you can ignore all of the below and just npm install --save-dev @types/pdf-dist.


Original Answer:

This is a consequence of bad package naming - the @types/pdf package provides types for Mozilla's PDF.js (which you can obtain via NPM using the pdfjs-dist package), not the long since abandoned pdf.

npm uninstall --save pdf
npm install --save pdfjs-dist

Additionally, it seems like the @types/pdf definitions only defines PDF.js as a global variable, not as an importable module. This is despite the fact that the library definitely supports being imported... Add the following to your project somewhere, it should fix it.

declare module "pdfjs-dist" {
    export = { PDFJS };
}

Then import PDF.js like so:

import { PDFJS } from "pdfjs-dist";
like image 142
Joe Clay Avatar answered Sep 25 '22 03:09

Joe Clay