How to generate a custom PDF?

Community Alums
Not applicable

Hi,

I need to have a button on incident. 

When the button is clicked, a PDF must be generated. That PDF must contain details of assigned_to user and some custom text which client has suggested. 

Help me in understanding as to how can I achieve this.

1 ACCEPTED SOLUTION

Hi,


Use this share utility:

https://developer.servicenow.com/app.do#!/share/contents/3429377_pdf_generator?v=1.02&t=PRODUCT_DETAILS

 

Thanks,
Ashutosh

View solution in original post

13 REPLIES 13

Yifeng Zhang
Mega Expert

I just used a third party library, pdfmake, compile pdf in the browser basically.

Community Alums
Not applicable

Hi Yifeng,

I want to write the code in UI action and don't want to use any third part tool.

 

Regards,

Sindhuja

Hi,


Use this share utility:

https://developer.servicenow.com/app.do#!/share/contents/3429377_pdf_generator?v=1.02&t=PRODUCT_DETAILS

 

Thanks,
Ashutosh

Well no, it's not a third party tool per say, it's a library, you import it into your UI action (or UI page then call the page from your UI action)

 

then you can use the library and write your code to code the pdf.

 

so for example, you can define arrays, tables, titles, paragraphs, then put them into a variable called content, then call pdfmake.createpdf(content).open() to generate the pdf in your browser, a new window open with the pdf that user can download.

 

 

unless you meant writing the entire pdf generation library yourself...…:o... then I can't help

Community Alums
Not applicable

Hi Yifeng,

Thank you for giving your time on this.

1. Can you please let me know how to import the library into UI Page, is it some script include which I have to call?

2. As of now I am using GeneralPDF script include to create PDF in UI Action but I am stuck to understand of how can I send my custom html for this function. It is working if I give simple html <p>Hello World</p>

2. Also I have a hard time understanding of how to call UI Page from UI Action? For now I did coding in UI Action itself.

3. My PDF should have table containing the field values. Are you saying that, I have to store this HTML table in javascript variable and call to library function to make it into a PDF? 

i.e.,

Do we have to do something like below and does styling work in this case, i.e.,suppose I want add a border ?

var mycustomHtml = 
'<table>'+
'<tr>'+
'<th>Firstname</th>'+
'<th>Lastname</th>'+
'<th>Age</th>'+
'</tr>'+
'</table>';

 

Below is the code for your reference that I am using to create PDF containing Hello World in UI Action.

var mycustomHtml = '<p>Hello World!</p>';

createPDF(mycustomHtml, //HTML to convert to PDF
'cmdb_ci_computer', //Attach PDF to table
'b4fd7c8437201000deeabfc8bcbe5dc1', //Attach PDF to record/sys_id
'myFile.pdf'); //attachment filename


function createPDF(html, table, sys_id, filename) {
var pdfDoc = new GeneralPDF.Document(null, null, null, null, null, null);
this._document = new GeneralPDF(pdfDoc);
this._document.startHTMLParser();
this._document.addHTML(html);
this._document.stopHTMLParser();
this._saveAs(table, sys_id, filename);
}

function _saveAs(table, sys_id, filename){
var att = new GeneralPDF.Attachment();
att.setTableName(table);
att.setTableId(sys_id);
att.setName(filename);
att.setType('application/pdf');
att.setBody(this._document.get());
GeneralPDF.attach(att);
}

 

So Now instead Hello World I want lot of data, including the field values of cmdb_ci_computer as well as User table. Please let me know how can I achieve this. Please provide some scripts if possible.

 

Regards,

Sindhuja.