marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.55k stars 544 forks source link

Problem with external URLs #347

Open vaske opened 6 years ago

vaske commented 6 years ago
<html>
<head>
<style>
    body {
        background-image: url("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png");
        background-color: #ccc;
    }
</style>
</head>
<body>
    <div>sample div</div>
</body>
</html>

When I use this and store in html file, and run html-pdf from command line it works I see properly rendered pdf file, if I send that to pdf over nodejs like:

// 
...
    pdf.create(html, {
            format: 'letter',
            border: {
                top: '0.6in',
                right: '0.6in',
                bottom: '0.6in',
                left: '0.6in'
            }
        }).toFile(`./tmp/${fileName}`, function (error, response) {
            if (error) {
                console.log(error);
                future.return(error);
            } else {
                const file = fs.readFileSync(response.filename);
                base64String = new Buffer(file).toString('base64');
                fs.unlink(response.filename);
                future.return(base64String);
            }
        });

basically this doesn't work only when html contains remote URLs, if I remove remote URLs from code it works. Is there any further parameter which will allows reading of remote URLs or?

marcbachmann commented 6 years ago

Weird issue. Could you try to delay the rendering a bit. There’s a new option named renderDelay for that. I assume that the network is slower and phantomjs doesn’t wait for images in css to be loaded.

vaske commented 6 years ago

@marcbachmann yeah have slow connection here only 150Mbit/s lol. I found what is the issue basically this is issue with Meteor privileges and phantomjs, some weird issue which I'm investigating, it works in debug mode of app but not in regular startup.

However would like to thanks for help, tool is great!

vaske commented 6 years ago

@marcbachmann I found another issue, I send html string to create function, as part of html is style where I have full path URL for example like this one http://www.pngmart.com/files/4/Earth-PNG-Image.png and there is a class which have that as background-image, after printing pdf have blank background instead to have this image...I wonder is there only way to render properly with images is to set base file location from where it will pull images or?

kkor commented 5 years ago

@vaske Did you ever manage to solve this? We're also running into images not being rendered in the pdf anymore with our Meteor setup.

vaske commented 5 years ago

@kkor nope, we left it and made our own pdf server for such stuff, it was long time ago :)

kkor commented 5 years ago

Thanks for the fast reply, let's see if I can fix ours :)

phanthinh commented 5 years ago

Hi @vaske

I have a prolem when render gmap here image

I used code

pdf.create(htmlPageOne, _.merge({}, options, { orientation: 'landscape' })).toStream(function(err, stream){

            let ws = stream.pipe(fs.createWriteStream(tmpPath + '/' + outputName + '_one.pdf'));
            ws.on('finish', function () {

            });

          })

Can you help me it's same your issue?

vaske commented 5 years ago

@phanthinh nope I didn't have that kind of issue.