title: express.js - create a new pdf and force the download, without saving it on server
body: <p>I'm using express.js with pdfkit to generate a pdf and force a download to user. To this end I used the <code>res.download</code> function, that belongs to express.js. I could also <code>pipe</code> the pdf to <code>res</code>, but then the download wouldn't start.</p>
  
  <p>Ok, so what's my question: is it possible to somehow create the pdf on server and store it "in memory" or something like that? Currently I have to define a specific location where the pdf is created and then read from that location and serve it to the user. Basically, I'd like to "skip" that save-to-disk and read-from-disk, yet still force the download for the user.</p>
  
  <p>Perhaps I'm not understanding some concepts here, so any kind of explanation is greatly appreciated!</p>
  
  <p>Here's the code:</p>
  
  <pre><code>router.get('/pdf/:id', function(req, res) {
      Order._getById(req.params.id).
      then(function(order) {
          if (order !== null) {
              var doc = new PDFDocument();
              var result = order.toJSON();
  
              var products = result.products;
              var user = result.user;
              var comments = result.comments;
  
              doc.registerFont('arial', path.join(__dirname, '../fonts', 'arial' , 'arial.ttf'));         
  
              for (var i in products) {
                  var product = products[i];
                  doc.font('arial').text(product.name);
                  doc.image(path.join(__dirname,'../uploads','thumb', product.picture), {scale: 1.0});
                  doc.font('arial').text(product.price + ' €');
              }
  
              var r = doc.pipe(fs.createWriteStream(path.join(__dirname, '../uploads', 'out.pdf')));
              doc.end();
  
              r.on('finish', function(){
                  //the response is a "forced" download of a file I had to save on the disk
                  res.download(path.join(__dirname, '../uploads', 'out.pdf'));
              });
  
          } else {
              res.json(false);
          }
      });
  });
  </code></pre>
  
  <p>Again, if I don't save the file and just do this: <code>doc.pipe(res);</code> the pdf is opened in a new tab, and not forced to download.</p>