Watermark using pdfmake
Asked Answered
I

4

5

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

Indocile answered 29/7, 2015 at 4:41 Comment(0)
L
3

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.

Lorollas answered 18/9, 2015 at 18:24 Comment(0)
W
8

Solution: use this example from the docs:

var docDefinition = {
    //watermark: 'test watermark',
    watermark: {text: 'test watermark', color: 'blue', opacity: 0.3, bold: true, italics: false},
    content: [
        'Test page of watermark.\n\n',
    ]
};
Westberry answered 26/6, 2018 at 11:47 Comment(0)
L
3

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.

Lorollas answered 18/9, 2015 at 18:24 Comment(0)
H
1

I made few more changes to make watermark work as I wanted. Below are the changes I did in printer.js

pdfKitDoc.fill('darkgray'); // Change colour
pdfKitDoc.opacity(0.3); // Change opacity as watermark was too dark for my liking

Commented out below to keep the watermark text horizontal
//var angle = Math.atan2(pdfKitDoc.page.height, pdfKitDoc.page.width) * 180/Math.PI;
//pdfKitDoc.rotate(0, {origin: [pdfKitDoc.page.width/2, pdfKitDoc.page.height/2]});

// Changed width and height for proper rendering of watermark text horizontally
pdfKitDoc.addContent('' + (pdfKitDoc.page.width/2 - watermark.size.size.width/4) + ' ' + (pdfKitDoc.page.height/2 - watermark.size.size.height/4) + ' Td');

Ideally, colour, opacity & text orientation should be configurable.

Haworth answered 7/1, 2016 at 5:14 Comment(0)
M
0

Example

let docDefinition = {
        //watermark: 'watermark',
        watermark: {text: 'watermark', color: 'black', opacity: 0.3, bold: true, italics: false},
        content: [
             { text: "watermark test", fontSize: 18 },,
        ]
    };
Mercy answered 8/9, 2021 at 7:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.