PDF Creation from Catalog Variables
SN Scout
·
Dec 23, 2020
·
article
So the requirement went like this: create a catalog item to replace a set of 3 Word doc templates. Package up the submission in a PDF, outputting to the record so that the analyst can then review (and revise and rerun if necessary). This would then be shared with an external party that doesn't have access to the instance directly. The PDF should roughly replicate the layout of the original doc template in flow.
Simple enough. How do I create a PDF again?
I went about trying to figure out how to print to PDF and attach to the record. I found thisarticle on the community to be helpful start, but not complete for what I needed with regards to the variables and their layout.
The key API here is 'GeneralFormAPI'. There isn't much in the way of formal documentation of this Script Include, aside from the helpful community article above and anotherCommunity FAQ here. Most are targeted toward HRSD functions, but HRSD is not required for this.
A call to GeneralFormAPI accepts 3 parameters:
- File name that we will create from this call
- Table name of our target record
- Sys_id of the target record we are attaching to
So we form our initial object as follows:
| 1 | var formAPI = new global.GeneralFormAPI(filename, table, table_sys_id) |
|---|
The Script Include then has several functions for us to use. The most important are setDocument and createPDF.
| 1 2 3 4 5 6 7 8 9 10 11 | setDocument : function(headerImage, footerImage, footnote, headerPosition, footerPosition, pageSize) {***}, createPDF : function(body, pages) {***}, _parseBodyForAttachmentSysId : function(body) {***}, _parseBodyForImagesFromLib : function(body) {***}, _getBase64ImageFromSysAttachment : function(attachmentGR) {***}, getBase64SignatureImage : function(signatureImageSysId) {***}, |
|---|
setDocument
With our first act is to set the document attributes:
- headerImage = attachment GlideRecord to image to be included in header of PDF
- footerImage = attachment GlideRecord to image to be included in footer of PDF
- footnote = string footer text
- headerPosition = numeric position for header (0==left, 1==center, 2==right)
- footerPosition = numeric position for footer (0==left, 1==center, 2==right)
- pageSize = string name of paper size (default is 'a4')
For example:
| 1 | formAPI.setDocument(null, null, footer, '1', '1', 'a4'); |
|---|
createPDF
This is where we are actually executing the function to create the PDF file and attach it to our record. We can pass one of two parameters into this function:
- body = string of well-formed HTML (do not use the
tag. It will break) - pages = array of JSON objects containing HTML
Option 1 - body:
| 1 2 3 4 | var htmlContent = "Here's my header"; htmlContent += "initial content here "; formAPI.createPDF(htmlContent); |
|---|
Option 2 - pages:
| 1 2 3 4 5 6 | var pages = []; pages.push({heading: 'First Page HeaderFirst page content ' }); pages.push({heading: 'Second page content ' }) pages.push({heading: '
|
|---|
Practical Application
So back to our use case. We need to parse through our catalog item variables, format and print them out like the original form template. Last week I wrote about parsing through catalog item variables including MRVS. We'll take that same script and expand on it here. As we roll through the variables, including our MRVS, we add new HTML lines to our PDF body (as opposed to pages). The code is too long to post here, but it's available in my code snippet library.
You'll notice that we have a lot more than just the opening code.
The first portion of the function is short and covers the items above for PDF creation. The remainder does not only variable value extraction, but also:
- Validates the variable sets to include for which version of the form (remember a compilation of 3 forms into 1).
- Pull the display values for references I've added a fair bit of commentary into the script, but feel free to reach out to me with any questions or recommendations for improvements.
As always, feel free to copy/paste. Let me know if it's helpful or if I can provide any clarification.
https://snscout.blogspot.com/2020/12/pdf-creation-from-catalog-variables.html
