Neel Neel - 6 months ago 121
PHP Question

Creating Landscape PDF using WkHtmlToPdf by Mikehaertl

I am using mikehaertl's OOP interface to create pdf using wkhtmltpdf via php. Ref: http://mikehaertl.github.io/phpwkhtmltopdf/

This is my code:

<?php
require_once('../WkHtmlToPdf.php');

$pdf = new WkHtmlToPdf(array(
// Use `wkhtmltopdf-i386` or `wkhtmltopdf-amd64`
'bin' => 'C:\Users\me\Documents\EasyPHP-5.3.6.0\www\wkhtmltopdf\wkhtmltopdf.exe'
));

// Add a HTML file, a HTML string or a page from a URL
$pdf->addPage('page.html');

if(!$pdf->send())
throw new Exception('Could not create PDF: '.$pdf->getError());


The above code generates the pdf but it only creates all files in the portrait orientation. How do I change it to landscape orientation? When I searching the web, I found that wkhtlktopdf has an option (-O landscape) to change the orientation but I dont know how to use it with PHP WkHtmlToPdf created by Mikeheart?

Answer

You can do this by calling setOptions() with an array of your desired options, in this case array('orientation' => 'landscape') before the addPage() call.

$pdf->setOptions(array(
    'orientation' => 'landscape'
));

As can be seen here: https://github.com/mikehaertl/phpwkhtmltopdf#full-example

You can add any other options that can be found under "Global Options" in the wkhtmltopdf documentation, to the array you pass in.

Comments