Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Add signature image to fillable PDF

stryker129
Mega Guru

Hi guys,

I've a 'generated_new_unsigned.pdf' file which is attached to sys_user table (for test),

Also I've a signature (activated through Signature Pad plugin) with the following sys_id: 8fa2a193db8003006b2e7b1ebf9619c6

Now I want to add mentioned signature to PDF.

For this purpose I walked through "hr_PdfUtils" script include which comes from HR module plugin.

So, here is my code snippet which doesn't work (sys_id from the last line of code returned as undefined).

var a = new global.GeneralPdfUtils();

var user_table = 'sys_user';

var admin_user_sysid = '6816f79cc0a8016401c5a33be04be441';

var pdf_template_sysid = 'c3d9ce1fdb8003006b2e7b1ebf961943';

var prefilled_pdf_sysid = '146cca9fdbc003006b2e7b1ebf96193a';

var signature_image_sysid = '8fa2a193db8003006b2e7b1ebf9619c6';

var sig = new GlideRecord("signature_image");

sig.get(signature_image_sysid);

var jsonArrayString = '';

var validatorJson = {};

var fl = [];

var mappingInfo = {};

mappingInfo.pageNumber = 0;

mappingInfo.docTop = 30;

mappingInfo.docLeft = 30;

mappingInfo.signTop = 30;

mappingInfo.signLeft = 30;

mappingInfo.boxHeight = 100;

mappingInfo.boxWidth = 100;

mappingInfo.mappingField = "";

fl.push(mappingInfo);

var mappingInfoJson = {};

mappingInfoJson["signatureMapping"] = fl;

mappingInfoJson["dateMapping"] = [];

jsonArrayString = JSON.stringify(mappingInfoJson);

validatorJson.signatureImageSysId = signature_image_sysid;

validatorJson.isAllSignaturesCollected = true;

var jsonStr = JSON.stringify(validatorJson);

var sysid = a.mergeImageToPdf(user_table, admin_user_sysid, jsonArrayString, prefilled_pdf_sysid, 'generated_new_signed.pdf', jsonStr);

Has anyone faced with this before? If so - why 'mergeImageToPdf' method from last line of this code returns undefined? Probably some parameter is wrong?

5 REPLIES 5

icesinker
Kilo Explorer

Ivan did you resolve the issue? We have similar problem here. Thanks.


milapshah15
Kilo Expert

@stryker129

Have you found a solution for this?


Warm regards

Milap

stephanierose
Kilo Explorer

I FINALLY got this working by adding validatorJson.generateDocument = false;

T Pappu
Kilo Contributor

Hi stephanierose-

 

I have a similar requirement where i have to add a signature image to a pdf. Can you please assist in this regard?

Here is my code:

 

(function executeRule(current, previous /*null when async*/) {

// Add your code here
var gd = new GlideDate();
gd.setValue(current.effectivedate);
var effDate = gd.getByFormat("yyyyMMdd");
gd.setValue(current.level_9_approval_date);
var dirAppDate = gd.getByFormat("yyyyMMdd");
var gDateTime = new GlideDateTime();
var gDate = gDateTime.getDate();
var datePart = gDate.getByFormat('yyyyMMdd');
var timePart = gDateTime.getLocalTime().getByFormat('hhmmss');

//TEMPLATE UPLOADED THROUGH MANAGED DOCUMENTS->REVISIONS
var attachmentSysId = "330e812b2162001053f7de03e461d965";

// GET SYS ID FOR SIGNATURE IMAGE
var imageSysId = "14303b802df6001053f78e4799a3cfb6";


var destinationTableName = 'x_form';
var destinationTableSysId = current.getUniqueValue();
var jsonObject =
{"eff_date":effDate,
"xdist_app":"",

"aaccode":current.aaccpde.getDisplayValue(),
"grade_config":current.current_grade_configuration.getDisplayValue(),
"dodea_date":dirAppDate,
"xpurpose":"",
"staffing":current.staffing.getDisplayValue(),

//first signature, the fields are the form fields in the fillable PDF
"dist_sign":"",
"rmo":current.rmo.getDisplayValue(),
"installation":current.installation.getDisplayValue(),
"attends":current.attend_feeder_plan.getDisplayValue(),
"area_date":"",
//"Reset":"",
"addr3":current.billing_address.getDisplayValue(),
"budget":current.budget_impact_analysis.getDisplayValue(),
"addr2":current.freight_address.getDisplayValue(),
"hro":current.hro.getDisplayValue(),
"coord":current.military_installation.getDisplayValue(),
"documentation":current.documentation.getDisplayValue(),
"app":"",
"rationale":current.rationale.getDisplayValue(),
"schoolname":current.name_change.getDisplayValue(),
"xarea_app":"",

//second signature
"area_sign":"",

"addr4":current.commercial_address.getDisplayValue(),
"dep":current.dep.getDisplayValue(),
"addr1":current.mailing_address.getDisplayValue(),
"district":current.school_district.getDisplayValue(),
"dist_date":"",
"location":current.location.getDisplayValue(),

//third signature
"sign2":"",

"enroll":current.current_enrollment.getDisplayValue(),
"receives":current.receive_feeder_plan.getDisplayValue(),
"facilities":current.facilities.getDisplayValue(),
"code":current.code.getDisplayValue(),
"support":current.support_agreement.getDisplayValue()};

var pdfName = current.number + "_"+ datePart + '_' + timePart + "_Form.pdf";
var pdf = new global.GeneralPdfUtils();
var jsonString = JSON.stringify(jsonObject);
//pdf.prefillPdf(jsonString, destinationTableSysId, attachmentSysId, destinationTableName, pdfName);
//mergeImageToPDF(documentTableName, documentTableSysId, jsonArrayString, attachmentSysId, pdfName and signatureImage)
gs.addInfoMessage("jsonString "+jsonString);
var sig = new GlideRecord("signature_image");
sig.get(imageSysId);
gs.addInfoMessage("after getting signature");
var validatorJson = {};
var jsonArrayString = '';
var fl = [];
var mappingInfo = {};
mappingInfo.pageNumber = 0;
mappingInfo.docTop = 30;
mappingInfo.docLeft = 30;
mappingInfo.signTop = 30;
mappingInfo.signLeft = 30;
mappingInfo.boxHeight = 100;
mappingInfo.boxWidth = 100;
mappingInfo.mappingField = "";
fl.push(mappingInfo);
gs.addInfoMessage("second part..."+mappingInfo);
var mappingInfoJson = {};
mappingInfoJson["signatureMapping"] = fl;
mappingInfoJson["dateMapping"] = [];
jsonArrayString = JSON.stringify(mappingInfoJson);

validatorJson.dist_sign = imageSysId;
validatorJson.isAllSignaturesCollected = true;
validatorJson.generateDocument = false;
var jsonStr = JSON.stringify(validatorJson);
gs.addInfoMessage(mappingInfoJson);
gs.addInfoMessage(jsonStr);
pdf.mergeImageToPdf(destinationTableName, destinationTableSysId, jsonArrayString, attachmentSysId, pdfName, jsonStr);

})(current, previous);

 

From the code above, the prefillPdf works without the signature. The fillable PDF has three areas where the signature of 3 people have to go. I have the signatures captured in signature images.

When i tried to add these image to the fillable PDF, the mergeImageToPDF does not work. This is a custom table and not tied to any task/incident and i did not see any documentation on the mergeImagetoPDf on how we can add image to a fillable pdf.

Could you shed light on the mappingInfo JSON structure and how do i map the 3 signatures areas to signature images?

Thanks,

TK