safeer-pdf-generator
Version:
Framework-agnostic PDF generation library with chunking, merging, S3 upload, and email delivery
119 lines • 4.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultEmailTemplate = exports.defaultPdfOptions = exports.consoleLogger = exports.noOpLogger = void 0;
exports.mergeDefaults = mergeDefaults;
/**
* No-op logger that discards all log messages
*/
exports.noOpLogger = {
debug: () => { },
info: () => { },
warn: () => { },
error: () => { },
};
/**
* Console logger for development/debugging
*/
exports.consoleLogger = {
debug: (msg, ...args) => console.debug(`[PDF-REPORTER:DEBUG] ${msg}`, ...args),
info: (msg, ...args) => console.info(`[PDF-REPORTER:INFO] ${msg}`, ...args),
warn: (msg, ...args) => console.warn(`[PDF-REPORTER:WARN] ${msg}`, ...args),
error: (msg, ...args) => console.error(`[PDF-REPORTER:ERROR] ${msg}`, ...args),
};
/**
* Default PDF generation options
*/
exports.defaultPdfOptions = {
locale: 'en',
filteredColumnKeys: [],
translationFn: (key) => key, // Identity function for no translation
chunking: {
enabled: true,
chunkSize: 100,
maxConcurrency: 2,
},
template: 'default',
includeTotals: false,
email: false,
s3: false,
return: 'buffer',
logoFetch: {
inlineAsBase64: true,
},
timeoutMs: 300000, // 5 minutes
puppeteer: {
headless: true,
protocolTimeout: 600000, // 10 minutes
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--disable-gpu',
'--no-zygote',
'--single-process',
'--disable-software-rasterizer',
'--disable-extensions',
'--font-render-hinting=none',
'--no-first-run',
'--max-old-space-size=4096',
],
},
pdf: {
format: 'A4',
landscape: true,
margin: {
top: '80px',
bottom: '60px',
left: '5mm',
right: '5mm',
},
displayHeaderFooter: false, // Disabled by default to prevent "about:blank"
},
logging: exports.noOpLogger,
};
/**
* Default email template HTML
*/
exports.defaultEmailTemplate = `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2 style="color: #333;">Your PDF Report is Ready!</h2>
<p>Hello{{#user.name}} {{user.name}}{{/user.name}},</p>
<p>Your requested PDF report "<strong>{{title}}</strong>" has been generated successfully.</p>
{{#downloadUrl}}
<p>You can download your report using the link below:</p>
<p><a href="{{downloadUrl}}" style="display: inline-block; padding: 10px 20px; background-color: #3498db; color: white; text-decoration: none; border-radius: 5px;">Download PDF Report</a></p>
{{/downloadUrl}}
<div style="background-color: #f5f5f5; padding: 15px; border-radius: 5px; margin: 20px 0;">
<h3 style="margin-top: 0; color: #555;">Report Details:</h3>
<ul style="margin: 0; padding-left: 20px;">
<li>Title: {{title}}</li>
<li>Generated: {{generatedAt}}</li>
<li>File Size: {{fileSizeMB}} MB</li>
</ul>
</div>
<p>If you have any questions or need assistance, please don't hesitate to contact our support team.</p>
<p>Best regards,<br>{{#company.name}}{{company.name}}{{/company.name}}{{^company.name}}Your Team{{/company.name}}</p>
</div>
`;
/**
* Merge deep objects, with right-hand values taking precedence
*/
function mergeDefaults(defaults, overrides) {
const result = { ...defaults };
for (const [key, value] of Object.entries(overrides)) {
if (value !== undefined) {
if (typeof value === 'object' &&
value !== null &&
!Array.isArray(value) &&
!Buffer.isBuffer(value)) {
result[key] = mergeDefaults(result[key] || {}, value);
}
else {
result[key] = value;
}
}
}
return result;
}
//# sourceMappingURL=defaults.js.map