Need help generating a PDF and attaching to a record.

Jeffrey Siegel
Mega Sage

I have a custom table u_pco.  I created a widget and a service portal page to display the contents of the record in a letter format.  I currently have a UI action thats working nicely to display and send the print command for the url.  however, i have a need to have a button to generate a PDF of the page and attach it to the record, so that it can be sent out via an email notification.  

Does anyone know how i can adjust this print client script to generate a pdf of the service portal page and attach it to my current record without user intervention (except clicking of the button to trigger)

 

my UI Action script:

function PrintPCOLetter(){
if(g_form.modified){
		alert("Please save the form before continuing");
		return false;
	}
	var url = 'sp?id=pc_letter&sys_id='+g_form.getUniqueValue()+"&print=false";
	window.open(url, "_self");
	//g_navigation.openPopup(url);
	return false;
}

about this simple script: the script, ensures that the form isnt "dirty before allowing to proceed"

then sets the URL to goto the service portal page "pc_letter" and grabs the sys id and print parameter that the widget is using to a) get the desired record, and the print=false advises to show the letter, not print it using system dialog once loaded. (if print=true then the system dialog will automatically send the page to the system print dialog, allowing the user to choose which printer and see a preview)

 

 

9 REPLIES 9

In that case, you can have additional button in your service portal widget for user to add this letter as PDF in record. Otherwise you have keep to HTML sync in both place

 

<div id="container">

    <div>Testing</div>
   
</div>

 <button ng-click="c.attachPDF()">
        Attach to record
    </button>



Client script:

c.attachPDF = function(){
		var completeHTML = $("#container").html();
                call server script to generate PDF from API
		
	};


Thanks, can you help me figure out the syntax of the server script and how to call it, im a bit lost...

Please find the details below. Mark it answered, if its solved your issue.

HTML:

<div id="container" ng-model="myApp" ng-controller="MyCtrl">
    <div>Testing</div>
</div>

<button ng-click="c.attachPDF()">Attach PDF</button>


Client Script:
c.attachPDF = function(){
		var html = $("#container").html(); // "test_1" replace your widget id
		spUtil.get("test_1", {"generatePDF": true, "html": html}).then(function(response) {
    
});
	};


Server Script:

	if (input.generatePDF){
		
		var v = new sn_pdfgeneratorutils.PDFGenerationAPI;

    var hfInfo = {};
 

    var result = v.convertToPDFWithHeaderFooter(input.html, "incident", $sp.getParameter("sys_id"), "myPDF", hfInfo);
    
		
	}

Hi, 

 

I am Kinda stuck with a similar thing. I created a UI action to generate PDF in my form. in that UI action, I wrote a script to generate the pdf from an HTML template which I created and attached to the same record. 



var html = current.html_script_body;
    var UpdatedHTML = html.replace('&nbsp', current.getDisplayName('Invoice'));
    var tableName = 'Invoice';
    var targetTableSysId = current.getUniqueValue();
    var fileName = current.getDisplayName('Invoice');

    var v = new sn_pdfgeneratorutils.PDFGenerationAPI;

    var result = v.convertToPDF(UpdatedHTML, tableName, targetTableSysId, fileName);
    gs.info(JSON.stringify(result));

I have used this script added in my UI action button of Generate PDF, But when I clicked it its not generating and adding the pdf to the record. 
If anyone can help?
 

duaanuj
Tera Contributor

You can create fillable PDF template and then map the field in the code. 

where '55c622189702211057763dafe153af70' is the sys_id of the attachment (fillable PDF revision stored in dms_document_revision table in my case) 

 

var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
var result = v.isDocumentFillable('55c622189702211057763dafe153af70'); // check if document is fillable pdf
var result1 = v.getDocumentFields("55c622189702211057763dafe153af70"); // list fields in fillable pdf.
var fieldMap = new Object();
var flatten = new Object();
flatten["FlattenType"] = "partially_flatten";
fieldMap["EmployeeName"] = "anuj dua";
var result2 = v.fillDocumentFieldsAndFlatten(fieldMap, "55c622189702211057763dafe153af70", "sys_user", "6816f79cc0a8016401c5a33be04be441", "pdfName", flatten);

 

Refer below link for more details

PDFGenerationAPI API | ServiceNow Developers