The module on npm exposes the pdfmake Printer, which the docs themselves do not cover. Using it is pretty simple though.
var fontDescriptors = {}; // required font setup, see ...
var docDefinition = {}; // this is what you see in all the docs
var Printer = require('pdfmake');
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();
}The document definition object that you pass to pdfmake
| properties | default | description |
|---|---|---|
| header | header, placed on every page | |
| footer | footer, placed on every page | |
| content | text, table, etc - the meat of the document | |
| defaultStyle | { fontSize: 12, font: 'Roboto' } | applied to entire document |
| styles | available style dictionaries | |
| pageSize | document page size, string or { width: number, height: number } |
|
| pageOrientation | portrait | document orientation, landscape or portrait |
| pageMargins | document margins - [left, top, right, bottom] or [horizontal, vertical] | |
| pageBreakBefore | function, executed to determine if there should be a page break before content | |
| background | background-layer added on every page, can be a function | |
| images | similar to styles, globally-available image content - most useful as 'data:image/jpeg;base64,...' |
|
| watermark | ||
| info | metadata | |
| content | metadata |
tableLayouts- ???autoPrint- ???
Nice, thanks! This helped me a lot