Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2025 06:28 AM - edited 08-05-2025 07:24 AM
Hi Anila-21,
You may try below steps for converting an Excel file to a PDF:
1. Create any server-side script, for eg, a business rule or ui action on the source table where the attachment is attached and use below script:
try {
// Find the most recent attachment on the current record
var grAttach = new GlideRecord('sys_attachment');
grAttach.addQuery('table_sys_id', current.sys_id + '');
grAttach.addQuery('file_name', 'ENDSWITH', '.xlsx').addOrCondition('file_name', 'ENDSWITH', '.xls');
grAttach.orderByDesc('sys_created_on');
grAttach.query();
while (grAttach.next()) {
// Use GlideExcelParser to read the attachment data
var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
var attachmentStream = attachment.getContentStream(grAttach.sys_id);
parser.parse(attachmentStream);
// Build the HTML string
var htmlContent = '<table border="1" style="border-collapse: collapse; width: 100%;">';
var headers = parser.getColumnHeaders();
// Add table headers
htmlContent += '<tr style="background-color: #f2f2f2;">';
for (var i = 0; i < headers.length; i++) {
htmlContent += '<th style="padding: 8px;">' + headers[i] + '</th>';
}
htmlContent += '</tr>';
// Add table rows with data
while (parser.next()) {
var row = parser.getRow();
htmlContent += '<tr>';
for (var j = 0; j < headers.length; j++) {
var cellValue = row[headers[j]] || '';
htmlContent += '<td style="padding: 8px;">' + cellValue + '</td>';
}
htmlContent += '</tr>';
}
htmlContent += '</table>';
// Generate the PDF from the HTML field
var pdfName = (grAttach.file_name.toString().indexOf('.xlsx')) ? grAttach.file_name.toString().replace(/\.xlsx?$/, '') + '.pdf' : grAttach.file_name.toString().replace(/\.xls?$/, '') + '.pdf';
var pdfApi = new sn_pdfgeneratorutils.PDFGenerationAPI();
pdfApi.convertToPDF(htmlContent, current.getTableName(), current.sys_id, pdfName);
}
action.setRedirectURL(current);
} catch (e) {
gs.error('Excel to PDF Error: ' + e.message + ' At line: ' + e.lineNumber);
}I hope this helps anyone in need of any requirement of converting Excel attachment to PDF.
Please like and mark helpful if it helps.
Best Regards,
Amish Ranjan