- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2022 12:24 PM
Is there a way to convert attachments to PDF format, specifically image files (jpg, png) and txt files? If not using SN tools and functionality then using JS somehow. Did anyone ever successfully implemented this?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2022 06:48 PM
To convert images to PDF there's two ways that I was able to do it, using PDFGenerationAPI:
var html = '<p><img style="align: baseline;" src="sys_attachment.do?sys_id=' + attachImgID + '" width="auto" height="auto"></p>';
var pdf = new sn_pdfgeneratorutils.PDFGenerationAPI();
// I was able to add the footer
var hfInfo = new Object();
hfInfo["FooterText"] = "0001/2022 - v1 de 02-02-2022";
hfInfo["PageSize"] = "A4";
hfInfo["GeneratePageNumber"] = "true";
hfInfo["FooterTextAlignment"] = "BOTTOM_LEFT";
var result = pdf.convertToPDFWithHeaderFooter(html, "tableName", "recordID", "pdfFileName", hfInfo);
gs.info(JSON.stringify(result));
And using Image e Document APIs:
var pageSize = new sn_pdfgeneratorutils.PdfPage("A4");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);
var image = new sn_pdfgeneratorutils.Image(attachImgID);
//auto scales the image so it fits the PDF
image.setAutoScale(true);
document.addImage(image);
gs.info(document.saveAsAttachment("tableName", "recordID", "fileName.pdf"));
And for txt files:
var gsa = GlideSysAttachmentInputStream(txtID);
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos);
baos.close();
var content = baos + ' ';
//the break lines were missing so I added this
for (var i = 0; i < content.length; i++) {
var match = /\r|\n/.exec(content[i]);
if (match) {
content = content.replace(content[i],'<br>');
}
}
var html = '<p>' + content + '</p>';
var pdf = new sn_pdfgeneratorutils.PDFGenerationAPI();
var hfInfo = new Object();
hfInfo["FooterText"] = "0001/2022 - v1 de 02-02-2022";
hfInfo["PageSize"] = "A4";
hfInfo["GeneratePageNumber"] = "true";
hfInfo["FooterTextAlignment"] = "BOTTOM_LEFT";
var result = pdf.convertToPDFWithHeaderFooter(html, "tableName", "recordID", "myPDF", hfInfo);
gs.info(JSON.stringify(result));
But MS Office attachments I'm still trying 😐

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2022 12:48 PM
Hi Yamilla,
See if this article could help
Generating Custom PDFs - using the new PDFGenerationAPI
Mark Correct or Helpful if it helps.
***Mark Correct or Helpful if it helps.***
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2022 09:14 AM
Hi Yousaf! Thanks for the reply but I don't think the PDFGenerationAPI helps in this case, the PDF content needs to be a HTML string but the attachment content are not stored as HTML.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2022 06:48 PM
To convert images to PDF there's two ways that I was able to do it, using PDFGenerationAPI:
var html = '<p><img style="align: baseline;" src="sys_attachment.do?sys_id=' + attachImgID + '" width="auto" height="auto"></p>';
var pdf = new sn_pdfgeneratorutils.PDFGenerationAPI();
// I was able to add the footer
var hfInfo = new Object();
hfInfo["FooterText"] = "0001/2022 - v1 de 02-02-2022";
hfInfo["PageSize"] = "A4";
hfInfo["GeneratePageNumber"] = "true";
hfInfo["FooterTextAlignment"] = "BOTTOM_LEFT";
var result = pdf.convertToPDFWithHeaderFooter(html, "tableName", "recordID", "pdfFileName", hfInfo);
gs.info(JSON.stringify(result));
And using Image e Document APIs:
var pageSize = new sn_pdfgeneratorutils.PdfPage("A4");
var document = new sn_pdfgeneratorutils.Document.createDocument(pageSize);
var image = new sn_pdfgeneratorutils.Image(attachImgID);
//auto scales the image so it fits the PDF
image.setAutoScale(true);
document.addImage(image);
gs.info(document.saveAsAttachment("tableName", "recordID", "fileName.pdf"));
And for txt files:
var gsa = GlideSysAttachmentInputStream(txtID);
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos);
baos.close();
var content = baos + ' ';
//the break lines were missing so I added this
for (var i = 0; i < content.length; i++) {
var match = /\r|\n/.exec(content[i]);
if (match) {
content = content.replace(content[i],'<br>');
}
}
var html = '<p>' + content + '</p>';
var pdf = new sn_pdfgeneratorutils.PDFGenerationAPI();
var hfInfo = new Object();
hfInfo["FooterText"] = "0001/2022 - v1 de 02-02-2022";
hfInfo["PageSize"] = "A4";
hfInfo["GeneratePageNumber"] = "true";
hfInfo["FooterTextAlignment"] = "BOTTOM_LEFT";
var result = pdf.convertToPDFWithHeaderFooter(html, "tableName", "recordID", "myPDF", hfInfo);
gs.info(JSON.stringify(result));
But MS Office attachments I'm still trying 😐
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 01:24 AM
Hello Yamilla! Were you finally able to convert MS Office attachments also? I'm facing the same requirement 😓