15

We have the pdf document save in data base , from webapi I am returning as byte array,Now from UI I have to open the pdf in new tab of browser, and user should able to download the pdf. How can I achieve this requirement of opening and downloading the pdf document?

Gorakh Nath
  • 9,140
  • 15
  • 46
  • 68
  • Have a look at this post: http://stackoverflow.com/questions/41504668/how-to-open-a-pdf-in-reactjs has some answers, seems answer may lie in the React module react-pdf or react-pdfjs – Jayce444 Jan 10 '17 at 05:52
  • @Jayce444 I did the same but, problem is that we can't download opened pdf. If you will right click then it will save as the image.But my requirement is to download the opened pdf. – Gorakh Nath Jan 10 '17 at 05:56
  • @Jayce444 do you have any better solution for this? – Gorakh Nath Jan 10 '17 at 07:33

5 Answers5

20

The following code work for me

try {
    await axios
      .get(uri.ApiBaseUrl + "AccountReport/GetTrialBalancePdfReport", {
        responseType: "blob",
        params: {
          ...searchObject,
        },
      })
      .then((response) => {
        //Create a Blob from the PDF Stream
        const file = new Blob([response.data], { type: "application/pdf" });
        //Build a URL from the file
        const fileURL = URL.createObjectURL(file);
        //Open the URL on new Window
         const pdfWindow = window.open();
         pdfWindow.location.href = fileURL;            
      })
      .catch((error) => {
        console.log(error);
      });
  } catch (error) {
    return { error };
  }
Md. Nazrul Islam
  • 2,809
  • 26
  • 31
15

You can use something like this:

<a href='/api/v1/print/example.pdf' target='_blank' rel='noopener noreferrer'>
Saurish Kar
  • 576
  • 7
  • 13
Max Alekseenko
  • 346
  • 2
  • 7
  • 1
    I tried window.open(this.state.path, "_blank"); but its not identifying the path while the path is correct : Cannot GET /Decoding.pdf and in browser Failed to load resource: the server responded with a status of 404 (Not Found) error is coming – Gorakh Nath Jan 10 '17 at 10:26
  • @jack Can you write what does the path contain? – Max Alekseenko Jan 10 '17 at 16:40
13

Solution from 2020

   import Pdf from '../Documents/Document.pdf';

   <a href={Pdf} without rel="noopener noreferrer" target="_blank">
      <button trailingIcon="picture_as_pdf" label="Resume">
        PDF
      </button>
   </a>
Qui-Gon Jinn
  • 3,722
  • 3
  • 25
  • 32
0

For some reason, React doesn't allow you to directly reference the path in the href tag, so you need to import it at the top of your file.

import PDF from "file.pdf"

Then use it in your href ->

<a href={PDF} without rel='noreferrer' target='_blank'>
    PDF here
</a>
0

Piggybacking off of Qui-Gon Jinn's answer, I also had the same issue mentioned by Pardeep Jain.

I managed to avoid the random name generation at the end of the file name by placing my PDF inside my public/assets folder (shown below).

pdf placement in file structure

Then, to actually display the PDF, I slightly altered the code.

<a href={`${process.env.PUBLIC_URL}/assets/abc.pdf`} rel="noopener noreferrer" target="_blank">
   PDF 
</a>

The use of an .env file is necessary as React does not allow the import of anything outside of the src folder.

andrewJames
  • 19,570
  • 8
  • 19
  • 51
Raquel
  • 33
  • 1
  • 7