Generated PDF using PDF generation API has margin issue, JPG image seems to be causing it.

ryev_a_
Tera Contributor

I am troubleshooting a case where the generated pdf has margin issue, the content is truncated or cut from the right side. Upon testing same data used, when one image is removed or not included the PDF is generated correctly. 

Maybe you have experienced this kind of issue or if you have any idea if an image can cause such error and how to prevent it. thanks!

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@ryev_a_ 

can you share your code which generates that PDF?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sure, just to give you a brief context, there's a record producer for creating a kb article and once created that kb article will be published along with the PDF version attached to it. The images inserted in the html field in the record producer will also be in the PDF file.

 

here's the part where the PDF generation API is called in the business rule triggered when kb is created.

this is where the header of the pdf processed.

 

var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
   
    var headerImage = null;
    var att = new GlideRecord('sys_attachment');
    att.addEncodedQuery('content_type=image/png^table_sys_id=5e02da82dbf07850347c64d3149619e0');
    att.query();
    if (att.next()) {
        headerImage = getAttachmentBase64(att);
    }

 

 

then this is a snippet where the data html data is formed from the record producer.

 

var htmlContent = '<table border="0" width="994"><tbody><tr><td style="width: 100%; "><img src="' + headerImage + '"/></td></tr></tbody></table><p></p><table border="0" width="100%"><tbody><tr bgcolor="#5B656F"><td style="height: 27px; padding: 5px;" colspan="3"><span style="color: #ffffff; font-family: helvetica;"><strong>INCIDENT: </strong>' + incidentTitle + '</span></td></tr><tr

 

 

and then here's where the PDF is created and attached to the kb and also the function to handle the attachment/image for the header:

 

 
    var html = htmlContent.toString();
    var result = v.convertToPDF(html, table, table_sys_id, filename);

    function getAttachmentBase64(attGR) {
        var base64ImageStr = GlideStringUtil.base64Encode(new GlideSysAttachment().getBytes(attGR));
        gs.info('attachment contnet base64: ' + base64ImageStr);
        return "data&colon;image/png;base64," + base64ImageStr + "";
    }
 
Note that the record producer has HTML fields where the user can directly insert images like the mentioned JPG file in question. when inserted in html field it automatically creates a record in sys_attachment table and then the html tag will contain that sys_id and then once pdf is generated the images can also be seen in the PDF file. Except for the case of this one JPG image (I have attached the exact image here)

@ryev_a_ 

I believe some limitations might be there.

But please check this link

San Diego: PDFGenerationAPI No Longer Respects CSS Paged Media 

OR

Another way is to use HTML document templates and generate PDF using that, try that

Document Templates auto-generate a PDF and attach to case 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

thanks, the system property update did not work and we also cannot use a template as there are existing format they want that we are currently using.