PDFCrowd is a cloud-based PDF generation utility that leverages a basic web service API to receive HTML data and produce a PDF rendering of the page. PDFCrowd gives you a handful of free credits to get started and then charges a monthly fee.
While there may be other PDF generators out there, this integration could easily be adapted to be routed to another generator of your choice.
The integration allows you to both preview and download custom pages to a PDF file locally through the browser.
While there is built-in PDF capabilities in the ServiceNow platform, it is tied to table forms and has a set format. For most customers, this is sufficient. However, here are a handful of use cases where a custom PDF capability would be useful:
- Display data from multiple forms in a unified view
- Capture snapshots of a record during different stages of its lifecycle and display those snapshots for the record in a PDF
- Put a custom header and footer on different PDF records you want to export
Setup and Configuration
Download the update set and install it into your instance using the wiki documentation on how to load an update set from an XML file.
Once you have loaded the update set, previewed it, and committed it, an application called “PDF Preview” will be loaded into the instance (See picture below).
Click on the “Settings” link and fill in the information required for the integration.
Designing your own UI Macro
The PDF Generator will leverage “UI Macros” as the renderer for the PDF document.
You have the option to only allow UI Macros that have a “pdfpreview” prefix on the macro name if you want to lock down what kind of macro content can be used to generate PDF files. If you choose to do this, go to the “Settings” link in the PDF Preview application menu and set the “Require the pdfpreview macro prefix” setting to true.
A sample UI Macro is included for you to play with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | <?xml version="1.0" encoding="utf-8" ?> <j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null"> <g:evaluate object='true'> var record = RP.getParameterValue("record"); var gr = new GlideRecordSecure('incident'); gr.get(record); var info=new Object(); info.short_description = gr.short_description; info.number = ""+gr.number; info.journal = ""+gr.comments_and_work_notes.getJournalEntry(-1); info.caller = new Object(); info.caller.name = ""+gr.caller_id.getDisplayValue(); info.caller.email = ""+gr.caller_id.email; info.caller.notification = ""+gr.caller_id.notification.getDisplayValue(); info.caller.location = ""+gr.caller_id.location.getDisplayValue(); info.caller.num_incidents = 0; var incc = new GlideAggregate("incident"); incc.addQuery("active", "true"); incc.addQuery("caller_id", gr.caller_id.sys_id); incc.addAggregate('COUNT'); incc.query(); if(incc.next()){ info.caller.num_incidents = incc.getAggregate('COUNT'); } info; </g:evaluate> <div style='padding: 2px; font-size: 1.3em; font-weight:bold; width:100%; background-color: black; color: white;'>Incident Info</div> <div style='background-color: #EEEEEE; padding-left: 20px; padding-top: 10px;'> NUMBER: ${info.number}<br/> SHORT DESCRIPTION: ${info.short_description}<br/> COMMENTS AND WORKNOTES: <br/> <pre>${info.journal}</pre> </div> <div style='padding: 2px; font-size: 1.3em; font-weight:bold; width:100%; background-color: black; color: white;'>Caller Info</div> <div style='background-color: #FFFFFF; padding-left: 20px; padding-top: 10px;'> NAME: ${info.caller.name}<br/> EMAIL ADDR: ${info.caller.email}<br/> EMAIL NOTIFICATIONS: ${info.caller.notification}d<br/> LOCATION: ${info.caller.location}<br/> TOTAL NUMBER OF OPEN INCIDENTS: ${info.caller.num_incidents}<br/> </div> </j:jelly> |
This UI macro displays a few fields from the incident record as well as some additional information such as the history of the comments and worknotes and some more detailed information about the caller, including the number of open tickets they currently have.
The Sample UI Actions
The integration provides a couple of sample UI Actions that either shows the PDF Preview, or skips the preview altogether and generates the PDF document directly.
The code to these UI Actions can be viewed by personalizing the sample “UI Actions” on the incident form. They are inactive by default. If you want to try it out, you will need to activate them.
When activated, the UI Actions create links to the Incident form:
Here is a sample preview of the demo UI Macro:
If you click the “Generate PDF” button, the HTML will be sent to PDFCrowd and PDFCrowd will return with a PDF file that gets downloaded to your computer.
Coding your own UI Action
UI actions should redirect to the following page: pdf_preview.do
URL Parameters that should accompany that page include:
- macro: the name of the UI Macro to use
- origin: (optional) the originating page that redirected to the preview. This will be the page we return to when someone clicks the “Return” button
- preview: (optional) “true” means we will show a preview of the document first. This is the default action. “false” means we will just generate the PDF and download it to the computer
You can include other URL parameters that may be required by the UI Macro itself as well. In this sample example, the UI Macro requires a record parameter that holds the sys_id to the incident record.
The following is the UI Action script used to to generate the PDF Preview in the sample UI Action provided:
1 2 3 4 5 | action.setRedirectURL("pdf_preview.do?"+ "macro=pdfpreview_demo_incident&"+ "record="+current.sys_id+"&"+ "preview=true&"+ "origin=incident.do?sys_id="+current.sys_id); |
Download
This update set is available on ServiceNow’s “Share Portal”:
PDF Preview for PDFCrowd
John, Your PDF solution worked great. I am trying to pass in and set values for the setPageWidth() and setPageHeight(). Any ideas on how to accomplish this? I need to change the PDF output to landscape.
Hmmm – this doesn’t work so well with responsive pages – why didn’t you use http://pdfmyurl.com?
@Jack – Because from a mobile you still want the same layout on the PDF?
Are you just spamming?
Hi, When i clicked on “Generate PDF” it is not working, PDF not generated. i just wondering about this.
Let me what was the issue here please
Hi,
Where can I find the update set?
I need help on Jira Integration as it does not work, I’m not sure of the setting part. Please help
the update set link seems not working, please assist.
Hi John,
Preview PDF is working fine. But the Generate PDF is not doing anything. It is a blank page with return button. What could be the issue here?