### Configuring wrapper options with commandOptions Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Example of setting binary path, ignoreWarnings, and commandOptions including environment variables for wkhtmltopdf. ```php '/obscure/path/to/wkhtmltopdf', 'ignoreWarnings' => true, 'commandOptions' => array( 'useExec' => true, // Can help on Windows systems 'procEnv' => array( // Check the output of 'locale -a' on your system to find supported languages 'LANG' => 'en_US.utf-8', ), ), )); ``` -------------------------------- ### Install via Composer Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Install the package using Composer. Requires the wkhtmltopdf command to be installed and working on your system. ```bash composer require mikehaertl/phpwkhtmltopdf ``` -------------------------------- ### Pdf constructor Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Creates a new Pdf instance with optional global options. The options array can contain global, page, and wrapper options. ```APIDOC ## Pdf constructor ### Description Creates a new Pdf instance with optional global options. The options array can contain global, page, and wrapper options. ### Method Constructor ### Signature `new Pdf($globalOptions)` ### Parameters - `$globalOptions` (array) - Optional - An array of global options for the PDF generation. ### Example ```php $pdf = new Pdf($globalOptions); ``` ``` -------------------------------- ### Image constructor Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Creates a new Image instance with optional options. The options array can contain image and wrapper options. ```APIDOC ## Image constructor ### Description Creates a new Image instance with optional options. The options array can contain image and wrapper options. ### Method Constructor ### Signature `new Image($options)` ### Parameters - `$options` (array) - Optional - An array of options for the image generation. ### Example ```php $image = new Image($options); ``` ``` -------------------------------- ### Passing options to Pdf methods Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Demonstrates how to set global PDF options and add pages, covers, and TOC with options using the Pdf class. ```php setOptions($globalOptions); // Set global PDF options (alternative) $pdf->addPage($page, $pageOptions); // Add page with options $pdf->addCover($page, $pageOptions); // Add cover with options $pdf->addToc($tocOptions); // Add TOC with options ``` -------------------------------- ### Create a multi-page PDF with cover and table of contents Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Builds a PDF by adding multiple pages (file, HTML string, or URL), a cover page, and a table of contents. Saves to a file, sends inline to the client, sends as a download, or returns the raw PDF as a string. Each method checks getError() on failure. ```php addPage('/path/to/page.html'); $pdf->addPage('....'); $pdf->addPage('http://www.example.com'); // Add a cover (same sources as above are possible) $pdf->addCover('/path/to/mycover.html'); // Add a Table of contents $pdf->addToc(); // Save the PDF if (!$pdf->saveAs('/path/to/report.pdf')) { $error = $pdf->getError(); // ... handle error here } // ... or send to client for inline display if (!$pdf->send()) { $error = $pdf->getError(); // ... handle error here } // ... or send to client as file download if (!$pdf->send('report.pdf')) { $error = $pdf->getError(); // ... handle error here } // ... or you can get the raw pdf as a string $content = $pdf->toString(); ``` -------------------------------- ### Create PDF with wkhtmltopdf options and add page Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Sets global PDF options including zero margins and disabled smart shrinking, then adds a page and sends the PDF. Handles errors via getError() if send() fails. ```php 0, 'margin-right' => 0, 'margin-bottom' => 0, 'margin-left' => 0, // Default page options 'disable-smart-shrinking', 'user-style-sheet' => '/path/to/pdf.css', )); // Add a page. To override above page defaults, you could add // another $options array as second argument. $pdf->addPage('/path/to/demo.html'); if (!$pdf->send()) { $error = $pdf->getError(); // ... handle error here } ``` -------------------------------- ### Image::setOptions Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Sets options for the Image instance. This is an alternative to passing options in the constructor. ```APIDOC ## Image::setOptions ### Description Sets options for the Image instance. This is an alternative to passing options in the constructor. ### Method setOptions ### Signature `$image->setOptions($options)` ### Parameters - `$options` (array) - Required - An array of options. ### Example ```php $image->setOptions($options); ``` ``` -------------------------------- ### Setting options for wkhtmltoimage Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Shows how to set options for the Image class, which has only one set of options. ```php setOptions($options); // Set image options (alternative) ``` -------------------------------- ### Create an image with Image::saveAs() and send() Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Creates an image from a file, HTML string, URL, or options array passed to the Image constructor. Saves to a PNG file with saveAs(), or sends inline or as a download with send(). Checks getError() on failure. ```php saveAs('/path/to/page.png'); // ... or send to client for inline display if (!$image->send()) { $error = $image->getError(); // ... handle error here } // ... or send to client as file download if (!$image->send('page.png')) { $error = $image->getError(); // ... handle error here } ``` -------------------------------- ### Setting wkhtmltopdf options array Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Shows how to pass options to wkhtmltopdf as an array, including options without arguments, with arguments, with 2 arguments, and repeatable options. ```php 'UTF-8', // option with argument // Option with 2 arguments 'cookie' => array('name'=>'value'), // Repeatable options with single argument 'run-script' => array( '/path/to/local1.js', '/path/to/local2.js', ), // Repeatable options with 2 arguments 'replace' => array( 'number' => $page++, // Replace '[number]' 'title' => $pageTitle, // Replace '[title]' ), ); ``` -------------------------------- ### Windows command options for Pdf Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Configures commandOptions with escapeArgs false and procOptions to bypass cmd.exe and suppress errors, recommended for Windows. Requires manual argument escaping. ```php array( 'escapeArgs' => false, 'procOptions' => array( // This will bypass the cmd.exe which seems to be recommended on Windows 'bypass_shell' => true, // Also worth a try if you get unexplainable errors 'suppress_errors' => true, ), ), ... )); ``` -------------------------------- ### Pdf::setOptions Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Sets global options for the PDF instance. This is an alternative to passing options in the constructor. ```APIDOC ## Pdf::setOptions ### Description Sets global options for the PDF instance. This is an alternative to passing options in the constructor. ### Method setOptions ### Signature `$pdf->setOptions($globalOptions)` ### Parameters - `$globalOptions` (array) - Required - An array of global options. ### Example ```php $pdf->setOptions($globalOptions); ``` ``` -------------------------------- ### Passing HTML content to header-html using File helper Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Shows how to pass a string to options like header-html using the File helper when automatic detection fails. ```php new File('Complex content', '.html'), ]; ``` -------------------------------- ### demo.html - HTML page for PDF generation Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md HTML document used as the page source for the PDF. Contains a print area with header, content, and footer sections. ```html ``` -------------------------------- ### Create a single page PDF with Pdf::saveAs() Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Creates a PDF from a file, HTML string, URL, or options array passed to the Pdf constructor. Saves to a file with saveAs() and checks getError() on failure. Optionally set the $pdf->binary property if wkhtmltopdf is not in the system PATH. ```php binary = 'C:\...'; if (!$pdf->saveAs('/path/to/page.pdf')) { $error = $pdf->getError(); // ... handle error here } ``` -------------------------------- ### Enable built-in Xvfb support in PHP Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Configure the Pdf class to use xvfb-run for each wkhtmltopdf call. Set 'enableXvfb' to true and optionally adjust 'xvfbRunBinary' and 'xvfbRunOptions'. This is recommended for low-frequency sites due to CPU overhead. ```php array( 'enableXvfb' => true, // Optional: Set your path to xvfb-run. Default is just 'xvfb-run'. // 'xvfbRunBinary' => '/usr/bin/xvfb-run', // Optional: Set options for xfvb-run. The following defaults are used. // 'xvfbRunOptions' => '--server-args="-screen 0, 1024x768x24"', ), )); ``` -------------------------------- ### Use standalone Xvfb display in PHP Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Configure the Pdf class to use an already-running Xvfb process by setting the DISPLAY environment variable via 'procEnv'. Change ':0' to match your daemon's display. ```php array( // You can change ':0' to whatever display you pick in your daemon script 'procEnv' => array( 'DISPLAY' => ':0' ), ), )); ``` -------------------------------- ### Error handling with getError() Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Checks return values of send() and toString() for false and throws an exception with the detailed error message from getError(). ```php send()) { throw new Exception('Could not create PDF: '.$pdf->getError()); } $content = $pdf->toString(); if ($content === false) { throw new Exception('Could not create PDF: '.$pdf->getError()); } ``` -------------------------------- ### Pdf::addCover Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Adds a cover page to the PDF with optional page-specific options. The cover can be a URL, file path, or HTML string. ```APIDOC ## Pdf::addCover ### Description Adds a cover page to the PDF with optional page-specific options. The cover can be a URL, file path, or HTML string. ### Method addCover ### Signature `$pdf->addCover($page, $pageOptions)` ### Parameters - `$page` (string) - Required - The cover content or source (URL, file path, or HTML string). - `$pageOptions` (array) - Optional - An array of page-specific options. ### Example ```php $pdf->addCover($page, $pageOptions); ``` ``` -------------------------------- ### Pdf::addPage Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Adds a page to the PDF with optional page-specific options. The page can be a URL, file path, or HTML string. ```APIDOC ## Pdf::addPage ### Description Adds a page to the PDF with optional page-specific options. The page can be a URL, file path, or HTML string. ### Method addPage ### Signature `$pdf->addPage($page, $pageOptions)` ### Parameters - `$page` (string) - Required - The page content or source (URL, file path, or HTML string). - `$pageOptions` (array) - Optional - An array of page-specific options. ### Example ```php $pdf->addPage($page, $pageOptions); ``` ``` -------------------------------- ### Pdf::addToc Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Adds a table of contents to the PDF with optional TOC-specific options. ```APIDOC ## Pdf::addToc ### Description Adds a table of contents to the PDF with optional TOC-specific options. ### Method addToc ### Signature `$pdf->addToc($tocOptions)` ### Parameters - `$tocOptions` (array) - Optional - An array of TOC-specific options. ### Example ```php $pdf->addToc($tocOptions); ``` ``` -------------------------------- ### Apache mod_deflate exclusion for pdf.php Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Disables gzip compression for the download URL to prevent corrupted PDFs when using send(). Add to Apache configuration. ```apache SetEnvIfNoCase REQUEST_URI ^/pdf.php$ no-gzip dont-vary ``` -------------------------------- ### pdf.css - Print styles with margins and page size Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md CSS stylesheet applied via user-style-sheet. Defines A4 page size and printable area with margins, plus header and footer styling. Requires print-area adjustment when changing page size. ```css /* Define page size. Requires print-area adjustment! */ body { margin: 0; padding: 0; width: 21cm; height: 29.7cm; } /* Printable area */ #print-area { position: relative; top: 1cm; left: 1cm; width: 19cm; height: 27.6cm; font-size: 10px; font-family: Arial; } #header { height: 3cm; background: #ccc; } #footer { position: absolute; bottom: 0; width: 100%; height: 3cm; background: #ccc; } ``` -------------------------------- ### Suppress Content-Length header in send() Source: https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/README.md Disables the Content-Length header to avoid issues with webserver compression. Available since version 2.5.0. ```php send('name.pdf', false, array( 'Content-Length' => false, )); $image->send('name.png', false, array( 'Content-Length' => false, )); ```