Convert attachment to PDF

Yamilla Manjko
Tera Guru

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?

1 ACCEPTED SOLUTION

Yamilla Manjko
Tera Guru

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 😐

View solution in original post

8 REPLIES 8

Yousaf
Giga Sage

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.***

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.

Yamilla Manjko
Tera Guru

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 😐

Hello Yamilla! Were you finally able to convert MS Office attachments also? I'm facing the same requirement 😓