Hhtml-pdf error
I'm using this module in my project. It works fine in generating a pdf file. But it responds back with an error that i have mentioned below.
[SyntaxError: Unexpected token R].
I don't know where it comes from... need help
Hey there. It might be a problem with some https files. They aren't supported unless you add ignore-ssl-errors=true to the phantomArgs option (That's a phantomjs bug).
Have a look at that: https://github.com/marcbachmann/node-html-pdf/commit/3576f46f68dbda49c315ec3b1d5ab39c9fba6d3e
hi,I'm using this module but its not working,its says: Failed to load PhantomJS module. You have to set the path to the PhantomJS binary using 'options.phantomPath' I dont know how to set the path
I'm also using this module to generate pdf. I haven't using phontomjs. it works fine for me. just use fs module if your are using node.js
Otherwise place your code here. or in plunker
I'm using node.js,i just copy your exampel it say the same thing
var fs = require('fs');
var pdf = require('html-pdf');
app.get('/pdf',function(){
var html = fs.readFileSync('./test.html', 'utf8')
var options = { format: 'Letter' };
pdf.create(html, options).toFile('./test.pdf', function(err, res) {
if (err) return console.log(err);
console.log(res); // { filename: '/app/businesscard.pdf' }
});
});
@s-kh what system are you on?
npm install html-pdf should download the phantomjs executable by itself.
try this,
var fs = require('fs');
var pdf = require('html-pdf');
app.get("/pdf",function(){
var html = fs.readFileSync('./test.html', 'utf8');
var options = {
filename: './path/to/yourFile.pdf', format: 'Letter', "type": "pdf", "font-family": "Calibri",
"footer": {
"height": "28mm",
"contents": <div>--content of your footer--</div>
},
//Page option
"border": {
"top": "0.2in",
"bottom": "0.1in",
"right": "0.5in",
"left": "0.5in"
}
};
pdf.create(html, options).toFile(function(err, res) {
if (err) return console.log(err);
console.log(res);
});
});
I'm using the command but it's not work,this command (npm install html-pdf) is enough to get phantomjs ?
Had the same error, i tried adding the phantomjs --ignore-ssl-errors=true but didn't work.
In the html head i had a javascript function. I removed it and both toStream and toBuffer function now works.
If this is also your problem, i discovered that the toFile function works anyway
yes I got the same error "SyntaxError: Unexpected token R in JSON at position 0"
pdf.create(html, options).toFile('./test.pdf', function(err, res) {
if (err) return console.log(err); // SyntaxError: Unexpected token R in JSON at position 0
console.log(res); // { filename: '/app/businesscard.pdf' }
});
the nodejs crash, and console.log(err) got Error " SyntaxError: Unexpected token R in JSON at position 0".
the object "res" got undefined. it not work for the nodejs 6.0 .
I got solution to fixed this:
- remove console.log(err) or next(err);
- add fs.stat to check file
pdf.create(html, options).toFile('./test.pdf', function(err, res) {
if (err) return console.log(err); // SyntaxError: Unexpected token R in JSON at position 0
fs.stat(pdffile, function(err, stat){
if(stat && stat.isFile()){
console.log("-----PDF SUCCESS", stat);
resolve({'pdfpath':pdffile});
}else{
console.log("-----PDF Error", stat);
reject(err);
}
})
});
It is little weird why res is undefined.