question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
LucasFylcommented, May 31, 2017

Same problem here - could use an inch?

1reaction
ansmonjolcommented, May 31, 2017

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

PDF.js v1.8.557 (build: 4a74cc41) Messaggio - Google Support
PDF.js v1.8.557 (build: 4a74cc41) Messaggio: Invalid PDF structure. I can't open PDF files.
Read more >
Invalid PDF Structure - PDF Plugins For WordPress
If you see 'Invalid PDF Structure' where you expected the PDF to display, this may be due to server configuration problems. Either the...
Read more >
How to fix Google Chrome pdf file opening error 'PDF JS Click ...
How to fix Google Chrome pdf file not opening. error message ' PDF JS Click on "Allow access o file URLs" at Chrome...
Read more >
Error = Invalid PDF structure, while viewing and downloading ...
I'd like to test and see if you can at least download the PDF files to view them. Open the main menu, navigate...
Read more >
PDF.js v1.1.366 (build: 9e9df56) Bericht: Invalid PDF structure
js v1.1.366 (build: 9e9df56) Bericht: Invalid PDF structure This is the error code. Opening with any other PDF viewer is still possible and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found