Most Recent

Blog Post

Previewing or downloading PDF files in Pega

Previewing or downloading PDF files in Pega

In Case Management it is common practice to generate Adobe Portable Document Format (PDF) files based on current case data. Case workers no longer have to rely on external word processing application like Microsoft Word or Office 365 for authoring, which is often time consuming. Pega applications can create documents on-the-fly and this makes your team become more productive by letting them focus on the job at hand — that is resolving your case in management terms.

Let´s shortly explain how to construct such a document and then provide the code to correctly return it to the browser for previewing and downloading this PDF. Such previewing is particularly useful for concept documents right before you decide to attach it to the case.

Constructing your Correspondence

One of more Correspondence rules typically define which rich (or plain) text strophes and other properties you wish to include as content. These rules serve as template for your document instance. Correspondences can be built visually with a rich text editor or as HTML source; the latter allowing for more precise control and cleaner code (see screenshot). You would typically want to keep the document structure separate from the styling just like on any other web-based platform. Correspondence rule as HTML source only mode

Note: The underlying library currently bundled with and leveraged by PRPC is called PD4ML Pro DMS (398fx4a1) and in fact "eats" regular HTML4.

Correspondence Type

PRPC assumes that you use correspondence rules of type Mail if you want to generate a PDF based on these rules. You are likely to already have correspondence rules of type Email for digital communications to your customers. These emails could also attach your (generated) case´s PDFs i.e. the more formal or even legal documents.

Previewing and Downloading

The current (7.2) implementation of PRPC´s internal Code-Pega-PDF.View activity only sets the MIME Media Type as contentType. To actually preview or download any PDF file including a hint for the file´s name we add the Content-Disposition response header (see Mozilla Developer Network for details).

Note: Internet Explorer expects the header "ContentDisposition" without the dash and this currently seems to function for Chrome and Firefox as well.

The following code will return the appropriate HTTP response headers to your browser:

byte[] byteArray=(byte[])tools.getParameterPage().getParameterValue("PDFDocument");
boolean inline=Boolean.parseBoolean(tools.getParameterPage().getParameterValue("Preview").toString());
//Send file bytes to client´s browser (Public API)
HashStringMap headers = new HashStringMap();//Constructor from StringMap implementation class
headers.put("contentType", "application/pdf" );
if (inline) {
headers.put("ContentDisposition", "inline;");
} else {
headers.put("ContentDisposition", "attachment; filename=\"preview.pdf\"");
}
String errorMessage = tools.sendFile(byteArray, "preview.pdf", true, headers, true);//Sends the byteArray to the client
if (errorMessage != null) {
System.err.println("A sendFile error has occurred.");
}

Make sure you have Param.PDFDocument and Param.Preview in scope before this Java step in your activity as well as a local boolean variable called inline which determines previewing (true) versus downloading (false). [Edgar] (5/26/2017 2:41:21 PM)

 

Twitter Feed

@edgarverburg

Bio

About Edgar

Edgar is a software engineer with experience in TIBCO Middleware and Pega Case Managemement. He holds a master's degree in Computer Science with a specialization in Data Visualization & Computer Graphics.

In his spare time Edgar reads SOS and Empire, mixes house music, blogs and writes film reviews or goes running.


Currently employed by SynTouch he is specifically looking for a PRPC project. Feel free to contact him for challenging assignments through LinkedIn.