Pdf.js: Invalid PDF Structure for latest version of PDF.js in Chrome

Created on 6 Apr 2017  路  6Comments  路  Source: mozilla/pdf.js

I'm using the test PDF, shown here, which I have saved locally.

I'm using pdfjs-dist, and the pdf.js version 1.8.172 (or higher).

Configuration:

  • Browser: Chrome 56, I've also tried on FireFox 52 and the same error happens.
  • OS version: OSX 10.12.1
  • PDF.js version: 1.8.172

Steps to reproduce the problem:

const url = "http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf";
// other code involved with React

  componentDidMount() {
    window.PDFJS
      .getDocument('../pdf/compressed.tracemonkey-pldi-09.pdf')
      .then((pdf) => {
        this.renderPdf(pdf);
      })
      .catch((e) => {
        console.log(e);
      });
  }

  renderPdf(pdf) {
    const container = document.getElementById("pageContainer");
    for (let i = 1; i <= pdf.numPages; i++) {

      // Get desired page
      pdf.getPage(i).then(function(page) {

        const scale = 1.5;
        const viewport = page.getViewport(scale);
        const div = document.createElement("div");

        // Set id attribute with page-#{pdf_page_number} format
        div.setAttribute("id", "page-" + (page.pageIndex + 1));

        // This will keep positions of child elements as per our needs
        div.setAttribute("style", "position: relative");

        // Append div within div#container
        container.appendChild(div);

        // Create a new Canvas element
        const canvas = document.createElement("canvas");

        // Append Canvas within div#page-#{pdf_page_number}
        div.appendChild(canvas);

        const context = canvas.getContext('2d');
        canvas.height = viewport.height;
        canvas.width = viewport.width;
        const renderContext = {
          canvasContext: context,
          viewport: viewport
        };
        // Render PDF page
        page.render(renderContext);
      });
  }

What is the expected behavior? The PDF loads

What went wrong? I get a InvalidPDFStructure error.

screen shot 2017-04-06 at 4 19 55 pm

All 6 comments

It also doesn't work for other simple pdfs, for example -->

http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf

Any solution @prakashsanker ?

Same problem here - could use an inch?

@ansmonjol @LucasFyl are you sure you have the same problem?

Looking at screenshot it has errors about hex digits String.fromCharCode(108,110,103,61,34,110,34,104) which is lng="n" that is part of lang="en" and it's not part of the PDF (but some HTML code, possibly server error message)

hum, just tried again and it works... It was because I was loading the wrong file as worker with webpackCopyWebpackPlugin. Fixed now.
This wrong worker was throwing the same issue as @prakashsanker

Oh I was doing something really dumb - I wasn't serving the pdf properly, so I think it was getting parsed some other way.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sujit-baniya picture sujit-baniya  路  3Comments

jigskpatel picture jigskpatel  路  3Comments

brandonros picture brandonros  路  3Comments

liuzhen2008 picture liuzhen2008  路  4Comments

kleins05 picture kleins05  路  3Comments