Search code examples
node.jspdfpuppeteerwatermark

How can I add a watermark to a PDF that is generated by puppeteer?


I'm using puppeteer to generate a PDF file for my report view and I want to add a watermark on that report PDF version. Is there anyway that I can do that?


Solution

  • The easiest way to do it, is to add an additional element to the page, which represents the watermark when the PDF is created. As you can change the page in any way you like, you could add a "Watermark" element to the page like this:

    await page.evaluate(() => {
      const div = document.createElement('div');
      div.innerHTML = 'Watermark Text...';
      div.style.cssText = "position: fixed; bottom: 10px; right: 10px; background: red; z-index: 10000";
      document.body.appendChild(div);
    });
    await page.pdf(/* ... */);
    

    The code adds a fixed element to the bottom right of the page. When printed it will appear on every printed page. You can use any kind of CSS styling to style your watermark. Just make sure it has a high z-index value, so that nothing overlaps it.