Export KB Article as PDF

Kasi Ramanathan
Kilo Guru

Hi,

@AnirudhKumar 

Kindly help me  to export KB article as PDF.  I need a button called Export as PDF in kb_article page which will export the repective KB article as PDF.

find_real_file.png

1 ACCEPTED SOLUTION

Pranesh072
Mega Sage
Mega Sage

You can use the same script include provided.

 

Add the following lines in your html and client script on the clone Knowledge Article Content widget

<li class="kb-menu-entry" ><a href="" ng-click="c.exportThisArticle()"><i class="fa fa-print" aria-hidden="true"></i>  Print</a></li>

 

 

c.exportThisArticle=function()
	{
		var ga = new GlideAjax('DownloadPDFCustom');
		ga.addParam('sysparm_name' , 'kbArticleDownload');
		ga.addParam('sysparm_kbID',c.data.article_sys_id);
		ga.getXML(getResponse);
	}


function getResponse(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		window.open(answer);
	}

 

find_real_file.png

 

Script Include 

var DownloadPDFCustom = Class.create();
DownloadPDFCustom.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    kbArticleDownload: function() {
		
		var kbID = this.getParameter('sysparm_kbID');

		var kbContent = '';
        var kb = new GlideRecord("kb_knowledge");
        if (kb.get(kbID)) {
            kbContent = kb.text.toString();
        }
		
		var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
        var result = v.convertToPDF(kbContent, "kb_knowledge", kbID, kb.getValue('short_description')); //updated the title of pdf
		
		return '/sys_attachment.do?sys_id=' + result.attachment_id;
    }

});

https://community.servicenow.com/community?id=community_question&sys_id=72f14c67dbd0bc904819fb243996...

View solution in original post

26 REPLIES 26

I found this incredibly helpful, but I am running into the same issue where my images aren't visible on the PDF document that is rendered using the above script.  Was anyone successful in getting those to come across?  Thank you 🙂

Hi @Nichole Weiland ,

 

Please enter the codes below in the respective Boxes.

Body HTML template:

<button type="button" ng-if="!c.submitted" name="export_kb" class="btn btn-default btn-question" ng-click="c.exportThisArticle()">${Export Article}</button>

Client Controller:

    c.exportThisArticle = function() {
		window.print();
    }

If you need to deal with knowledge blocks you can do something like the following:

kbArticleDownload: function() {
		
		var kbID = this.getParameter('sysparm_kbID');
    var kbContent = '';

    var kbArticle = new GlideRecord('kb_knowledge');
    if(kbArticle.get(kbID)){
      kbText = new global.KBBlock();
      kbContent = kbText.getArticleContent(kbArticle, <users sys_id>);
    }
		
		var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
        var result = v.convertToPDF(kbContent, "kb_knowledge", kbID, kb.getValue('short_description'));
		
		return '/sys_attachment.do?sys_id=' + result.attachment_id;
}

 

Also keep in mind this solution generates a PDF that is attached the knowledge article. You may want to put in a check to see if the PDF is already generated so you don't end up with 100s of the same PDF attached to the article.

I also put in a business rule that deletes the PDFs when a new version of the knowledge article is published.

Doesn't seem like images will be possible based on the information here: https://community.servicenow.com/community?id=community_article&sys_id=2e2e69d71bdaa0d038739979b04bcb84

"Attachments against records won't work unfortunately as the PDF generation API is not authenticated to your instance."

 

I recommend everyone vote on the idea below so we all don't have to create custom code for this:

https://community.servicenow.com/community?id=view_idea&sysparm_idea_id=0e7227f3dbda1410fb115583ca96190c&sysparm_idea_table=x_snc_com_ideation_idea&sysparm_module_id=enhancement_requests

Hello,

I tried this solution and modified the function to not to attach the pdf file to article each time it is exported but use one if already exists. I noticed that export does not carry images for those articles with 'Display attachments' option set to true whereas the images are included in the pdf if the option is not enabled. Has anyone faced a similar scenario and found a solution?

Hi Pranesh 

 

Your code works perfect, but my requirement is the PDF should be similar if we press control + P from the Portal. As this is fetching the data from the kb_knowledge and then again exports the same way how OOB export works from Form view.