Answers for "createPdfKitDocument"

0

createPdfKitDocument

var path = require('path');
var Printer = require('pdfmake');

// fonts are available in the test-env/tests/fonts path, this is a helper
function fontPath(file) {
  retrun path.resolve('pdfmake', 'test-env', 'tests', 'fonts', file);
}

// required font setup, requires that you link to the fonts shipped with npm
var fontDescriptors = {
  Roboto: {
    normal: fontPath('Roboto-Regular.ttf'),
    bold: fontPath('Roboto-Medium.ttf'),
    italics: fontPath('Roboto-Italic.ttf'),
    bolditalics: fontPath('Roboto-Italic.ttf'),
  }
}; 
var docDefinition = {}; // this is what you see in all the docs
var printer = new Printer(fontDescriptors);

// get a reference to the PdfKit instance, which is a streaming interface
var pdfDoc = printer.createPdfKitDocument(docDefinition);

// pipe to a file or response object
function streamTo(pdfDoc) {
  // writeStream can be an fs object, response object, etc
  var writeStream = fs.createWriteStream('pdfs/output.pdf');
  var pdfDoc = printer.createPdfKitDocument(docDefinition);
  pdfDoc.pipe(writeStream); // streaming interface
}

// turn the stream into a Buffer
// Usage: getDoc(pdfDoc, function (err, buffer, pages) { var base64 = buffer.toString('base64'); /* app logic */ });
function getDoc(pdfDoc, cb) {
  // buffer the output
  var chunks = [];
  
  pdfDoc.on('data', function(chunk) {
  	chunks.push(chunk);
  });
  pdfDoc.on('end', function() {
  	var result = Buffer.concat(chunks);
  	cb(null, result, pdfDoc._pdfMakePages);
  });
  pdfDoc.on('error', cb);
  
  // close the stream
  pdfDoc.end();
}
Posted by: Guest on February-02-2022

Browse Popular Code Answers by Language