Using toStream, toFile and toBuffer the first time through the PDF is created successfully, but when I try to create another PDF (giving the first one time to finish) I always get the following error:
Error: ENOENT: no such file or directory /tmp/html-pdf-61515.pdf /* 61515 being a random number generated by html-pdf */
Is something not closing out properly...I'm following the examples to the letter.
Running
node v6.3.0
"express": "4.14.0",
"html-pdf": "2.1.0",
Any help would be really appreciated. At first I thought this was related to running multiple instances of pdf.create....PhantomJS is not thread safe, but I remedied that (or so I thought) with async.queue.
same here
I did the process manually just now:
1) First call phantomjs passing pdf_a4_portrait.js and click enter:
/home/vagrant/data/web/api/node_modules/html-pdf/node_modules/phantomjs/lib/phantom/bin/phantomjs /home/vagrant/data/web/api/node_modules/html-pdf/lib/scripts/pdf_a4_portrait.js
2) copy/paste a test JSON and click enter:
{"html":"<html><head><title>teste</title></head><body>teste</body></html>","options":{"format":"Letter","orientation":"portrait","border":"0.5in","settings":{"loadImages":true,"localToRemoteUrlAccessEnabled":true},"phantomPath":"/home/vagrant/data/web/api/node_modules/html-pdf/node_modules/phantomjs/lib/phantom/bin/phantomjs","phantomArgs":[],"timeout":30000,"filename":"/home/vagrant/data/web/api/pdfTemplates/briefs/tempPdf1475003122875.pdf"}}
It will output the JSON with the path:
{"filename":"/home/vagrant/data/web/api/pdfTemplates/tempPdf1475003122875.pdf"}
The piece of code that does this process automatically is:
PDF.prototype.exec = function(callback) {
var child, stderr, stdout, timeout;
child = childprocess.spawn(this.options.phantomPath, [].concat(this.options.phantomArgs, [this.script]));
stdout = [];
stderr = [];
timeout = setTimeout(function() {
child.stdin.end();
child.kill();
if (!stderr.length) {
return stderr = [new Buffer('html-pdf: PDF generation timeout. Phantom.js script did not exit.')];
}
}, this.options.timeout);
child.stdout.on('data', function(buffer) {
return stdout.push(buffer);
});
child.stderr.on('data', function(buffer) {
stderr.push(buffer);
child.stdin.end();
return child.kill();
});
child.on('exit', function(code) {
var data, error1, ref;
clearTimeout(timeout);
if (code || stderr.length) {
err = new Error(Buffer.concat(stderr).toString() || 'html-pdf: Unknown Error');
return callback(err);
} else {
try {
data = (ref = Buffer.concat(stdout).toString()) != null ? ref.trim() : void 0;
data = JSON.parse(data);
} catch (error1) {
err = error1;
return callback(err);
}
return callback(null, data);
}
});
return child.stdin.write(JSON.stringify({
html: this.html,
options: this.options
}) + '\n', 'utf8');
};
If you output data after the JSON parsing, you will see the invalid path:
data = (ref = Buffer.concat(stdout).toString()) != null ? ref.trim() : void 0;
data = JSON.parse(data);
console.log(data);
When I run it manually the path is valid, but the node execution for some reason does not actually create the file.
I'm getting the same error. makes no sense. It was working fine for 2 days, now suddenly it's just failling to create the PDF. Same for toStream and toFile. It only creates PDF the first time, then nothing.
Guys, I have no idea what happened, but html-pdf is working fine now. My script is the same, which is bizzare that it started working again.
I had a same issue and solved it.
do this.
sudo apt-get install libfontconfig.
related document -> http://stackoverflow.com/questions/13046555/wkhtmltopdf-libfontconfig-so-1-cannot-open-shared-object-file
@danielmapar were you able to find the solution ? Even I am facing the same right now, file is not getting generated sometimes even though pdf.create() gives response with filename. This doesn't happen in my Mac (localhost) but happens on server only (CentOS).
I was able to solve this on my end. There was something wrong on my end, I didn't write the code in stream.on('end',()=>) and due to fs.removeSync(), file was getting removed before stream would end.
I was able to solve this on my end. There was something wrong on my end, I didn't write the code in stream.on('end',()=>) and due to fs.removeSync(), file was getting removed before stream would end.
Thanks for sharing @sharvilak11
Most helpful comment
I was able to solve this on my end. There was something wrong on my end, I didn't write the code in stream.on('end',()=>) and due to fs.removeSync(), file was getting removed before stream would end.