Search code examples
phpcakephpcakephp-3.0wkhtmltopdf

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?


Solution

  • 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