diff --git a/CHANGELOG.md b/CHANGELOG.md index bb78f75bc..6fd326259 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Fixed speed in Node.js if is fetching URL for image or font redirected - Fixed aspect ratio for image with exif orientation tag +- Fixed font size calculation for watermark if is page orientation is changed ## 0.3.0-beta.12 - 2024-11-03 diff --git a/src/LayoutBuilder.js b/src/LayoutBuilder.js index 37dc80247..1dc884a28 100644 --- a/src/LayoutBuilder.js +++ b/src/LayoutBuilder.js @@ -261,36 +261,40 @@ class LayoutBuilder { return; } - watermark.font = watermark.font || defaultStyle.font || 'Roboto'; - watermark.fontSize = watermark.fontSize || 'auto'; - watermark.color = watermark.color || 'black'; - watermark.opacity = isNumber(watermark.opacity) ? watermark.opacity : 0.6; - watermark.bold = watermark.bold || false; - watermark.italics = watermark.italics || false; - watermark.angle = isValue(watermark.angle) ? watermark.angle : null; - - if (watermark.angle === null) { - watermark.angle = Math.atan2(this.pageSize.height, this.pageSize.width) * -180 / Math.PI; + 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); } - if (watermark.fontSize === 'auto') { - watermark.fontSize = getWatermarkFontSize(this.pageSize, watermark, pdfDocument); - } + function getWatermarkObject(watermark, pageSize, pdfDocument, defaultStyle) { + watermark.font = watermark.font || defaultStyle.font || 'Roboto'; + watermark.fontSize = watermark.fontSize || 'auto'; + watermark.color = watermark.color || 'black'; + watermark.opacity = isNumber(watermark.opacity) ? watermark.opacity : 0.6; + watermark.bold = watermark.bold || false; + watermark.italics = watermark.italics || false; + watermark.angle = isValue(watermark.angle) ? watermark.angle : null; + + if (watermark.angle === null) { + watermark.angle = Math.atan2(pageSize.height, pageSize.width) * -180 / Math.PI; + } - let watermarkObject = { - text: watermark.text, - font: pdfDocument.provideFont(watermark.font, watermark.bold, watermark.italics), - fontSize: watermark.fontSize, - color: watermark.color, - opacity: watermark.opacity, - angle: watermark.angle - }; + if (watermark.fontSize === 'auto') { + watermark.fontSize = getWatermarkFontSize(pageSize, watermark, pdfDocument); + } - watermarkObject._size = getWatermarkSize(watermark, pdfDocument); + let watermarkObject = { + text: watermark.text, + font: pdfDocument.provideFont(watermark.font, watermark.bold, watermark.italics), + fontSize: watermark.fontSize, + color: watermark.color, + opacity: watermark.opacity, + angle: watermark.angle + }; - let pages = this.writer.context().pages; - for (let i = 0, l = pages.length; i < l; i++) { - pages[i].watermark = watermarkObject; + watermarkObject._size = getWatermarkSize(watermark, pdfDocument); + + return watermarkObject; } function getWatermarkSize(watermark, pdfDocument) {