Yoset GA
Yoset GA

Reputation: 39

Open PDF file with React

Hello there im trying to open a PDF file that is saved outside of src, i only wanna open the pdf in another windows to get a prev. Im trying sending a post request to the backend with the path of pdf, i recive a pdf string, is any way to open?

This is my route:

app.post('/open',(req,res)=>{
  var data =fs.readFileSync(req.body.path);
  res.contentType("application/pdf");
  res.send(data);
});

and this is my method from react

const data = {
            path: './files/BENH970204HJCRVM01_ac.pdf'
        }
        axios
            .post('http://localhost:8000/open',data)
            .then(res=>{
                console.log("ok");
            }).catch(err=>console.log(err.response.data));

Upvotes: 0

Views: 1509

Answers (2)

geeky01adarsh
geeky01adarsh

Reputation: 400

You might use react-pdf for rendering a pdf or @react-pdf/renderer for viewing and rendering it to the website.

Upvotes: 0

Pedro Ramos
Pedro Ramos

Reputation: 41

A good way to render PDFs in the browser is to use Mozilla's pdf-js.

https://github.com/mozilla/pdf.js/

Also, there is a wrapper for this in react that you could check out:

https://www.npmjs.com/package/react-pdf-js

Upvotes: 2

Related Questions