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.

Is there a way to load a PDF from base64 data?

See original GitHub issue

Loading a PDF from react-pdf worked this way, could anyone help me out with this?

I’m getting a PDFDocument: stream must have data error whenever I try to load my PDF’s

<PDF
          file={`data:application/pdf;base64,${this.state.base64}`}
          onDocumentComplete={this.onDocumentComplete}
          onPageComplete={this.onPageComplete}
          page={this.state.page}
/>

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
biyunwucommented, Oct 7, 2018

Loading a PDF from react-pdf worked this way, could anyone help me out with this?

I’m getting a PDFDocument: stream must have data error whenever I try to load my PDF’s

<PDF
          file={`data:application/pdf;base64,${this.state.base64}`}
          onDocumentComplete={this.onDocumentComplete}
          onPageComplete={this.onPageComplete}
          page={this.state.page}
/>

Basically, react-pdf-js supports base 64 string. Different from react-pdf, it is not required to separate the data type data:application/pdf;base64 from the date string in this library. Here is an example, and it works to me.

// data.js
const pdf = `data:application/pdf;base64,JVBERi0xLjYNJeLjz9MNCjEyNCAw...`
export const getData = () => JSON.stringify(pdf)

// PdfViewer.jsx
import * as Data from '../data/pdf'
export default class PdfViewer extends React.Component {
...
render() {
        return (
        <div>
            <PDF
                file={JSON.parse(Data.getData())}
                onDocumentComplete={this.onDocumentComplete}
                page={this.state.page}
            />
            {pagination}
        </div>
        )
    }
}
0reactions
mcviswanadhacommented, Dec 1, 2021

this approach is not working in iphone safari

Read more comments on GitHub >

github_iconTop Results From Across the Web

Opening PDF String in new window with javascript
If you are able to pass the content from the backend encoded you can use.. window. open("data:application/pdf;base64, " + base64EncodedPDF);
Read more >
How to load the PDF document as base64 string in PDF Viewer
You can load the PDF document as base64 string in the PDF Viewer using the documentPath API during the control initialization. The name,...
Read more >
Opening a document in WebViewer from base64 data - PDFTron
If you have the file data as a base64 string, the best way to load the document in WebViewer is to first convert...
Read more >
Downloading a base 64 PDF from an api request in Javascript.
It creates an anchor tag element assigned to the constant downloadLink , uses the base64 pdf along with “data:application/pdf;base64” as a ...
Read more >
How to display ("data:application/pdf;base64," + pdfData) in ...
Just for the record: pdf.js is able to display PDF files encoded as base64 strings. Maybe the problem of @kakaluote444 is that the...
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