title: Javascript, I am using PDF kit to create pdf, and attached it to email, but sometimes pdf file in the email cannot open
body: <p>I am using PDFkit with javascript to create pdf file and attached it to email. </p>
  
  <p>When I received email, some pdf files can open ,some cannot. I guess that pdf files don't finish creation and then be attached to the email? My question is how to make sure that the pdf files are finished ? Thank you so much!</p>
  
  <p>My code is shown below:</p>
  
  <pre><code>var doc;
  var stream;
  var attachmentList = [];
  for (var i = 0; i &lt; sites.length; i++)　 {
      doc = new PDFDocument();
  
      doc.pipe(fs.createWriteStream('pdfStorage/' + sites[i].title.toString() + '.pdf'));
      doc.font('Calibri.ttf').fontSize(20)
          .text('PERFORMANCE REPORT for ' + sites[i].title.toString());
  
      doc.end();
  
      // Add to attachments
      attachmentList.push({
          path: './pdfStorage/' + sites[i].title.toString() + '.pdf',
      });
  }
  }
  
  if (email) {
      //this is email information ;
  
  }
  </code></pre>