- August 24, 2017
- Posted by: Rajni
- Category: Uncategorized
We tried using node-pdf from NPM – JavaScript Package Manager to generate PDF file from HTML file. node-pdf take HTML source as an input and return PDF file. A requirement to generate PDF format file for Photo files comprising multiple image format, So the purpose to convert photos into PDF format.
Once trying, we experienced an issue in generating PDF file when HTML file size is more than 3 MB.
We got to understand about default time out maximum to 30 Seconds for PhantomJS, So in a case when HTML file size is larger than 3 MB, PhantomJS generates timeout error and node-pdf return response as a corrupted PDF file.
[gist filename=”phantomjs” url=”isummation/fbcfb54c914883c5a2f7f64f3e640023″ public=”true”]this.options.timeout = parseInt(this.options.timeout) || 30000[/gist]
Fixing PhantomJS timeout issue I need to override default timeout parameter by passing timeout attributes in option.
[gist filename=”nodepdf” url=”isummation/bc24cf75a0b0c71e3249b13168edf7c6″ public=”true”] var pdf = require(‘html-pdf’); var options = { format: ‘A4’, border: { top: “0.5cm”, right: “0.5cm”, bottom: “0.5cm”, left: “0.5cm” }, timeout: 60000 } pdf.create(html, options).toFile(‘./photos.pdf’, function(err, res){ if (err) return console.log(err); console.log(res); }); [/gist]
So by increasing PhantomJS timeout I’ve fixed big size file issue.