node-html-pdf: Error deploying to Vercel / AWS Lambda
In my PC (Kubuntu 20.04) it works perfectly. The problem appears when I deploy to Vercel. To begin with, I need to set phantomPath, otherwise I get a “write EPIPE” error:
pdf.create(html, {
phantomPath: path.resolve(
process.cwd(),
"node_modules/phantomjs-prebuilt/bin/phantomjs"
),
})
Now, if I run it, it throws:
html-pdf: Received the exit code '1'
Error executing phantom at /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs
Error: spawn /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19)
at onErrorNT (internal/child_process.js:469:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
events.js:287
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:695:22)
at Socket.emit (events.js:310:20)
at addChunk (_stream_readable.js:286:12)
at readableAddChunk (_stream_readable.js:268:9)
at Socket.Readable.push (_stream_readable.js:209:10)
at Pipe.onStreamRead (internal/stream_base_commons.js:186:23)
Emitted 'error' event on Socket instance at:
at errorOrDestroy (internal/streams/destroy.js:108:12)
at Socket.onerror (_stream_readable.js:729:7)
at Socket.emit (events.js:310: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:695:22) {
code: 'ERR_STREAM_DESTROYED'
}
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 5
- Comments: 27
I forgot to mention it, but I found a (hacky) solution:
First, you add a
fonts.confwherever you have your fonts (remember to change thedir):Then, you download these libs (the same from here, but in a ZIP) and put them in a folder. I put them in
/bins.Finally, before you run
pdf.create(), set these env vars:Dev Mode
If your PDF throws some error in Dev mode, you can set the env vars only in production mode. Like this:
Also, remember to change the
phantomPath!!!I ended up using puppeteer-- was really straight forward.
Borrowed a little from here https://github.com/vercel/vercel/discussions/4903#discussioncomment-234166
Perfect! I edited the comment for new people. Now it should be right
Oh, wait. I think I was wrong about the
phantomPath. Try this:node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs(sorry for no answering sooner, my PC crashed)