Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,325 changes: 3,314 additions & 11 deletions build/pdfmake.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/pdfmake.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/pdfmake.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/pdfmake.min.js.map

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions examples/absolute.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified examples/pdfs/absolute.pdf
Binary file not shown.
Binary file modified examples/pdfs/qrCode.pdf
Binary file not shown.
Binary file added examples/pdfs/qrCodeV2.pdf
Binary file not shown.
96 changes: 96 additions & 0 deletions examples/qrCodeV2.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@foliojs-fork/linebreak": "^1.1.1",
"@foliojs-fork/pdfkit": "^0.13.0",
"iconv-lite": "^0.6.3",
"qrcode-generator": "1.4.4",
"xmldoc": "^1.1.2"
},
"devDependencies": {
Expand Down Expand Up @@ -83,4 +84,4 @@
]
}
}
}
}
9 changes: 9 additions & 0 deletions src/docMeasure.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var fontStringify = require('./helpers').fontStringify;
var getNodeId = require('./helpers').getNodeId;
var pack = require('./helpers').pack;
var qrEncoder = require('./qrEnc.js');
var qrEncoderV2 = require('./qrEncV2.js');

/**
* @private
Expand Down Expand Up @@ -67,6 +68,8 @@ DocMeasure.prototype.measureNode = function (node) {
return extendMargins(self.measureCanvas(node));
} else if (node.qr) {
return extendMargins(self.measureQr(node));
} else if (node.qrV2) {
return extendMargins(self.measureQrV2(node));
} else {
throw 'Unrecognized document structure: ' + JSON.stringify(node, fontStringify);
}
Expand Down Expand Up @@ -804,4 +807,10 @@ DocMeasure.prototype.measureQr = function (node) {
return node;
};

DocMeasure.prototype.measureQrV2 = async function (node) {
node = await qrEncoderV2.measure(node, this.images);
node._alignment = this.styleStack.getProperty('alignment');
return node;
};

module.exports = DocMeasure;
14 changes: 9 additions & 5 deletions src/docPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ DocPreprocessor.prototype.preprocessDocument = function (docStructure) {
return this.preprocessNode(docStructure);
};

DocPreprocessor.prototype.preprocessNode = function (node) {
DocPreprocessor.prototype.preprocessNode = function (node, order = 1) {
// expand shortcuts and casting values
if (isArray(node)) {
node = { stack: node };
Expand All @@ -34,6 +34,8 @@ DocPreprocessor.prototype.preprocessNode = function (node) {
node.text = '';
}

node.order = order;

if (node.columns) {
return this.preprocessColumns(node);
} else if (node.stack) {
Expand All @@ -56,6 +58,8 @@ DocPreprocessor.prototype.preprocessNode = function (node) {
return this.preprocessCanvas(node);
} else if (node.qr) {
return this.preprocessQr(node);
} else if (node.qrV2) {
return this.preprocessQr(node);
} else if (node.pageReference || node.textReference) {
return this.preprocessText(node);
} else {
Expand All @@ -67,7 +71,7 @@ DocPreprocessor.prototype.preprocessColumns = function (node) {
var columns = node.columns;

for (var i = 0, l = columns.length; i < l; i++) {
columns[i] = this.preprocessNode(columns[i]);
columns[i] = this.preprocessNode(columns[i], i);
}

return node;
Expand All @@ -77,7 +81,7 @@ DocPreprocessor.prototype.preprocessVerticalContainer = function (node) {
var items = node.stack;

for (var i = 0, l = items.length; i < l; i++) {
items[i] = this.preprocessNode(items[i]);
items[i] = this.preprocessNode(items[i], i);
}

return node;
Expand All @@ -87,7 +91,7 @@ DocPreprocessor.prototype.preprocessList = function (node) {
var items = node.ul || node.ol;

for (var i = 0, l = items.length; i < l; i++) {
items[i] = this.preprocessNode(items[i]);
items[i] = this.preprocessNode(items[i], i);
}

return node;
Expand All @@ -105,7 +109,7 @@ DocPreprocessor.prototype.preprocessTable = function (node) {
data = '';
}
if (!data._span) {
rowData[col] = this.preprocessNode(data);
rowData[col] = this.preprocessNode(data, (col * node.table.body[0].length) + row);
}
}
}
Expand Down
33 changes: 32 additions & 1 deletion src/elementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ ElementWriter.prototype.addImage = function (image, index, type) {
};

ElementWriter.prototype.addSVG = function (image, index) {
return this.addImage(image, index, 'svg')
return this.addImage(image, index, 'svg');
};

ElementWriter.prototype.addQr = function (qr, index) {
Expand Down Expand Up @@ -149,6 +149,37 @@ ElementWriter.prototype.addQr = function (qr, index) {
return position;
};

ElementWriter.prototype.addQrV2 = function (qr, index) {
return this.addImage(qr, index, 'svg');
// var context = this.context;
// var page = context.getCurrentPage(),
// position = this.getCurrentPositionOnPage();

// if (!page || (qr.absolutePosition === undefined && context.availableHeight < qr._height)) {
// return false;
// }

// if (qr._x === undefined) {
// qr._x = qr.x || 0;
// }

// qr.x = context.x + qr._x;
// qr.y = context.y;

// this.alignImage(qr);

// for (var i = 0, l = qr._canvas.length; i < l; i++) {
// var vector = qr._canvas[i];
// vector.x += qr.x;
// vector.y += qr.y;
// this.addVector(vector, true, true, index);
// }

// context.moveDown(qr._height);

// return position;
};

ElementWriter.prototype.alignImage = function (image) {
var width = this.context.availableWidth;
var imageWidth = image._minWidth;
Expand Down
47 changes: 43 additions & 4 deletions src/layoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ LayoutBuilder.prototype.registerTableLayouts = function (tableLayouts) {
* @param {Object} defaultStyle default style definition
* @return {Array} an array of pages
*/
LayoutBuilder.prototype.layoutDocument = function (docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark, pageBreakBeforeFct) {
LayoutBuilder.prototype.layoutDocument = async function (docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark, pageBreakBeforeFct) {


function addPageBreaksIfNecessary(linearNodeList, pages) {

Expand Down Expand Up @@ -133,16 +134,44 @@ LayoutBuilder.prototype.layoutDocument = function (docStructure, fontProvider, s
});
}

var result = this.tryLayoutDocument(docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark);
var result = await this.tryLayoutDocument(docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark);
while (addPageBreaksIfNecessary(result.linearNodeList, result.pages)) {
resetXYs(result);
result = this.tryLayoutDocument(docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark);
result = await this.tryLayoutDocument(docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark);
}

return result.pages;
};

