I'm trying to create a lambda function that will generate a PDF file from an HTML template file using the pdf-creator-node
package (built on top of html-pdf
). The function is able to find the template just fine, and I have logged the html and verified that it was correct, but when I try to generate the actual PDF I get the following error:
Error: html-pdf: Unknown Error
Error executing phantom at /usr/local/bin/phantomjs
Error: spawn /usr/local/bin/phantomjs ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
at onErrorNT (internal/child_process.js:470:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
events.js:291
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
at doWrite (_stream_writable.js:399:19)
at writeOrBuffer (_stream_writable.js:387:5)
at Socket.Writable.write (_stream_writable.js:318:11)
at Socket.ondata (_stream_readable.js:718:22)
at Socket.emit (events.js:314:20)
at addChunk (_stream_readable.js:297:12)
at readableAddChunk (_stream_readable.js:272:9)
at Socket.Readable.push (_stream_readable.js:213:10)
at Pipe.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on Socket instance at:
at errorOrDestroy (internal/streams/destroy.js:108:12)
at Socket.onerror (_stream_readable.js:754:7)
at Socket.emit (events.js:314:20)
at errorOrDestroy (internal/streams/destroy.js:108:12)
at onwriteError (_stream_writable.js:418:5)
at onwrite (_stream_writable.js:445:5)
at doWrite (_stream_writable.js:399:11)
at writeOrBuffer (_stream_writable.js:387:5)
at Socket.Writable.write (_stream_writable.js:318:11)
at Socket.ondata (_stream_readable.js:718:22) {
code: 'ERR_STREAM_DESTROYED'
}
Here's my code
import pdf from 'pdf-creator-node';
export const generatePdf = (template: string, filename: string) => {
const templateFile = `src/templates/${template}.html`;
try {
// Read HTML Template
const html = fs.readFileSync(templateFile, 'utf8');
const options = {
format: 'A3',
orientation: 'portrait',
height: '900px',
width: '1100px',
phantomPath: 'node_modules/phantomjs-prebuilt/bin/phantomjs',
};
const document = {
html,
data: {
data,
},
path: `${filename}`,
type: '',
};
await pdf.create(document, options);
return true;
} catch (error) {
console.log(error);
throw Error(error);
}
}
The error message makes me think that the phantomjs binary wasn't included in the build, but when I browse my deployment package (downloaded from Lambda), I can see the binary at 'node_modules/phantomjs-prebuilt/bin/phantomjs'
. I find it strange that the error message gives the path usr/local/bin/phantomjs
.
Any help on this is greatly appreciated!