1. Go to this page and download the library: Download mikehaertl/phpwkhtmltopdf library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mikehaertl / phpwkhtmltopdf example snippets
use mikehaertl\wkhtmlto\Pdf;
// You can pass a filename, a HTML string, an URL or an options array to the constructor
$pdf = new Pdf('/path/to/page.html');
// On some systems you may have to set the path to the wkhtmltopdf executable
// $pdf->binary = 'C:\...';
if (!$pdf->saveAs('/path/to/page.pdf')) {
$error = $pdf->getError();
// ... handle error here
}
use mikehaertl\wkhtmlto\Pdf;
$pdf = new Pdf;
$pdf->addPage('/path/to/page.html');
$pdf->addPage('<html>....</html>');
$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();
use mikehaertl\wkhtmlto\Image;
// You can pass a filename, a HTML string, an URL or an options array to the constructor
$image = new Image('/path/to/page.html');
$image->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
}
$options = array(
'no-outline', // option without argument
'encoding' => '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]'
),
);
$pdf = new Pdf($globalOptions); // Set global PDF options
$pdf->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
$image = new Image($options); // Set image options
$image->setOptions($options); // Set image options (alternative)
$pdf = new Pdf(array(
'binary' => '/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',
),
),
));
use mikehaertl\tmp\File;
$options = [
'header-html' => new File('Complex content', '.html'),
];
if (!$pdf->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 = new Pdf(array(
'commandOptions' => 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 = new Pdf(array(
// Explicitly tell wkhtmltopdf that we're using an X environment
'use-xserver',
// Enable built in Xvfb support in the command
'commandOptions' => 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"',
),
));
$pdf = new Pdf(array(
'use-xserver',
'commandOptions' => array(
// You can change ':0' to whatever display you pick in your daemon script
'procEnv' => array( 'DISPLAY' => ':0' ),
),
));
use mikehaertl\wkhtmlto\Pdf;
// Create a new Pdf object with some global PDF options
$pdf = new Pdf(array(
'no-outline', // Make Chrome not complain
'margin-top' => 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
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.