node-html-pdf icon indicating copy to clipboard operation
node-html-pdf copied to clipboard

Hhtml-pdf error

Open nandhacse opened this issue 11 years ago • 11 comments

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

nandhacse avatar Aug 27 '15 11:08 nandhacse

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

marcbachmann avatar Sep 06 '15 18:09 marcbachmann

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

s-kh avatar Sep 30 '15 06:09 s-kh

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

nandhacse avatar Sep 30 '15 06:09 nandhacse

Otherwise place your code here. or in plunker

nandhacse avatar Sep 30 '15 06:09 nandhacse

I'm using node.js,i just copy your exampel it say the same thing

s-kh avatar Sep 30 '15 07:09 s-kh

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 avatar Sep 30 '15 07:09 s-kh

@s-kh what system are you on? npm install html-pdf should download the phantomjs executable by itself.

marcbachmann avatar Sep 30 '15 07:09 marcbachmann

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);
  });
  });

nandhacse avatar Sep 30 '15 07:09 nandhacse

I'm using the command but it's not work,this command (npm install html-pdf) is enough to get phantomjs ?

s-kh avatar Sep 30 '15 07:09 s-kh

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

stellamargonar avatar Nov 23 '15 12:11 stellamargonar

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:

  1. remove console.log(err) or next(err);
  2. 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.

jinwyp avatar Jun 03 '16 05:06 jinwyp