Add an image (signature) in a PDF using Quebec API... - 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 article on how to "Fill fields in a PDF". Though what about adding an image to an existing PDF? Is that possible at all and how? Searching the Community, you'll hardly find any topics or articles on this subject and even none with a detailed working example.

 

In my search for filling fields in a PDF, I came across the new Quebec API's "PDFGenerationAPI" and "PdfMergeSignRequestor API". API's which somehow haven't got that much attention, I missed it also 🙂 API's 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

Sign a PDF, while looking at this method: actually, it's about adding an image to an existing PDF. An image that ServiceNow assumes is a signature that is stored as an image in the sys_attachment table (though actually this could be any image, a picture, a logo a photo, as long as it is in the sys_attachment table!).


PDFGenerationAPI addSignatureMapping

Let's just start with sharing working code 😀.

 

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

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

var pdfSignRequestor = new sn_pdfgeneratorutils.PdfMergeSignRequestor;
pdfSignRequestor.createRequest(params.fillablePdfSysId, params.targetTableName, params.targetRecordSysId, params.targetFilename);

pdfSignRequestor.addSignatureMapping(signature.pageNumber, signature.leftMargin, signature.topMargin, signature.boxWidth, signature.boxHeight, signature.attachmentImageSysId);

var result = pdfSignRequestor.processRequest();

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

 

In a follow-up article, I'll dive into combining filling fields in a PDF and adding an image in a 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?
- 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
Mrman
Tera Guru

@Mark Roethof  This solution you have mentioned is working only for the first time . Please let me now how to dynamically create the signature image whenever user signs on the signature pad.

Mark Roethof
Tera Patron
Tera Patron

Can you explain what you mean with working only for the first time?

Kind regards,
Mark

Rafael Dias1
Tera Contributor

Great post! It helped me a lot.

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