LayoutBuilder.prototype.tryLayoutDocument = function (docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark, pageBreakBeforeFct) {
// https://alphahydrae.com/2021/02/a-javascript-function-that-recursively-resolves-promises/
async function resolver(value) {
// Await the value in case it's a promise.
const resolved = await value;

if (isPlainObject(resolved)) {
const entries = Object.entries(resolved);
const resolvedEntries = entries.map(
// Recursively resolve object values.
async ([key, value]) => [key, await resolver(value)]
);
return Object.fromEntries(
await Promise.all(resolvedEntries)
);
} else if (Array.isArray(resolved)) {
// Recursively resolve array values.
return Promise.all(resolved.map(resolver));
}

return resolved;
}

function isPlainObject(value) {
return typeof value === 'object' &&
value !== null &&
value.constructor === Object;
}

LayoutBuilder.prototype.tryLayoutDocument = async function (docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark, pageBreakBeforeFct) {

this.linearNodeList = [];
docStructure = this.docPreprocessor.preprocessDocument(docStructure);
Expand All @@ -157,6 +186,8 @@ LayoutBuilder.prototype.tryLayoutDocument = function (docStructure, fontProvider
});

this.addBackground(background);
docStructure = await resolver(docStructure)

this.processNode(docStructure);
this.addHeadersAndFooters(header, footer);
if (watermark != null) {
Expand Down Expand Up @@ -411,6 +442,8 @@ LayoutBuilder.prototype.processNode = function (node) {
self.processCanvas(node);
} else if (node.qr) {
self.processQr(node);
} else if (node.qrV2) {
self.processQrV2(node);
} else if (!node._span) {
throw 'Unrecognized document structure: ' + JSON.stringify(node, fontStringify);
}
Expand Down Expand Up @@ -804,4 +837,10 @@ LayoutBuilder.prototype.processQr = function (node) {
node.positions.push(position);
};

LayoutBuilder.prototype.processQrV2 = function (node) {
var position = this.writer.addQrV2(node);
node.positions.push(position);
};


module.exports = LayoutBuilder;
7 changes: 7 additions & 0 deletions src/pageElementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ PageElementWriter.prototype.addQr = function (qr, index) {
});
};

PageElementWriter.prototype.addQrV2 = function (qr, index) {
return fitOnPage(this, function (self) {
return self.writer.addQrV2(qr, index);
});
};


PageElementWriter.prototype.addVector = function (vector, ignoreContextX, ignoreContextY, index) {
return this.writer.addVector(vector, ignoreContextX, ignoreContextY, index);
};
Expand Down
4 changes: 2 additions & 2 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function PdfPrinter(fontDescriptors) {
*
* @return {Object} a pdfKit document object which can be saved or encode to data-url
*/
PdfPrinter.prototype.createPdfKitDocument = function (docDefinition, options) {
PdfPrinter.prototype.createPdfKitDocument = async function (docDefinition, options) {
options = options || {};

docDefinition.version = docDefinition.version || '1.3';
Expand Down Expand Up @@ -143,7 +143,7 @@ PdfPrinter.prototype.createPdfKitDocument = function (docDefinition, options) {
builder.registerTableLayouts(options.tableLayouts);
}

var pages = builder.layoutDocument(docDefinition.content, this.fontProvider, docDefinition.styles || {}, docDefinition.defaultStyle || {
var pages = await builder.layoutDocument(docDefinition.content, this.fontProvider, docDefinition.styles || {}, docDefinition.defaultStyle || {
fontSize: 12,
font: 'Roboto'
}, docDefinition.background, docDefinition.header, docDefinition.footer, docDefinition.images, docDefinition.watermark, docDefinition.pageBreakBefore);
Expand Down
Loading