Adam Harris
Adam Harris

Reputation: 77

PhantomJS - Render image per page

So I am in the process of migrating to PhantomJS to render our PDFs & Screen Scrapes for our site. Everything is pretty much figured out except for one thing that is giving me problems.

Is it possible to use PhantomJS to render a single image per 'page' in a document? similar to how it does paging in PDF except for a new image for each one of the pages.

Upvotes: 1

Views: 93

Answers (1)

Dmitry Demidovsky
Dmitry Demidovsky

Reputation: 8197

similar to how it does paging in PDF

I suppose you are talking about paperSize property.

render a single image per 'page'

What if you simply "move" clipRect page-by-page, rendering each pass intro separate file?
Something like this:

var page = require('webpage').create();
var pageHeight = 600;
var pageWidth = 400;
var index = 0;
var maxPages = 5;

page.viewportSize = { width: pageWidth, height: pageHeight };

function takeNextPage() {
    page.clipRect = {
        top: index * pageHeight,
        left: 0,
        width: pageWidth,
        height: pageHeight
    };

    page.render('page' + index + '.png');

    if (++index < maxPages) {
        takeNextPage();
    } else {
        phantom.exit();
    }
}

page.open('http://...', function() {
    takeNextPage();
});

Upvotes: 1

Related Questions