Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting paperSize for PDF printing in Casper

Tags:

casperjs

In generating PDFs in Phantom, I can set the paper size like this:

page.paperSize = {
  height: '8.5in',
  width: '11in',
  orientation: 'landscape',
  border: '0.4in'
};

then the page.render(output) function generates a PDF properly. In other words, the size is correct and it has many pages of that size.

I can't get this to work in Casper (and I'm not sure if it is supported). So for example, the following:

var casper = require('casper').create({
    paperSize: {
      height: '8.5in',
      width: '11in',
      orientation: 'landscape',
      border: '0.4in'
    },
    logLevel: 'debug',
    verbose: true
});

....this.capture('print.pdf'); ...

creates a PDF with a single, very long page. Setting viewportSize does not fix the problem.

Is there any way to access the pageSize object from within Casperjs?

like image 878
Jeff Avatar asked May 18 '13 20:05

Jeff


1 Answers

You can access paperSize through casper.page.paperSize, however you will need to set this after calling casper.start(), otherwise casper.page will be equal to null.

Here's an example:

var casper = require("casper").create();
casper.start();

casper.page.paperSize = {
  width: '11in',
  height: '8.5in',
  orientation: 'landscape',
  border: '0.4in'
};

casper.thenOpen('http://www.facebook.com/', function() {
  this.capture('test.pdf');
  this.echo('created pdf.');
});

casper.run();
like image 92
hexid Avatar answered Dec 08 '22 01:12

hexid