1

I am sending a pdf file from express server like this -

  app.get("/pdf", (req, res) => {
    var file = fs.createReadStream('./public/file.pdf');
    res.contentType("application/pdf");
    res.send(file);
   })

I want to display it on react side (Note- display pdf , not download) I tried converting it into blob but it shows blank pdf

I am calling below function on click in react end

 viewHandler = async () => {
        axios.get('http://localhost:4000/pdf')
            .then(response => {
                console.log(response.data);

                //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
                console.log("Asdasd");

                window.open(fileURL);
            })
            .catch(error => {
                console.log(error);
            });
    };

I want to display pdf without using static url to server pdf file like -

 <object data='http://localhost:4000/file.pdf'
          type='application/pdf'
          width='100%'
          height='700px' />
Akash Salunkhe
  • 2,698
  • 2
  • 16
  • 31
  • 1
    Possible duplicate of [Display PDF within web browser](https://stackoverflow.com/questions/4853898/display-pdf-within-web-browser) – Dmitry Jan 15 '19 at 11:23

0 Answers0