Creating a PDF and moving it to a directory?

1.2k Views Asked by At

I am using PDFMake to create a PDF file, the body of which is dynamic. When I create the PDF it downloads to my downloads folder.

I would love for the PDF to be able to save to a directory of my choice. I have looked through the PDFMake docs and all I can see is that it gives you the option to give it a custom name. Does anyone know how I can make this happen. I don't want to just retrieve it from the folder it's in because it could possibly be in a different folder depending on the user.

Below is the code where I create the PDF:

function createPDF() {

    let body = tinymce.get('elm1').getContent();

    let docDefinition = {
      content: body
    };

    console.log(body);

    pdfMake.createPdf(docDefinition).download();
  }
1

There are 1 best solutions below

6
On

Example:

let fs = require('fs');
let PdfPrinter = require('pdfmake');

let printer = new PdfPrinter();

let docDefinition = {
    content: .....
};

pdf = printer.createPdfKitDocument(docDefinition);

pdf.pipe(fs.createWriteStream('YOUR-PDFs/YOUR-PDF.pdf'));
pdf.end();