
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
07-12-2021 04:25 AM - edited 08-15-2024 06:42 AM
Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
Hi there,
Struggling with making a PDF export from a record? For example wanting to generate a user agreement, with data from the current record? The default form export to PDF isn't that pretty, and scripting such an export yourself used to be pretty tough or limited looking at layout possibilities.
Used to be! With the Quebec release a nice new API has been introduced, an API which somehow hasn't got that much attention, I missed it also 🙂 An API with methods with which you can easily:
- Dynamically generate a PDF from an HTML string and attach it to a record
- Fill fields in a PDF
- Sign a PDF
- Unflattened, flattened, or partially flattened
- Retrieving PDF field data
In this article, I'll highlight "Fill fields in a PDF".
Pre-Quebec
Having a template PDF with fillable fields and filling it, through for example a UI Action, was already possible pre-Quebec. Dylan Lindgren wrote a nice piece of art on this, "Filling PDF forms in ServiceNow".
Quebec
With Quebec ServiceNow introduced a new API called "PDFGenerationAPI". One of the methods allows you to fill fields in a PDF easier, more robust, and... This can be combined with other methods which I'll write about in a future article.
PDFGenerationAPI fillDocumentFields
Let's just start with sharing working code 😀.
// Define variables
var pdfTemplateSysId = '<sys_id of the template PDF containing fillable fields>',
tableName = current.getTableName(),
recordSysId = current.getUniqueValue(),
pdfName = '<Name of the newly created PDF>';
// Setup field mapping
var fieldMap = {
"company" : "Company X"
"employee" : current.assigned_to.getDisplayValue(),
"ci" : current.category.getDisplayValue(),
"brand" : current.manufacturer.getDisplayValue(),
"model" : current.model_id.getDisplayValue()
};
// Execute API
var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
var result = v.fillDocumentFields(fieldMap, pdfTemplateSysId, tableName, recordSysId, pdfName);
// Log result
gs.info(JSON.stringify(result, null, '\t'));
A successful return would provide you with something like:
{
"attachment_id": "35cc9b6d2f9db010cd5dd99df699b62f",
"message": "Request completed successfully.",
"status": "success"
}
In this example, I'm assuming this code is used on the current record. If that's not the case, do update tableName and recordSysId. Also this example is code used in a UI Action. Though you could go crazy on this! Automating the generation of the PDF, or sending the created PDF by email to the user, uploading the PDF to a server, etcetera. You don't have to limit this to manually executing a UI Action which just attaches a PDF to the current record.
Comparing this code with the Docs page on fillDocumentFields, you'll notice some slight differences. I'd like to see this code as a bit more improved. The code mentioned on the Docs page does work already, unlike the code for one of the other methods ☹️.
What is incorrect on the Docs page, is the description of the Parameters:
- fieldMap is not optional.
- sysId concerns the sys_id of the pdfTemplate (that why I changed this into pdfTemplateSysId).
- tableName is not the table containing the PDF, it's the table on which the newly created PDF will be attached.
- tableSysId is not the sys_id of the record containing the PDF, it's the sys_id of the record on which the newly created PDF will be attached.
- pdfName is optional, if left out the name of the newly created PDF will be the sys_id of the attachment + ".pdf".
Note
We are working with a template PDF with fillable fields. In this case I've added the template PDF to the UI Action. If you would promote your development through an Update Set, by default the attachment will not be included. So that would be a one-time fix in the environment on which you are previewing and committing the Update Set.
Because the template PDF is in the sys_attachment table, when performing system clones to lower environments, by default sys_attachments are excluded. Either you need to change the system clone, or after performing the System Clone adding the template PDF again on the lower environment.
Flatten
Good to mention, the fillDocumentFields method flattens all the fields in the PDF automatically. If you would like a different behavior, that's possible using the fillDocumentFieldsAndFlatten method instead. The basic working is the same, only an additional 6th param is used. This 6th param should contain one of the following values:
- "donot_flatten" - Do not flatten any fields.
- "partially_flatten" - Flatten only the fields which are modified.
- "fully_flatten" - Flattens all the fields.
// Define variables
var pdfTemplateSysId = '<sys_id of the template PDF containing fillable fields>',
tableName = current.getTableName(),
recordSysId = current.getUniqueValue(),
pdfName = '<Name of the newly created PDF>';
// Field mapping
var fieldMap = {
"company" : "Company X"
"employee" : current.assigned_to.getDisplayValue(),
"ci" : current.category.getDisplayValue(),
"brand" : current.manufacturer.getDisplayValue(),
"model" : current.model_id.getDisplayValue()
};
// Flatten document
var flatten = {
"FlattenType" : "partially_flatten"
};
// Execute API
var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
var result = v.fillDocumentFieldsAndFlatten(fieldMap, pdfTemplateSysId, tableName, recordSysId, pdfName, flatten);
// Log result
gs.info(JSON.stringify(result, null, '\t'));
Result
After having transformed a Word document into a PDF with fillable fields and having the PDF attached in ServiceNow (see attachments added to this article), the result of the above code (using a UI Action) would be something like:
In a follow-up article I'll dive into adding an image into your template PDF.
---
And that's it actually. Hope you like it. If any questions or remarks, let me know!
C |
If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.
Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in? |
Kind regards,
Mark Roethof
ServiceNow Technical Platform Architect @ Quint Technology
2x ServiceNow Developer MVP
2x ServiceNow Community MVP
---
- 6,931 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Mark,
As usual, you are awesome in providing us the new info...Similarly, if you could provide us the info on how to create the editable PDF from the word that will be great.
I have been addicted to ur articles BTW...thanks in advance man!!!

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi there,
Thx.
In my next article I can share some extra attention on how to create a fillable PDF. Though it is something that has nothing to do with ServiceNow. You can use for example Adobe Acrobat Pro.
In short really quick, within Adobe (depending on your version) using something like:
Then fields will be automatically generated where possible (for example if your PDF uses underscores like _________, that would be automatically discovered as possible fillable field. If not and wanting to add your own fillable field (or a different type), right mouse click in your PDF:
Pretty basic, though you do need an Adobe Acrobat version which allows you to add fillable fields.
If you like the articles, don't forget to mark them as helpful.
Kind regards,
Mark
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello Mark,
Thanks, the above article is really helpful.
I am too working with the PDF document template and facing few issues, hoping if you have any solution for them.
1. I am trying to Map a HTML field in PDF Template, but when it happens, the data appears with all the Tags, and if I remove the tags, then the formatting is lost.
So is there a way that I could remove the tags without losing the formatting of the text?
2. While mapping the signature Date field, I could see the name of person whose signature date is supposed to be there is the previewed over the date field, although it gets replaced with the date after the document is signed, but it would be really helpful if we could remove it.
Thanks for the help in advance.
Best Regards,
Anubha Datey

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi there,
The image you are sharing is something different than what I wrote this article on. Is what you are referring to within HR or GRC perhaps, and using a Portal?
Kind regards,
Mark
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi,
Yes, it is within HR.
Thanks & Regards,
Anubha Datey

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi!
Is it possible to generate PDF with fillable fields from HTML? For example, i want to generate a PDF document that prefilled by script and that have 2 fields which users fill in external app (Acrobat Reader)
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Mark,
Do you have any suggestions on what I should do if I want to set the font for the fill PDF?
Thanks for the help in advance.
Best Regards

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi there,
That's simply how you set this up in your PDF template. So for example using Adobe Acrobat. This has nothing to do with ServiceNow itself.
Kind regards,
Mark
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Sincere thanks
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Mark,
I have two questions:
1) Is it possible to set style of text in fillable PDF.
2) Can we add pages to fillable pdf?
Best Regards,
Daniel
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello,
Would mind ellaborating on how to fill a radio_button value?
Best,
DJ
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I know this is an old thread, but in reference to Mark's answer to Jaylin1's question about the font of a field in a fillable PDF, it appears ServiceNow has a problem with honoring font properties of editable fields in a PDF when using PDFGenerationAPI fillDocumentFields method.
I created an editable PDF that had both static text and fillable fields. The static text was configured to have 'Times New Roman' font (size 12) and I configured the fields to have the same font (in Adobe Acrobat). I then created a Document Template in ServiceNow, attached the file, parsed it, and used the "PDF Preview" UI Action. The static text in the PDF was Times New Roman but the values in my fields was showing as Arial. Tried other fonts; field values always came back as Arial.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Mark Roethof Thank you for sharing it .. its great.
Is there any way, we could do it in Powerpoint PPT.
For example, I have a UI action - Generate Demand PPT on the Demand Table, where I can select multiple or single records. Based on that I would like to export the data into Powerpoint PPT.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Jamie Stroud Hi Jamie,
Did you ever get a solution to this? I am facing the same issue. It keeps defaulting to Arial even though the PDF's field's font is set to Times New Roman. Thanks.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@zynsn I had submitted a support ticket and, although it's been awhile, I believe the below proposed solution (from ServiceNow) did take care of the problem:
Solution Proposed:
We now have a system "com.snc.pdf.fillpdf.use_assigned_fonts" which is shipped OOB as false.
Functionality of this described below:
This System property is used to determine if the PDF filling process should consider the font associated with the fillable field.
If this system property is set to true, the content of field is validated for proper rendering using the font that is assigned to field. If validation succeeds, the PDF field font is used.
If the validation fails or the system property is set to false, then the system identifies a suitable font and applies it.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Jamie Stroud Thank you for the solution!!! Changing the system property to true allowed the text to honor the PDF field's font property!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@zynsn glad to hear it
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Is there a way to dynamically add form fields? Example, I want to create an invoice that lists materials. There could be 1 to n number of materials for a given invoice.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@NBeheydt I can't think of any way to dynamically add form fields to the pdf. The pdf template pretty much is what it is when you design and upload it as a template. Adding dynamic content to your PDF is a different story. You can have an editable field designed into your PDF template --- perhaps a multi-row textbox or something --- then fill it with a PDF script of dynamically built content. That's not perfect because you'll be constrained by the fixed size of your PDF editable field, but if you have an idea of the size/shape of your likely invoice material list, that might work.
That said, I'd generally say the PDF-style template is better for when the structure of the PDF is pretty well fixed and you just need to fill in some fields dynamically. If you need full dynamic content, I'd go with Text/HTML style template.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello,
Has anyone have luck using the donot_flatten option?
the pdf filler seems to be ignoring this option, and my pdf is locked from any editing.
Thanks for the great article.
Thank you,
j
var flatten = new Object();
flatten["FlattenType"] = "donot_flatten"; //allow for new edits to the pdf, even after filling
//We take the field map, the attachemenid id of the sys attach record of the blank PDF, the table sys id and name of the table that we want to attach the filled out pdf to.
var result = v.fillDocumentFieldsAndFlatten(fieldMap,
inputs.atchSysID,
inputs.tableName,
inputs.tableSysID,
inputs.fileName, flatten);