Skip to content

Commit

Permalink
convert static header/footer to dynamic content before document proce…
Browse files Browse the repository at this point in the history
…ssing
  • Loading branch information
liborm85 committed Nov 3, 2024
1 parent eb7305d commit 03d2da6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
13 changes: 2 additions & 11 deletions src/LayoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,6 @@ class LayoutBuilder {
}
}

addStaticRepeatable(headerOrFooter, sizeFunction) {
this.addDynamicRepeatable(() => // copy to new object
JSON.parse(JSON.stringify(headerOrFooter)), sizeFunction);
}

addDynamicRepeatable(nodeGetter, sizeFunction) {
let pages = this.writer.context().pages;

Expand Down Expand Up @@ -248,16 +243,12 @@ class LayoutBuilder {
height: pageMargins.bottom
});

if (typeof header === 'function') {
if (header) {
this.addDynamicRepeatable(header, headerSizeFct);
} else if (header) {
this.addStaticRepeatable(header, headerSizeFct);
}

if (typeof footer === 'function') {
if (footer) {
this.addDynamicRepeatable(footer, footerSizeFct);
} else if (footer) {
this.addStaticRepeatable(footer, footerSizeFct);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { normalizePageSize, normalizePageMargin } from './PageSize';
import { tableLayouts } from './tableLayouts';
import Renderer from './Renderer';
import { isNumber, isValue } from './helpers/variableType';
import { convertToDynamicContent } from './helpers/tools';

/**
* Printer which turns document definition into a pdf
Expand Down Expand Up @@ -56,6 +57,14 @@ class PdfPrinter {
docDefinition.pageMargins = isValue(docDefinition.pageMargins) ? docDefinition.pageMargins : 40;
docDefinition.patterns = docDefinition.patterns || {};

if (docDefinition.header && typeof docDefinition.header !== 'function') {
docDefinition.header = convertToDynamicContent(docDefinition.header);
}

if (docDefinition.footer && typeof docDefinition.footer !== 'function') {
docDefinition.footer = convertToDynamicContent(docDefinition.footer);
}

let pageSize = normalizePageSize(docDefinition.pageSize, docDefinition.pageOrientation);

let pdfOptions = {
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ export function offsetVector(vector, x, y) {
break;
}
}

export function convertToDynamicContent(staticContent) {
return () => // copy to new object
JSON.parse(JSON.stringify(staticContent));
}
6 changes: 4 additions & 2 deletions tests/integration/svgs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ describe('Integration Test: svg\'s', function () {
it('writes svg in header', function () {
var dd = {
content: [],
header: {
svg: '<svg width="200" height="100" viewBox="0 0 600 300"></svg>',
header: function () {
return {
svg: '<svg width="200" height="100" viewBox="0 0 600 300"></svg>',
};
}
};

Expand Down

0 comments on commit 03d2da6

Please sign in to comment.