Support Article
Watermark does not display in PDF
SA-96450
Summary
On adding a water mark to a PDF generated using the HTMLToPDF activity, the watermark does not display on the second page or the watermark image displays multiple times.
Error Messages
Not Applicable
Steps to Reproduce
- Create an HTML file.
- Create a binary file for the Watermark, url("Webwb/WMR.png");
- Add the below code:
body
{
background-repeat:repeat-y !important;
background-size:100%;
background-position:right top;
background-image: url("Webwb/WMR.png");
background-attachment:fixed;
}
inside a style tag -image size (Width: 1139 Height: 747)
- Run a HTML to PDF activity after adding the HTML file in the activity.
Root Cause
Pega does not provide the out-of-the-box support for watermark in the PDF using the HTMLToPDF activity.
Resolution
Perform the following local-change:
- Open the activity where the HTMLToPDF step is invoked.
- Comment the HTMLToPDF step.
- Add another step below the HTMLToPDF step.
- Use a Java method in the step added in Steps 3 and include the below code in the Java step.
try{
String markup = tools.getParamValue("Markup");
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
java.io.StringReader htmlReader = new java.io.StringReader(markup);
byte[] pdfBytes = null;
org.zefer.pd4ml.PD4ML pd4ml = new org.zefer.pd4ml.PD4ML();
pd4ml.setHtmlWidth(1100);
pd4ml.adjustHtmlWidth();
java.util.Map map = new java.util.HashMap();
map.put(org.zefer.pd4ml.PD4Constants.PD4ML_EXTRA_RESOURCE_LOADERS, "com.pega.pegarules.integration.engine.internal.mapping.PDFResourceHandler");
//Set WaterMark
org.zefer.pd4ml.PD4PageMark header = new org.zefer.pd4ml.PD4PageMark();
header.setWatermark( "https://pd4ml.com/i/logo.png", new java.awt.Rectangle(10,10,300,200), 30);
pd4ml.setPageHeader(header);
pd4ml.render(htmlReader, baos);
pdfBytes = baos.toByteArray();
tools.putParamValue("PDFDocument",pdfBytes);
} catch (Exception e) {
e.printStackTrace();
}
Published January 17, 2020 - Updated December 2, 2021
Have a question? Get answers now.
Visit the Collaboration Center to ask questions, engage in discussions, share ideas, and help others.