/** * A [wkhtmltopdf](https://wkhtmltopdf.org) wrapper for AWS Lambda. * @param {string} html the html content * @param {any} options available at [wkhtmltopdf's website](https://wkhtmltopdf.org/docs.html) * @param {string} rootDir rootDir to use instead of the default one to locate `wkhtmltopdf` */ export function html2PDF(html: string, options: any, rootDir?: string): Promise { if (process && process.env) process.env.PATH = process.env.PATH + ':' + process.env.LAMBDA_TASK_ROOT; let exePath = `${rootDir || '.'}/node_modules/aws-wkhtmltopdf/wkhtmltopdf`; options = options || []; return new Promise((resolve, reject) => { let bufs: Array = []; var proc = require('child_process').spawn('/bin/sh', [ '-o', 'pipefail', '-c', exePath + ' ' + options.join(' ') + ' - - | cat' ]); proc .on('error', (error: Error) => reject(error)) .on('exit', (code: string) => { if (code) reject(new Error(`Exited with code ${code}`)); else resolve(Buffer.concat(bufs)); }); proc.stdin.end(html); proc.stdout.on('data', (data: any) => bufs.push(data)).on('error', (error: Error) => reject(error)); }); }