9

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?

Jeff
  • 939
  • 10
  • 19

1 Answers1

16

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();
hexid
  • 3,811
  • 1
  • 30
  • 40
  • @hexid is there anyway to have a custom header and footer added to the PDF before casperjs saves the page as a PDF? – Anagio Oct 23 '13 at 13:46
  • @Anagio You can check out [this](http://stackoverflow.com/q/17125955/395353) question for an example. – hexid Oct 23 '13 at 14:34