Search code examples
javascriptwatermarkpdfmake

Watermark using pdfmake


In Javascript, I am using pdfmake to generate a pdf document. I read it from github that it supports watermark and the following is my usage but it is giving me some random and weird characters. Those characters are the same no matter what texts are provided in watermark. Anyone got an idea?

pdfmake

enter image description here

enter image description here


Solution

  • I can't say as I have a solution to this problem, but I have made it work for me. It seems the renderWatermark() function at or around line 465 or pdfmake.js has problems with encoding the font.

    var encoded = watermark.font.encode(watermark.text); returns a blank string, and doesnt have the extended properties the function is looking for later on.

    By changing

    pdfKitDoc.addContent('/' + encoded.fontId + ' ' + watermark.size.fontSize + ' Tf');
    

    to

    pdfKitDoc.addContent('/ ' + watermark.size.fontSize + ' Tf');
    

    and

    pdfKitDoc.addContent('<' + encoded.encodedText + '> Tj');
    

    to

    pdfKitDoc.addContent('(' + watermark.text + ') Tj');
    

    I was able to get a watermark to show up in the correct position, albeit with a generic font rather than anything I had selected.