Akash Rajput Technology over a cup of coffee

Next-Gen PDF Generation – Part 2

5 min read

In last blog, we learned about Next-Gen PDF Generation – Conversion API. In this part, we are playing with one more option available – Utility API. To work with examples, you need to download the update-set from Part 1 of the blog.

Utility API

API group to work with fillable PDF. Below is the basic syntax to use this API:

var resultX = new sn_pdfgeneratorutils.PDFGenerationAPI;

Below are the function available in this Utility API.

  1. isDocumentFillable ➔ To check if PDF is fillable returns T/F

Basic code syntax to use this API:

var isFillable = resultX.isDocumentFillable(<<sys_id_of_fillable_pdf_attachment>>);
gs.info(JSON.stringify(isFillable));

I’ve attached one sample fillable pdf on record PDFC0001002, but you can use any fillable pdf. To see it in action, Go to fix script or background-script and run below code:

var _fillable_pdf_attachment_sys_id = 'acada2ae1b76ec50abbdbb7c0a4bcbd1';
var resultX = new sn_pdfgeneratorutils.PDFGenerationAPI;
//check if document is fillable
var isFillable = resultX.isDocumentFillable(_fillable_pdf_attachment_sys_id);
gs.info(JSON.stringify(isFillable));

It will give you output:

{
  "message":"Request completed successfully.",
  "document_editable":"true",
  "status":"success"
}

property document_editable true indicates that our attached document is fillable.

  1. getDocumentFields ➔ Return all pdf fillable fields with field names

Basic code syntax to use this API:

var docFields = resultX.getDocumentFields(<<sys_id_of_fillable_pdf_attachment>>);
gs.info(JSON.stringify(isFillable));

To see it in action, run below script:

var _fillable_pdf_attachment_sys_id = 'acada2ae1b76ec50abbdbb7c0a4bcbd1';
var resultX = new sn_pdfgeneratorutils.PDFGenerationAPI;
//sys_id to get all fillable field type....@include fillable attachment sys_id
var docFields = resultX.getDocumentFields(_fillable_pdf_attachment_sys_id);
gs.info(JSON.stringify(docFields));

Above script can be found on sample fix script Sample HTML to PDF with Fillable PDF included in update-set. Once run, it will give you JSON object as output which contain all the fillable pdf fields. It will be something like below:

{
  "message":"Request completed successfully.",
  "fields":[
    "Given Name Text Box",
    "Family Name Text Box",
    "House nr Text Box","Address 2 Text Box","Postcode Text Box",
    "Country Combo Box","Height Formatted Field","City Text Box",
    "Driving License Check Box","Favourite Colour List Box",
    "Language 1 Check Box","Language 2 Check Box","Language 3 Check Box",
    "Language 4 Check Box","Language 5 Check Box","Gender List Box","Address 1 Text Box"
  ],
  "status":"success"
}
  1. getDocumentFieldsType ➔ Return all pdf fillable fields with field names and types associated

Basic code syntax to use this API:

var docFields = resultX.getDocumentFieldsType(<<sys_id_of_fillable_pdf_attachment>>);
gs.info(JSON.stringify(isFillable));

To see it in action, run below script:

var _fillable_pdf_attachment_sys_id = 'acada2ae1b76ec50abbdbb7c0a4bcbd1';
var resultX = new sn_pdfgeneratorutils.PDFGenerationAPI;
//sys_id to get all fillable field type....@include fillable attachment sys_id
var docFields = resultX.getDocumentFieldsType(_fillable_pdf_attachment_sys_id);
gs.info(JSON.stringify(docFields));

Above script can be found on sample fix script Sample HTML to PDF with Fillable PDF included in update-set. Once run, it will give you JSON object as output which contain all the fillable pdf fields. It will include something like below:

It will give you each and every field information including its name, type and value(s) and on which page no that field is.

  1. fillDocumentFields ➔ Fill the document with object data values to respective fields and produce flatten pdf i.e. static pdf, no editing after PDF generation

Basic code syntax to use this API:

var pdfOutput = resultX.fillDocumentFields(
  					<<Object_containing_field_value>>,
                    <<sys_id_of_fillable_pdf_attachment>>,
                    <<table_name>>,
                    <<sys_id_of_record_where_pdf_need_to_attach>>,
					<<pdf_fiile_name_without_.pdf_extension>>);
gs.info(JSON.stringify(pdfOutput));

To see it in action, run below script or go to fix script Sample HTML to PDF with Fillable PDF and uncomment appropriate script line:

var _fillable_pdf_attachment_sys_id = 'acada2ae1b76ec50abbdbb7c0a4bcbd1';
var resultX = new sn_pdfgeneratorutils.PDFGenerationAPI;

