- 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
04-15-2025 01:32 AM
Hello Yamilla! Were you able to also convert MS Office attachments? I'm facing the same requirement here
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 06:54 AM
Hi, Angel! Only using the MS Graph API Download file in another format.
I had to build a custom integration in order to accomplish this functionality.
To use this API, I had to send the file to MS One Drive, pass the item id on One Drive as one of the parameters as the API documentation explains, convert it to PDF and then download the file back to SN.
Some important points are that the client needs to have the appropriate MS licences, and that this doesn’t work to protected or encrypted files.
In the end, the client was convinced that the easier way to go was to limit the file format that the users would upload to only pdf, this way the work of converting the file to pdf was on the user.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2023 09:27 PM
If you don't see Programs, choose Default Programs > Associate a file type or protocol with a program. In the Set Associations tool, select the file type you want to change the program for, then choose Change program. Once you've chosen the new program to use to open that file type, choose OK.
Regards,
Will
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 03:13 AM
Yes there is an amazing website that converts PDF files very easily try out these PDF Conversion Tools