Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import pdf.js with typescript #10547

Closed
cesco69 opened this issue Feb 12, 2019 · 10 comments
Closed

Import pdf.js with typescript #10547

cesco69 opened this issue Feb 12, 2019 · 10 comments

Comments

@cesco69
Copy link

cesco69 commented Feb 12, 2019

Attach (recommended) or Link to PDF file here:

Configuration:

  • Web browser and its version: NODE 10
  • Operating system and its version: LINUX/DOCKER
  • PDF.js version: pdfjs-dist 2.2.2
  • Is a browser extension: NO

Steps to reproduce the problem:

  1. Install pdfjs-dist with: npm install pdfjs-dist --save
  2. Install @types/pdfjs-dist with: npm install @types/pdfjs-dist --save-dev
  3. Import pdfjs-dist in a *.ts file: import { PDFJS } from 'pdfjs-dist';
  4. Use getDocument from PDFJS : PDFJS.getDocument('my.pdf')
  5. execute the code and see the error: PDFJS is not defined

What is the expected behavior?
PDFJS works on typescript

What went wrong?
PDFJS can't export correctly the lib on typescript

Link to a viewer:
not disponible

Work around:
Import PDFJS in this way

import { PDFJSStatic } from 'pdfjs-dist'; 
const PDFJS: PDFJSStatic = require('pdfjs-dist');
@Snuffleupagus
Copy link
Collaborator

2\. Install **@types/pdfjs-dist** with: `npm install @types/pdfjs-dist --save-dev`

Most likely, those types haven't been updated to handle PDF.js versions above 2.0.
However please note that that's a different library, which is not maintained here, hence this issue is unfortunately out-of-scope/invalid here.

@MickL
Copy link

MickL commented May 20, 2019

@Snuffleupagus @timvandermeij Could you please reopen? @types/pdfjs-dist is totally off for version >= 2 and the Github repository does not have any issue tracker.

@Snuffleupagus
Copy link
Collaborator

Install @types/pdfjs-dist with: npm install @types/pdfjs-dist --save-dev

As mentioned in #10547 (comment), those are not in any way affiliated with Mozilla or the PDF.js project.

@types/pdfjs-dist

Again this is not related to https://github.com/mozilla/pdfjs-dist, but most likely you're actually referring to these ones: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/pdfjs-dist

@MickL
Copy link

MickL commented May 20, 2019

@types/pdfjs-dist should be maintained by mozilla/pdfjs-dist and/or mozilla/pdf.js contributors.

Only this contributors know the code and the functions and can write the required interfaces.

@timvandermeij
Copy link
Contributor

timvandermeij commented May 20, 2019

There are already open issues and even PRs for this, but so far the real demand for this is low and the PRs have not really been updated. If you think this is in fact important, please consider updating the existing PRs (such as #10575 which seems like a good approach since it attempts to generate it from the code itself) so they can be brought in a mergeable state.

@Bluejanis
Copy link

Does pdfjs provide it's own Typescript types somewhere else?

@blasco
Copy link

blasco commented Aug 3, 2020

why is this close? Where are the types provided and how can we include the library into a typescript project? Thank you

@timvandermeij
Copy link
Contributor

Duplicate of #7909 and that was fixed just yesterday.

@nismojkee
Copy link

@timvandermeij not duplicated. #7909 about other troubles. But here asked about instructions to include library into a typescript project.

@severinbeauvais
Copy link

severinbeauvais commented May 1, 2023

The following works.

  • pdfjs-dist v3.5.141
  • typescript v3.9.10
import * as pdfjsLib from 'pdfjs-dist/webpack'

...

    // in this example, data is a Uint8Array
    const pdf = await pdfjsLib.getDocument({ data }).promise
    for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
      const page = await pdf.getPage(pageNum)
      ...

Also it doesn't hurt to have a file, "pdfjs-dist.d.ts":

declare module 'pdfjs-dist/webpack'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants