0

I have currently a problem to fetch my pdf from the backend to the front-end. I'm using NodeJS.

I have all my pdf made with Puppeteer and saved on a PDF directory.

I'm trying to fetch these PDF for the front-end. (for download)

I'have tried with res.sendFile and res.download (res.sendFile(path.join(__dirname,"/path"), res.download(path.join(__dirname,"/path))

This is the response from the front-end:

{data: "%PDF-1.4↵%����↵1 0 obj↵<</Creator (Chromium)↵/Prod…e 7↵/Root 6 0 R↵/Info 1 0 R>>↵startxref↵675↵%%EOF", status: 200,...}

I can't download this files.

Image

Code from back-end :

try { 
    res.download( path.join(__dirname, '/PDF/sofianddes.pdf'), (err) => { 
        if (err) { 
            console.log(err); 
        } 
    });
} catch (err) {}

Someone could help me ?

Thanks

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    What are you expecting to happen in the front-end? A PDF file is a binary file, it's not something you can directly look at the raw data. You have to feed it to a PDF viewer to view it. – jfriend00 Dec 06 '20 at 18:24
  • Download the file into pdf when the client click on a button for example –  Dec 06 '20 at 18:25
  • You should also verify that the PDF file you have saved on the server is valid. Try loading it into a PDF viewer directly from the file system on your server to verify that the PDF file you have there is valid. – jfriend00 Dec 06 '20 at 18:25
  • You should be using `res.download()` then if you want the browser to prompt the user to save the file to their file system. – jfriend00 Dec 06 '20 at 18:26
  • When i use res.download() nothing appear –  Dec 06 '20 at 18:27
  • try { res.download(path.join(__dirname, '/PDF/sofianddes.pdf'), (err) => { if (err) { console.log(err); } }); } catch (err) {} –  Dec 06 '20 at 18:28
  • nothing appears –  Dec 06 '20 at 18:28
  • If you're trying to ask a question about a specific piece of code you tried, then please put that exact code in your question. – jfriend00 Dec 06 '20 at 18:29
  • Done, i'have edit it –  Dec 06 '20 at 18:32
  • I receive the same object with res.download() –  Dec 06 '20 at 18:55
  • Hey , even I am facing this exact issue , how did u solve this one ? – Sanket Feb 05 '22 at 12:24

0 Answers0