Export a KB article to PDF using DownloadPDFCustom script

Viji2
Tera Expert

Hello,

I have a button on KB article portal page to download the article to pdf. The problem I have is that some articles do not export images to pdf whereas a few are exported with images. There is no difference between these articles in the way the images are embedded.

 

Here is the script I am using. (widget is modified accordingly)

 

kbArticleDownload: function() {

var kbID = this.getParameter('sysparm_kbID');
var kbContent = '';
var kb = new GlideRecord("kb_knowledge");
if (kb.get(kbID)) {
kbContent = kb.number.fontsize(5).bold() + ' : ' + kb.short_description.fontsize(5).bold();
kbContent += "\n\n"+ kb.text.toString();
var fileName = kb.number + '_' + kb.getValue('short_description');
}

var sysAtt = new GlideRecord('sys_attachment');
sysAtt.addEncodedQuery("table_sys_id="+kbID+"^file_nameSTARTSWITH"+kb.number+"_"+"^content_type=application/pdf");
sysAtt.query();
while (sysAtt.next()) {
sysAtt.deleteRecord(); // Each export attaches a pdf to article. Delete the existing attachment before exporting a new copy.
}
var expt = new sn_pdfgeneratorutils.PDFGenerationAPI;
var result = expt.convertToPDFWithHeaderFooter(kbContent, "kb_knowledge", kbID, fileName);

return '/sys_attachment.do?sys_id=' + result.attachment_id;
},

 

Has someone used this feature and got it working with no issues? Can you suggest what could be the issue?

 

Regards,

Viji

 

1 ACCEPTED SOLUTION

I tried replacing html string to desired path before exporting. Using this piece of code before calling the PDFGeneration API in the above script.


var gsa = new GlideSysAttachment();
var agr = gsa.getAttachments('kb_knowledge', kbID);
while (agr.next()) {
kbContent = kbContent.replace(
'<img src="/sys_attachment.do?sys_id=','<img src="sys_attachment.do?sys_id=');
}

 

This is exporting with images. I will do more tests and confirm so that others can make use of this.

View solution in original post

5 REPLIES 5

Do you have the full working code for this?  We have been asked recently to add a Print KBA button to the service portal which is to generate a PDF .