chishiki
chishiki

Reputation: 702

Wkhtmltopdf + cakepdf - Custom page size

I can't figure out how to enable custom page sizes with CakePDF and wkhtmltopdf. I have the following configuration code:

Configure::write('CakePdf', [
  'engine' => [
      'className' => 'CakePdf.WkHtmlToPdf',
      'binary' => '/usr/local/bin/wkhtmltopdf',
  ],
  'orientation' => 'portrait',
  'pageSize' => '', // this line
  'download' => true
]);

I want to have 150x150mm pages. I already tried several things like passing an array [150,150] but also things like '150 150' or '150mm 150mm'. Is this even possible?

Upvotes: 0

Views: 5733

Answers (2)

Mr. T
Mr. T

Reputation: 302

wkhtmltopdf does support custom page sizes. I'm not sure if cakepdf will work with them, but if it just passes the arguments to wkhtmltopdf then it should. You can do it like this:

Configure::write('CakePdf', [
  'engine' => [
      'className' => 'CakePdf.WkHtmlToPdf',
      'binary' => '/usr/local/bin/wkhtmltopdf',
  ],
  'orientation' => 'portrait',
  'page-width' => '4in'
  'page-height' => '6in'
  'download' => true
]);

You can use in,cm,and mm as size formats

Upvotes: 0

ndm
ndm

Reputation: 60463

The CakePDF pageSize option maps to the page-size option of wkhtmltopdf, which takes QPrinter::PaperSize constant names, like for example A4, A5, B0, B1, Legal, Letter, etc., ie you cannot define a custom size using that option.

If you need a custom size, then you have to use the wkhtmltopdf specific page-width and page-height options, which both take millimeter values by default.

Quote from the wkhtmltopdf docs:

Page sizes:

The default page size of the rendered document is A4, but using this --page-size option this can be changed to almost anything else, such as: A3, Letter and Legal. For a full list of supported pages sizes please see http://qt-project.org/doc/qt-4.8/qprinter.html#PaperSize-enum.

For a more fine grained control over the page size the --page-height and --page-width options may be used

Configure::write('CakePdf', [
    'engine' => [
        'className' => 'CakePdf.WkHtmlToPdf',
        'binary' => '/usr/local/bin/wkhtmltopdf',
        'options' => [
            'page-width' => 150,
            'page-height' => 150
        ]
    ],
    'orientation' => 'portrait',
    'download' => true
]);

See also

Upvotes: 2

Related Questions