Fill fields AND adding an image (signature) in a P... - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Hi there,

 

You might have read my previous articles on how to "Fill fields in a PDF" and "Add an image (signature) in a PDF". Both possible with the new Quebec API "PDFGenerationAPI". Though can you also do both?

Filling fields in a PDF and adding an image isn't mentioned in the introduction of the "PDFGenerationAPI" on the Developer Site. Though scrolling through the page, I did notice a method called "fillFieldsAndMergeSignature". The name of this method implies you can do both, filling fields in a PDF and adding an image!


PDFGenerationAPI fillFieldsAndMergeSignature

While the previous two explored methods worked almost instantly, getting the fillFieldsAndMergeSignature method up and running took me a bit more effort. Unfortunately, there's no example of coding on the Developer site. The code mentioned… is the wrong code and a copy of a different method. Though reading through the description, how the build-up should be, I managed to create working code myself.

Working code for the fillFieldsAndMergeSignature method:

 

var params = {
    fillablePdfSysId: '<sys_id of the template PDF containing fillable fields>',
    targetTableName: current.getTableName(),
    targetRecordSysId: current.getUniqueValue(),
    targetPdfName: '<Name of the newly created PDF>' + '.pdf'
};

params.fieldMap = {
    company: 'Company X',
    employee: current.assigned_to.getDisplayValue(),
    ci: current.category.getDisplayValue(),
    brand: current.manufacturer.getDisplayName(),
    model: current.model_id.getDisplayValue(),
    date: String(new GlideDate().getByFormat('dd-MM-yyyy'))
};

var signature = {
    pageNumber: 1,
    leftMargin: 35,
    topMargin: 350,
    boxWidth: 180,
    boxHeight: 180,
    attachmentImageSysId: '<sys_id of the image from the sys_attachment table>'
};

var pdfSignRequestor = new sn_pdfgeneratorutils.PdfMergeSignRequestor();
pdfSignRequestor.addSignatureMapping(signature.pageNumber, signature.leftMargin, signature.topMargin, signature.boxWidth, signature.boxHeight, signature.attachmentImageSysId);

var additionalParams = {
    FlattenType: 'fully_flatten' // donot_flatten // partially_flatten // fully_flatten
};

var v = new sn_pdfgeneratorutils.PDFGenerationAPI();

var result = v.fillFieldsAndMergeSignature(params.fieldMap, params.fillablePdfSysId, params.targetTableName, params.targetRecordSysId, pdfSignRequestor, params.targetPdfName, additionalParams);
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.


Note
We are working with a template PDF. 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.


Result

After having transformed a Word document into a PDF 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:
(I used the out-of-the-box signature Image of Don Goodlife for this example)

 

find_real_file.png

----


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?
- Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Kind regards,


Mark Roethof

ServiceNow Technical Platform Architect @ Quint Technology

2x ServiceNow Developer MVP

2x ServiceNow Community MVP

---

LinkedIn

Comments
Dennis2
Kilo Expert

Hi Mark,

GREAT ARTICLE!

Do you know if adding pictures are only available for signatures at the end of pdf?

I'm trying to use this article to convert emails in ServiceNow to PDF but I cannot achieve to keep pictures inside.

Thanks!

Dennis.

zynsn
Tera Expert

Hi Mark,

 

Thank you so much for this. It was an excellent write up! Was able to grab the image of signature from an e-signature and populate it into the PDF.

sowmyaj
Giga Expert

Hi Mark,

 

Thanks for your post. This is helping us but with this, we have additional requirement to add multiple signatures of different approvers on fillable pdf. Any idea how can we do it.  Also, how can we map signatures to the boxes to its right?

sowmyaj_0-1679681595775.png

 

Community Alums
Not applicable

Why don't I see this script include in my instance? I have verified PDF Generator Utils plugin is active in my instance.

Script include used in above code : sn_pdfgeneratorutils 

Version history
Last update:
‎08-15-2024 06:40 AM
Updated by:
Contributors