- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2023 07:12 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 12:10 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2023 10:27 AM
It sounds like you are having an issue with some articles not exporting images to PDF when using the script provided. Here are a few things to check:
Verify that the images are properly embedded in the articles. Make sure that the images are not being linked to externally and that they are embedded in the articles using the proper format (e.g. <img src="image.jpg">)
Check that the images are in a format that can be exported to PDF. Some image formats, such as .gif, may not be compatible with the PDF export process.
Verify that the images are being properly referenced in the script. Make sure that the script is correctly identifying and pulling in the images from the articles.
Check if there is any restriction in the script that is preventing the images to be exported. For example, if the script only exports images of certain dimensions or file types, this may be the cause of the issue.
Check if there is any issue with the PDF generator library that you are using. Verify that the library is up to date, and that it is configured correctly to export images.
It would be helpful if you can provide more information about the format and location of the images in the articles, as well as any error messages or issues that you are encountering.
// Define kbID variable to get the sysparm_kbID value
var kbID = this.getParameter('sysparm_kbID');
// Define kbContent variable as empty string
var kbContent = '';
// Define kb variable as new GlideRecord object
var kb = new GlideRecord("kb_knowledge");
// Query the kb table with sysparm_kbID value
if (kb.get(kbID)) {
// Get the number, short_description and text from kb table
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');
} else {
// If the kb record is not found, throw an error
throw "Error: KB article not found with the given sysparm_kbID value.";
}
// Define sysAtt variable as new GlideRecord object
var sysAtt = new GlideRecord('sys_attachment');
// Query the sys_attachment table with table_sys_id and file_name, content_type
sysAtt.addEncodedQuery("table_sys_id=" + kbID + "^file_nameSTARTSWITH" + kb.number + "_" + "^content_type=application/pdf");
if(!sysAtt.query()) {
// If the query returns no results, throw an error
throw "Error: No attachment found with the given sysparm_kbID value.";
}
while (sysAtt.next()) {
// Delete the existing attachment before exporting a new copy
sysAtt.deleteRecord();
}
// Define expt variable as new PDFGenerationAPI object
var expt = new sn_pdfgeneratorutils.PDFGenerationAPI;
// Try to convert the KB article to PDF
try {
var result = expt.convertToPDFWithHeaderFooter(kbContent, "kb_knowledge", kbID, fileName);
return '/sys_attachment.do?sys_id=' + result.attachment_id;
} catch(ex) {
// If the PDF generation fails,
throw "Error: Failed to generate PDF for the KB article. " + ex;
}
In this updated version of the script, I've added error handling to check if the kb record is found or not with the given sysparm_kbID value, and if the query returns no results for the sys_attachment table. If any of these conditions is not met, the script throws an error with a relevant message. Additionally, I've added a try-catch block to handle any errors that may occur during the PDF generation process and throw an error with a relevant message.
Keep in mind that this is just an example and you may need to adapt it to fit your specific use case.
It's also important to test your code thoroughly, to make sure that it works as expected and that it doesn't cause any unintended issues.
Please let me know if you have any question.
Mark as helpful if it solves your issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2023 04:46 PM - edited 01-15-2023 04:53 PM
Thanks for your reply.
I compared the nature of images embedded between an article that exports with the images and with the one that does not. Also compared the source code and both the articles reference to
<img src="/sys_attachment.do?sys_id=xxx />
I tried to reproduce the behavior with a new article.
1. I created a new article and copy/paste the images into article body - Export to PDF - Does not export images.
2. Checked out the same article, removed the images I had pasted earlier and inserted from Attachment by clicking on 'Insert/edit image' tool in the html (Though I deleted the screenshots, the pasted images were still in the attachment table and just selected the same and saved it ).
3. Export to PDF - Exports with images, works perfect.
4. The issue looks like the way the images are added or when the articles are imported. I am not sure now how the authors would have added the images and how is it feasible to re-do the images added in thousands of articles.
Do you have suggestions please?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2023 09:50 PM
Also, just found out that replacing the image url as below fixes the issue. But not sure how does the difference come up in the url and to prevent it from happening. Also require efforts to correct the existing articles (versioning enabled).
Any suggestions please?
<img src="/sys_attachment.do?sys_id=xxx />
to
<img src="sys_attachment.do?sys_id=xxx />
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 12:10 AM
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.