form3
form3

Reputation: 127

PhantomJS bridge for NodeJS: paperSize do not work correctly

I use PhantomJS bridge for NodeJS. I want to render PDF file with 595x842 px size.

var phantom = require('phantom');
phantom.create(function(ph) {
    ph.createPage(function(page) {
        page.set('paperSize', {width: '595px', height: '842px', border: '0px' });
        page.open("http://localhost:3000", function(status) {
            if (status !== 'success') {
                console.log('Unable to access the network!');
            } else {
                page.render('filename.pdf');
            }
            ph.exit();
        });
    });
});

But, by the end I get 235x331px PDF file. I can't understand why. Maybe someone can help and explain me how can I render necessary file size ?

Upvotes: 0

Views: 1395

Answers (1)

nramirez
nramirez

Reputation: 5630

Reviewing the repo tutorial I found this:

Properties can't be get/set directly, instead use page.get('version', callback) or page.set('viewportSize', {width:640,height:480}), etc. Nested objects can be accessed by including dots in keys, such as page.set('settings.loadImages', false)

So I tried the following code:

var phantom = require('phantom');

phantom.create(function(ph) {

    ph.createPage(function(page) {

        page.set('viewportSize', { width : 595, height : 842});
        page.open("http://localhost:3000", function(status) {
            if (status !== 'success') {
                console.log('Unable to access the network!');
            } else {
                page.render('filename.pdf');
            }
            ph.exit();

        });
    });
});

Hope this works for you, like it did for me.

Upvotes: 4

Related Questions