1

I'm making a project where when the page opens, the pdf file is automatically downloaded, I managed to use this:

 window.addEventListener('load', () => {
        window.print();


    })

but I want when windows opens the file is directly downloaded to the directory that I have defined, for example D: / myproject.

is there any way? I don't use pdf library because I make pdf with css myself.

thank you

rash
  • 23
  • 4
  • 2
    How can you know if the user has the directory you defined? Maybe they have not `D://` at all? Anyway, you can't control where the file will be saved: https://stackoverflow.com/a/33612619/863110 – Mosh Feu Mar 05 '20 at 10:28
  • Does this answer your question? [How to specify download location in Html using JavaScript](https://stackoverflow.com/questions/33612566/how-to-specify-download-location-in-html-using-javascript) – Mosh Feu Mar 05 '20 at 10:30
  • so my problem, I want to disable pop up print and make it directly downloaded in the directory – rash Mar 05 '20 at 10:35
  • @rash you can't. Browser decides if a file should be downloaded directly to a user-specified directory or it should ask. It would be a security vulnerability otherwise. Imagine any website uploading files to your machine without asking. – LEQADA Mar 05 '20 at 10:40
  • As much as I understand, you're using the browser print option to convert your page to PDF. If you want to skip the print step, you'll have to generate the PDF by yourself. You can use a tool like jsPDF to achieve this. (https://stackoverflow.com/a/24825130/863110) – Mosh Feu Mar 05 '20 at 10:47
  • so can window.print () download directly on the server side? – rash Mar 05 '20 at 10:50

1 Answers1

1

window will not be accessible on server side code.

If you want to download file in browser, on just open the web page the you can use res.download() as follows:

app.get('/download', function(req, res){
  const file = `${__dirname}/upload-folder/file_name.pdf`;
  res.download(file); // Set disposition and send it.
});

As you want to download the file in a specific directory, so you can use the npm module

var download = require('download-file')

app.get('/download', function(req, res){
   var url = ${__dirname} + "/upload-folder/file_name.pdf";
   var options = {
    directory: "path of directory/",
    filename: "file_name.pdf"
   }

   download(url, options, function(err){
     if (err) throw err

     res.send("Done"); // Set disposition and send it.
   }) 

});

Edit: To convert the html code into the pdf you can use the npm module jspdf

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
  • It seems like OP wants to do it on the client side (aka browser) – LEQADA Mar 05 '20 at 10:26
  • `because I make pdf with css myself`. Also, the question is about set the destination - `I managed to use this... the file is directly downloaded to the directory that I have defined` – Mosh Feu Mar 05 '20 at 10:29
  • so my problem, I want to disable pop up print and make it directly downloaded in the directory – rash Mar 05 '20 at 10:36
  • Is download-file module asking for download Pop up? – Deep Kakkar Mar 05 '20 at 10:39
  • I want to download but my pdf file is still html, how do I make it pdf? – rash Mar 05 '20 at 10:45
  • 1
    oh, dear. ... HTML to pdf is a different question... I have replied just for download the file in a specific and in the browser as per required. to get that as pdf you can use jspdf like tool – Deep Kakkar Mar 05 '20 at 10:46
  • The download function here overwrites the OS default download folder? – Timo Mar 15 '22 at 14:20