Add copy with watermark (pdf-utils)
-
Hi,
We have the requierment, that a delivery note report has to have a copy with a watermark. The same report twice in a single pdf document. If possible the second iteration with a watermark.
To duplicate the report I use a simple script with pdf-utils:
const jsreport = require('jsreport-proxy') async function afterRender(req, res) { res.content = await jsreport.pdfUtils.append(res.content, res.content); }
Can I add a watermark within this script?
Maybe:- use pdfUtils.parse to manipulate content of the second iteration directly
- or use pdfUtils with merge with another watermark template
What would be te best way to solve such a task?
Reto E.
-
This script should do the trick. Attach it to the main template. The "Watermark" template renders just the watermark image.
const jsreport = require('jsreport-proxy') async function afterRender(req, res) { const watermarkRes = await jsreport.render({ template: { name: 'Watermark' } }) const $pdf = await jsreport.pdfUtils.parse(res.content) const pagesToMerge = new Array($pdf.pages.length).fill(watermarkRes.content) const originalBuffer = Buffer.from(res.content) const withWatermarkBuffer = await jsreport.pdfUtils.merge(originalBuffer, pagesToMerge) res.content = await jsreport.pdfUtils.append(res.content, withWatermarkBuffer) }
-
Thank you for sharing.
I have a problem though. My document is made up of a main template and a header-footer template that I merge with PDF-Utils (in the GUI).
Using this snippet of code I have the correct number of pages, and on the second set of pages I have a watermark but I lose the header/footer..Any idea? Maybe because the GUI pdfUtils is run after render?
in any case I've tried to programmatically read the template and merge it and then merge over the watermark, but I don't seem to be able..const jsreport = require('jsreport-proxy') async function afterRender(req, res) { //this is the watermark const duplicateRes = await jsreport.render({ template: { name: 'watermark' } }) const $pdf = await jsreport.pdfUtils.parse(res.content) const pagesToMerge = new Array($pdf.pages.length).fill(duplicateRes.content) const originalBuffer = Buffer.from(res.content) //now add header-footer const corniceRes = await jsreport.render({template: {name: 'header_footer' }}) const pagesToMergeCornice = new Array($pdf.pages.length).fill(corniceRes.content) const corniceBuffer = await jsreport.pdfUtils.merge(originalBuffer, pagesToMergeCornice) // const withDuplicateBuffer = await jsreport.pdfUtils.merge(corniceBuffer, pagesToMerge) res.content = await jsreport.pdfUtils.append(res.content, withDuplicateBuffer) }
Thanks
Z