How can I get TypeScript to load PDF.js NPM modules and @types bindings in a way that generates working Node.JS code?

Context

I am trying to import PDF.JS into a TypeScript project. I use the bindings > Definitely pdfjs-dist for pdfjs-dist installed via npm install @types/pdfjs-dist and npm install pdfjs-dist .

Problem

It seems like I am unable to TypeScript to compile my project. I use the source code copied directly from the tests for DefinitelyTyped. This is a simplified (only for removal) code that I am trying to compile (an exact copy of the test code from DefinitelyTyped also fails in the same way):

 import { PDFJSStatic } from 'pdfjs-dist'; var PDFJS: PDFJSStatic; PDFJS.getDocument('helloworld.pdf').then(console.log); 

TypeScript finds the type declaration module and considers that the PDFJSStatic import PDFJSStatic valid. He does not think that PDFJS ever initialized, but if I disable strict in tsconfig , the code compiles, but it compiles to:

 "use strict"; exports.__esModule = true; var PDFJS; PDFJS.getDocument('helloworld.pdf').then(console.log); 

Which is clearly not working. It does not compile the import statement into anything.

Question

How to import PDF.JS into a TypeScript project and compile it into a working Node.JS code through the declaration files in @types/pdfjs-dist ?

What i tried

I tried different import options, but to no avail. Switching to require also does not help.

I verified that the pdjs-dist and @types/pdfjs-dist dependencies are present, updated, and can be used directly from NodeJS (not TypeScript) programs.

I tried various values ​​for module in my tsconfig. Sometimes they change the generated code, but not one of them changes it to contain the necessary import.

I tried adding /// <reference path="../node_modules/@types/pdfjs-dist/index.d.ts" /> above the import line. This has not changed behavior.

Environment

tsc version 2.4.2, node 8.5 and npm 5.3. I have the following tsconfig.json in my project root:

 { "compilerOptions": { "allowJs":true, "rootDir": ".", "outDir": "dist", "moduleResolution": "node" }, "include": [ "src/**/*" ], "exclude": [ "**/*.spec.ts", "dist/**/*" ] } 
+6
source share
5 answers

Perhaps you can use the require function.

Add @types/node packages and write require('pdfjs-dist') at the top of the source code. This way you can change your code as shown below.

Now this code will work.

 import { PDFJSStatic } from 'pdfjs-dist'; const PDFJS: PDFJSStatic = require('pdfjs-dist'); PDFJS.getDocument('helloworld.pdf').then(console.log); 

I think @types/pdfjs-dist has problems in its implementation.

+7
source

I also tried different ways. For me, the most readable one was:

 import * as pdfjslib from 'pdfjs-dist'; let PDFJS = pdfjslib.PDFJS; PDFJS.disableTextLayer = true; PDFJS.disableWorker = true; 
+1
source

This worked for me, without requiring require :

 import * as pdfjslib from "pdfjs-dist"; const PDFJS = (<any>pdfjslib) as PDFJSStatic; 

You just have to silence type errors by casting the module to any .

The types in @ types / pdfjs-dist are definitely incorrect, but this should be a quick workaround. :)

0
source

I just want \ in the path t \ node_modules @types \ pdfjs-dist \ index.d.ts in the reference library example: ///

scroll for //

0
source

With @types/pdfjs-dist 2.1.0 and pdfjs-dist "2.1.266", the fundamental problem is still not completely fixed, but I found that the approach from their own test works:

 import { getDocument, PDFDocumentProxy, PDFPromise, Util } from 'pdfjs-dist'; 

(This is not beautiful, though, since it pollutes your namespace.)

0
source

Source: https://habr.com/ru/post/1272608/


All Articles