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)
    }
    

Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.