PDF Generation In Pega

This is first blog from the Crochet family, we hope this journey never ends. In this blog, we will try to cover all the important things related to the PDF generation in Pega.

So Let’s Begin!!!

Overview of HTMLToPDF

So first we will start with the overview of the OOTB Activity Rule “HTMLToPDF” provided by Pega in order to generate PDF Document. HTML2PDF is activity rule contains 8 steps.

The first step is java step in which the output stale parameter PDFDocument is getting cleared.

In the Second step, it is checking if this “UseCompactStylesforPDF” input parameter is blank it will check in the DSS “UseCompactStylesforPDF” for the value.

Note: – We will discuss about all the common parameter of HTMLToPDF activity rule in details later in the blog.

We can pass the markup or we can either pass HTML Stream so if we pass stream it will convert it to markup in the third step.

In the fourth step, if the value of Param “UseCompactStylesforPDF” is true it will include the minimal style for css “pzCompactPDFStyles” to the HTML

In the fifth step, if the value of Param “UseCompactStylesforPDF” is true or if we want the pega minimal css to be included then it will strip the style css from the provided markup.

In the sixth step, it is Copying the “Markup” param to HTMLStream local variable and it will throw exception if provided stream is empty. And In seventh step, it is clearing parameter value.

In the eight step, it is generating the PDF Document and giving us output in output param “PDFDocument”

Commonly Used Parameters

Markup -> HTML markup to convert to PDF. Pass this or either Stream Name

PDFName -> Name of PDF which will be generated.

StreamName -> HTML rule stream name to use if Markup is not populated.

pyPDFPageOrientation -> Optional page orientation(portrait/landscape), Default landscape

pyPDFPageSize -> Optional page size (A4/LETTER/LEGAL), Default A4

PDFDocument à Output Param, it will include the generated pdf document

pyPDFFontsDirectory -> The path to the local directory where fonts are located

pyPDFEmbedFont-> Enable embedding the font(s) from pyPDFFontsDirectory into the PDF document.

pyPDFPropsDirectory -> Optional directory to use at runtime for the PDF generation library’s Properties file. Setting this parameter will disable the removal of the properties file at the end of use.

pyPDFHeaderHTMLTemplate -> Optional pdf header HTML template .

pyPDFFooterHTMLTemplate -> Optional pdf footer HTML template

UseCompactStylesforPDF -> Ignore skin while exporting to PDF and use minimal skin


Now We will create an activity rule in which we will set some of the required paramters and will call HTMLToPDF for generating pdf and will use View to download it on the client side.

First Step of our activity is to set basic details like PDFName, font directory, orientation, UseCompactStyleforPDF etc.

Note: Create a DSS for the font path so that we can change it accordingly.

 In our case we have a windows server so our dss have value as follows:

Add a second step and check if server is windows then skip this as we need font path in “pyPDFPropsDirectory” Param only if server is unix/linux.

Note: Create a DSS for the checking windows server or not so that we can change it accordingly.

Now we will add the third and the fourth step in the activity in which we will set html header stream and HTML Footer stream

HTML Header Stream

Note: – For the Pagination we have added <p><span><%= “${total}” %><span> <span><%= “${page}” %></span></p> in the header. Total variable will return the total number of page and page will return current page for the pagination.

HTML Footer Stream

Now we add fifth step in which we will call the HTMLToPDF activity by pass current page parameter

Now in the sixth step, we will call the view activity to download the pdf on the client side, For this add the below page in the pages and classes:-

Now we will call view activity by passing TempPDF page as step page and by passing current parameter page:-

Note :- PDFDocument is the output parameter of the HTMLToPDF Activity rule.

Add a Page-Remove Step in the 7th Step and we are done with the activity.

Note – If you want to auto attach the PDFDocument with the case add a step in the same activity call the activity rule “Code-Pega-PDF.AttachToWork” and pass current parameter page.

Now Let’s create a section rule which we will use as a markup for the pdf

On click of GeneratePDF we will call our activity so we will use the below action as follows:-

Leave a Reply

Your email address will not be published. Required fields are marked *