InvalidPDFException {name: "InvalidPDFException", message: "Invalid PDF structure"

See original GitHub issue

I am using 4.0.0-beta.5 but still I am getting InvalidPDFException for this file InvalidPDFException {name: "InvalidPDFException", message: "Invalid PDF structure"

My Component:

import React from "react"
import { Document, pdfjs } from "react-pdf"
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${ pdfjs.version }/pdf.worker.js`

class Sample extends React.Component {
  render(){
     return(
        <div>
          <Document file="/Users/laptop/Desktop/sample.pdf" />
        </div>
     )
  }
}

Also, there are lot of warning on console

_Originally posted by @Puspendert in https://github.com/wojtekmaj/react-pdf/issues/291#issuecomment-438381865_

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
Gui-Alucardcommented, May 21, 2021

Guys, react-pdf accepts only http protocols. So, if you are testing any pdf on your machine, the browser URL will likely be “file: //home/myComputer/localOfFiles/myPdf.pdf” and the correct path should be “http://localhost:3000/myPdf.pdf”.

You don’t need to use any extension or anything else, you just move a file.pdf to your public folder in React code and try this path “http://localhost:3000/myPdf.pdf” to see if your browser will render something.

Here is some sample code:

// ...previous code
    const local = `http://localhost:3000/original.pdf`

              <Document
                  file={local}
                  // file={server}
                  onLoadSuccess={this.onDocumentLoadSuccess}
                  options={{ workerSrc: `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js` }}
                >
                  <Page pageNumber={pageNumber} width={600} />
                </Document>

// ...rest of code
4reactions
bottxiangcommented, Jul 24, 2021

Try this import file from './x.pdf';

import React, { useState } from 'react';
import { Document, Page } from 'react-pdf';
import file from './x.pdf';
export default function MyApp() {
  const [numPages, setNumPages] = useState(null);
  const [pageNumber, setPageNumber] = useState(1);

  function onDocumentLoadSuccess({ numPages }) {
    setNumPages(numPages);
  }

  return (
    <div>
      <Document
        file={file}
        onLoadSuccess={onDocumentLoadSuccess}
      >
        <Page pageNumber={pageNumber} />
      </Document>
      <p>Page {pageNumber} of {numPages}</p>
    </div>
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - react-pdf-js - Uncaught (in promise) e {name ...
react-pdf-js - Uncaught (in promise) e {name: "InvalidPDFException", message: "Invalid PDF structure"} · Subscribe to RSS.
Read more >
Hi, I am trying to upload a pdf file but this message appears ...
3D FlipBook - Hi, I am trying to upload a pdf file but this message ... emessage: “Invalid PDF structure”name: “InvalidPDFException”proto: Error at...
Read more >
PDF Invalid Structure - Power Platform Community - Microsoft
Solved: Hello everyone, With help of Abm user on this link, I cretaed a Flow that takes all PDF in a librairy and...
Read more >
Error = Invalid PDF structure, while viewing and downloading ...
I noticed a friend had an older version of Brave where a list of extensions were natively proposed. Here is what I have...
Read more >
TB and Gmail: performance problem? - Topicbox
... resource://pdf.js/web/viewer.js:1184 14:48:09.212 Uncaught (in promise) Object { message: "Invalid PDF structure.", name: "InvalidPDFException", stack: ...
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