Setting paperSize for PDF printing in Casper

…衆ロ難τιáo~ 提交于 2019-12-10 02:43:36

问题


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?


回答1:


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();


来源:https://stackoverflow.com/questions/16628737/setting-papersize-for-pdf-printing-in-casper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!