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.

React PDF working with the response from a POST

See original GitHub issue

Objective

I’m trying to load a PDF into react-pdf viewer following a POST request to the backend. I’m sending some DOM elements to the backend to render a PDF, this is being sent through the POST body.

Solutions I’ve tried

On the README section the document has a file prop, which has three possible input formats. I’ve tried making the request for the PDF file and passing the response contents into the Document file prop via the React component state. Currently I just receive “Failed to load PDF file.”.

  const print = () => {
    const domElement = document.querySelector('html');
    printDomElements(domElement, setFile);
  }
      <button onClick={print}>Print this page</button>
      {!file ? null : (
        <Document file={file}></Document>
      )}

printDomElements makes an axios request and sets the state through the callback function.

I’ve also considered using the structure below, but it looks like this is probably for a GET request only (maybe I’m wrong):

{ url: 'http://example.com/sample.pdf', httpHeaders: { 'X-CustomHeader': '40359820958024350238508234' }, withCredentials: true }

Any ideas how I can pipe the response of the POST request into React PDF?

Environment

  • Chrome 80 “react-pdf”: “^4.1.0”, “react”: “16.8.6”,

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Kasunthilinacommented, Aug 15, 2020

@SailleshPawar when you do a post request to get data. you should pass bellow parameter in header section responseType: ‘arraybuffer’, headers: { ‘Content-Type’: ‘application/json’, Accept: ‘application/pdf’, }

1reaction
sbitprozcommented, Mar 27, 2020

Hi @SailleshPawar i was by doing the following:

 <Document
            file={`data:application/pdf;base64,${fileBase64}`}
            onLoadSuccess={onDocumentLoadSuccess}
          >
          <Page pageNumber={documentPages.pageNumber} />
</Document>

The key being data:application/pdf;base64, - base64 sufficed for me.

I think the documentation needs to be updated address the base64 format.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to view pdf response from post request in react
How to view pdf response from post request in react · 1. In your request header you are accepting 'application/json'. Update it with...
Read more >
Generating Pdf documents in React Using React-pdf
So in the tutorial/ blog post, I will try to explain briefly how react-pdf works and also walk you through how to generate...
Read more >
How to show a PDF Stream on a React Client without any library
Here's the situation: i've a PDF Stream, coming from a PHP Backend, and i've to show it up on a new window called...
Read more >
Thinking in React
React is, in our opinion, the premier way to build big, fast Web apps with JavaScript. It has scaled very well for us...
Read more >
Display WordPress Posts as PDF | React.js - YouTube
In this video we will display WordPress posts as PDF files using React.js.
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