Steps to Generate PDF from the script

  1. Create a table General Document Template(u_general_document_template) – Which will contains all the script
  2. Create a table PDF Script(u_pdf_script) – Which will contains all the custom pdf script
  3. Create a script include PDFUtil – that can generate html based on template and create pdf
  4. Call script Include form workflow or UI Action.

Table : General Document Template

Table have 4 fields. Number(u_number), Name(u_name), Active(u_active) and Body(u_body)

See a sample screenshot of the above table

Table: PDF Script

PDF Script table have 4 fields Number(u_number),Name(u_name),Active(u_active),Script(u_script)

see a sample record screenshot for above table

Sample Script for PDF Script:

answer = (function runScript(current) {
    var gr = new GlideRecord('sc_req_item');
    gr.addEncodedQuery('sys_id=' + current.sys_id);
	gr.query();
    if (gr.next()) {
		var out=gr.variables.services_payment_firm+'-'+gr.variables.services_payment_vendor;
		var location = gr.variables.services_payment_location;
		if(location != '')out=out+'-'+location;
        return  out;
    }
})(current);

Script Include: PDFUtil

var PDFUtil = Class.create();
PDFUtil.prototype = {
    initialize: function(template) {
        this.body;
        this.getTemplate(template);
    },
    getTemplate: function(template) {
        var grT = new GlideRecord('u_general_document_template');
        grT.addEncodedQuery('u_number=' + template + '^ORu_name=' + template + '^u_active=true');
        grT.query();
        if (grT.next()) {
            this.body = grT.getValue('u_body');
        } else {
            gs.log('PDF: Template not found');
        }
    },
    generatePDF: function(targetTable, record) {
        var gr = new GlideRecord(targetTable);
        gr.addEncodedQuery('sys_id=' + record + '^ORnumber=' + record);
        gr.query();
        if (gr.next()) {
			var pdf_scripts = this.body.match(/\$\{pdf_script:.*?\}/g);
            if (pdf_scripts) {
                var pdfScriptNames = '';
                for (var i = 0; i < pdf_scripts.length; i++) {
                    if (i == 0)
                        pdfScriptNames += pdf_scripts[i].substring(13, pdf_scripts[i].length - 1);
                    else
                        pdfScriptNames += ',' + pdf_scripts[i].substring(13, pdf_scripts[i].length - 1);
                }
                pdfScriptNames = pdfScriptNames.split(',');
                for (var s in pdfScriptNames) {
                    var ps = '${pdf_script:' + pdfScriptNames[s] + '}';
                    this.body = this.body.replace(ps, this.evalScript(pdfScriptNames[s], gr));
                }
            }
			
            var html = this.body.match(/\$\{.*?\}/g);
            for (var i = 0; i < html.length; i++) {
                var temp = html[i];
                temp = temp.replace('${', '');
                temp = temp.replace('}', '');
                var field = 'gr.' + temp + '.getDisplayValue()';
                var value = eval(field);
                this.body = this.body.replace(html[i], value);
            }
            var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
            var result = v.convertToPDF(this.body, targetTable, record, gr.getDisplayValue());
            // gs.log('PDF: Complete - ' +JSON.stringify(result));
        } else {
            gs.log('PDF: Target record nor found check generatePDF function');
        }
    },
    evalScript: function(scriptName, rec) {
        var gr = new GlideRecord('u_pdf_script');
        //gr.addQuery('u_name', scriptName);
		//gr.addQuery('u_active',true);
		gr.addEncodedQuery('u_active=true^u_name='+scriptName+'^ORu_number='+scriptName);
        gr.query();
        if (gr.next()) {
            var vars = {
                'current': rec,
                'answer': null
            };
            var evaluator = new GlideScopedEvaluator();
            evaluator.evaluateScript(gr, 'u_script', vars);
            return evaluator.getVariable('answer');
        }else return '';
    },
    type: 'PDFUtil'
};

Call Script Include from UI Action or Workflow

var t = new PDFUtil('PDF0001005');
t.generatePDF('sc_req_item',current.sys_id);

You can directly download XML FIle and use our code:

Total Views: 1334

No responses yet

Leave a Reply

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