Skip to content

Commit

Permalink
custom watermark support
Browse files Browse the repository at this point in the history
  • Loading branch information
liborm85 committed Nov 24, 2024
1 parent fd9e562 commit ece4927
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Binary file modified examples/pdfs/sections.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions examples/sections.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var docDefinition = {
header: function () { return 'default header'; },
footer: function () { return 'default footer'; },
background: function() { return { text:'global background', alignment: 'right' }; },
watermark: 'default watermark',
content: [
{
section: [
Expand All @@ -19,6 +20,7 @@ var docDefinition = {
header: function (currentPage, pageCount) { return 'header: ' + currentPage.toString() + ' of ' + pageCount; },
footer: function (currentPage, pageCount) { return 'footer: ' + currentPage.toString() + ' of ' + pageCount; },
background: function() { return { text:'SECTION 2 background', alignment: 'right' }; },
watermark: 'SECTION 2 watermark',
pageOrientation: 'landscape',
section: [
'SECTION 2',
Expand All @@ -29,6 +31,7 @@ var docDefinition = {
header: null,
footer: null,
background: null,
watermark: null,
pageSize: 'A7',
pageOrientation: 'portrait',
section: [
Expand All @@ -37,6 +40,7 @@ var docDefinition = {
]
},
{
watermark: 'inherit',
pageSize: 'A6',
pageOrientation: 'portrait',
pageMargins: 5,
Expand All @@ -46,6 +50,7 @@ var docDefinition = {
]
},
{
watermark: 'watermark for inherit',
pageSize: 'A6',
pageOrientation: 'landscape',
pageMargins: 10,
Expand All @@ -55,6 +60,7 @@ var docDefinition = {
]
},
{
watermark: 'inherit',
pageSize: 'inherit',
pageOrientation: 'inherit',
pageMargins: 'inherit',
Expand Down
31 changes: 19 additions & 12 deletions src/LayoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ class LayoutBuilder {

this.processNode(docStructure);
this.addHeadersAndFooters(header, footer);
if (watermark != null) {
this.addWatermark(watermark, pdfDocument, defaultStyle);
}
this.addWatermark(watermark, pdfDocument, defaultStyle);

return { pages: this.writer.context().pages, linearNodeList: this.linearNodeList };
}
Expand Down Expand Up @@ -276,17 +274,26 @@ class LayoutBuilder {
}

addWatermark(watermark, pdfDocument, defaultStyle) {
if (isString(watermark)) {
watermark = { 'text': watermark };
}

if (!watermark.text) { // empty watermark text
return;
}

let pages = this.writer.context().pages;
for (let i = 0, l = pages.length; i < l; i++) {
pages[i].watermark = getWatermarkObject({ ...watermark }, pages[i].pageSize, pdfDocument, defaultStyle);
let pageWatermark = watermark;
if (pages[i].customProperties['watermark'] || pages[i].customProperties['watermark'] === null) {
pageWatermark = pages[i].customProperties['watermark'];
}

if (pageWatermark === undefined || pageWatermark === null) {
continue;
}

if (isString(pageWatermark)) {
pageWatermark = { 'text': pageWatermark };
}

if (!pageWatermark.text) { // empty watermark text
continue;
}

pages[i].watermark = getWatermarkObject({ ...pageWatermark }, pages[i].pageSize, pdfDocument, defaultStyle);
}

function getWatermarkObject(watermark, pageSize, pdfDocument, defaultStyle) {
Expand Down

0 comments on commit ece4927

Please sign in to comment.