//Flattening pdf - non modifiable
var mymap = new Object();
//[<<give field name from abve script>>]
mymap["Given Name Text Box"]="Akash";

//sys_id to get all fillable field type....@include fillable attachment sys_id
var pdfOutput = resultX.fillDocumentFields(mymap,
                                           _fillable_pdf_attachment_sys_id,
                                           "incident",
                                           "fbe0e0a51bc7e0508e1e2fc42a4bcb05",
                                           "fillable sample pdf demo with flattening");
gs.info(JSON.stringify(pdfOutput));

Once run, you will see one attachment on above incident record. Open it and output pdf will contain our input value in result pdf.

  1. fillDocumentFieldsAndFlatten ➔ Produce non-flattening or partial flattening PDF after processing

You must have noticed one thing in above pdf that once downloaded, you are not able to edit other fillable field. This is known as flattening. By default, once you generate output, the resulted pdf will be flattened. To change this way, we have fillDocumentFieldsAndFlatten function to work with.

Basic code syntax to use this API:

var pdfOutput = resultX.fillDocumentFieldsAndFlatten(
  					<<Object_containing_field_value>>,
                    <<sys_id_of_fillable_pdf_attachment>>,
                    <<table_name>>,
                    <<sys_id_of_record_where_pdf_need_to_attach>>,
					<<pdf_fiile_name_without_.pdf_extension>>,
					<<parms_to_hold_properties>>);
gs.info(JSON.stringify(pdfOutput));

To see it in action, run below script or go to fix script Sample HTML to PDF with Fillable PDF and uncomment appropriate script line:

var _fillable_pdf_attachment_sys_id = 'acada2ae1b76ec50abbdbb7c0a4bcbd1';
var resultX = new sn_pdfgeneratorutils.PDFGenerationAPI;

//Flattening pdf - non modifiable
var mymap = new Object();
mymap["Given Name Text Box"]="Akash";

//To make non-flattening
var _parms = new Object();
_parms['FlattenType']="donot_flatten";

//sys_id to get all fillable field type....@include fillable attachment sys_id
var pdfOutput = resultX.fillDocumentFieldsAndFlatten(mymap,
                                                     _fillable_pdf_attachment_sys_id,
                                                     "incident",
                                                     "beaf96fc1b0aec50abbdbb7c0a4bcbc3",
                                                     "fillable sample pdf demo with non-flattening",
                                                     _parms);
gs.info(JSON.stringify(pdfOutput));

Once run, you will see one more attachment on above incident record. Open it and output pdf will contain our input value in result pdf and now all fields are editable.

What-If, you want only those fields should be editable for which you have not provided values. In that case we have to modify our parms object to have FlattenType as partially_flatten instead of donot_flatten.

Once changed, we will have our desired output having those fields as editable where we have not provided any value while generating pdf.

  1. addSignatureMapping ➔ Merge signature image in pdf as the final output

Basic code syntax to use this API:

var resultPDFSign = new sn_pdfgeneratorutils.PdfMergeSignRequestor;
resultPDFSign.createRequest(
                    <<sys_id_of_pdf_attachment_to>>,
                    <<table_name>>,
                    <<sys_id_of_record_where_pdf_need_to_attach>>,
					<<pdf_fiile_name_without_.pdf_extension>>,
					);
resultPDFSign.addSignatureMapping(<<page_no>>,<<x_coordinates>>,<<y_coordinates>>,
                                  <<desired_width_of_signature>>,<<desired_height_of_signature>>
                                  ,<<sys_id_of_signature_image_attachment>>);
var result = resultPDFSign.processRequest();
gs.info(JSON.stringify(result));

To see it in action, run below script or go to fix script Sample HTML to PDF with signature:

var resultPDFSign = new sn_pdfgeneratorutils.PdfMergeSignRequestor;
resultPDFSign.createRequest("8d7f2e621bb6ec50abbdbb7c0a4bcb50",
                "incident","beaf96fc1b0aec50abbdbb7c0a4bcbc3","PDF_demo_with_sign.pdf");

resultPDFSign.addSignatureMapping(1,300,500,150,150,"d8f773131b76e0108e1e2fc42a4bcb8b");
var result = resultPDFSign.processRequest();
gs.info(JSON.stringify(result));

Output pdf will contain signature as part of pdf. It will be something like below:

That’s it for this long post. Try it with different example and do let me know in case you are facing any issue. In the next post, we are going to see some next level of apis.


Akash Rajput Technology over a cup of coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

Never miss a story from me, get updates directly in your inbox.
Loading