const domo = require('sdk');
const { PDFDocument, StandardFonts, rgb } = require('pdf-lib');
/**
* Removes Unicode characters not supported by PDF Standard Fonts
* - Decomposes accented characters (é → e)
* - Removes remaining non-ASCII characters
*/
function sanitizeForPdf(text) {
if (!text) return '';
return text
.normalize('NFKD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^\x20-\x7E]/g, '');
}
/**
* Convert Outlook-safe HTML to PDF and upload to Domo Files
* using the two-step reliable upload method
*/
async function Convert_and_Export_HTML_To_PDF(html, fileName) {
/*************************************************
* 1. NORMALIZE + SANITIZE HTML
*************************************************/
let input = sanitizeForPdf(
html
.replace(/\r?\n/g, '')
.replace(/ /gi, ' ')
.replace(/<\/p>/gi, '\n\n')
.replace(/
/gi, '\n')
.replace(/<(style|script)[^>]*>.*?<\/\1>/gi, '')
.replace(/<\/?(div|span)>/gi, '')
);
/*************************************************
* 2. CREATE PDF
*************************************************/
const pdfDoc = await PDFDocument.create();
let page = pdfDoc.addPage([612, 792]);
const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
const boldFont = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
const margin = 50;
let y = page.getHeight() - margin;
const newPageIfNeeded = () => {
if (y < margin) {
page = pdfDoc.addPage([612, 792]);
y = page.getHeight() - margin;
}
};
const drawText = (text, size = 11, bold = false) => {
const safeText = sanitizeForPdf(text);
if (!safeText) return;
newPageIfNeeded();
page.drawText(safeText, {
x: margin,
y,
size,
font: bold ? boldFont : font,
maxWidth: 512,
lineHeight: 14,
color: rgb(0, 0, 0),
});
y -= 14;
};
/*************************************************
* 3. TABLE PARSING (OUTLOOK CORE FEATURE)
*************************************************/
const tableRegex = /