PHP Class mikehaertl\wkhtmlto\Pdf

This class is a slim wrapper around wkhtmltopdf.
Author: Michael Härtl ([email protected])
Datei anzeigen Open project: mikehaertl/phpwkhtmltopdf Class Usage Examples

Public Properties

Property Type Description
$binary the name of the wkhtmltopdf binary. Default is wkhtmltopdf. You can also configure a full path here.
$commandOptions options to pass to the Command constructor. Default is none.
$ignoreWarnings whether to ignore any errors if some PDF file was still created. Default is false.
$tmpDir the directory to use for temporary files. If null (default) the dir is autodetected.
$version9 whether the old version 9 of wkhtmltopdf is used (slightly different syntax). Default is false.

Protected Properties

Property Type Description
$_command the command instance that executes wkhtmltopdf
$_error the detailed error message. Empty string if none.
$_isCreated whether the PDF was created
$_objects list of wkhtmltopdf objects as arrays
$_options global options for wkhtmltopdf as array('--opt1', '--opt2'=>'val', ...)
$_tmpFiles list of tmp file objects. This is here to keep a reference to File and thus avoid too early call of File::__destruct() if the file is not referenced anymore.
$_tmpPdfFile the temporary PDF file

Public Methods

Method Description
__construct ( array | string $options = null )
addCover ( string $input, array $options = [], string | null $type = null ) : static Add a cover page object to the output
addPage ( string $input, array $options = [], string | null $type = null ) : static Add a page object to the output
addToc ( array $options = [] ) : static Add a TOC object to the output
getCommand ( ) : Command
getError ( ) : string
getPdfFilename ( ) : string
saveAs ( string $filename ) : boolean Save the PDF to given filename (triggers PDF creation)
send ( string | null $filename = null, boolean $inline = false ) : boolean Send PDF to client, either inline or as download (triggers PDF creation)
setOptions ( array $options = [] ) : static Set global option(s)
toString ( ) : string | boolean Get the raw PDF contents (triggers PDF creation).

Protected Methods

Method Description
createPdf ( ) : boolean Run the Command to create the tmp PDF file
processInput ( string $input, string | null $type = null ) : mikehaertl\tmp\File | string
processOptions ( array $options = [] ) : array

Method Details

__construct() public method

public __construct ( array | string $options = null )
$options array | string global options for wkhtmltopdf, a page URL, a HTML string or a filename

addCover() public method

Add a cover page object to the output
public addCover ( string $input, array $options = [], string | null $type = null ) : static
$input string either a URL, a HTML string or a filename
$options array optional options for the cover page
$type string | null a type hint if the input is a string of known type. This can either be `TYPE_HTML` or `TYPE_XML`. If `null` (default) the type is auto detected from the string content.
return static the Pdf instance for method chaining

addPage() public method

Add a page object to the output
public addPage ( string $input, array $options = [], string | null $type = null ) : static
$input string either a URL, a HTML string or a filename
$options array optional options for this page
$type string | null a type hint if the input is a string of known type. This can either be `TYPE_HTML` or `TYPE_XML`. If `null` (default) the type is auto detected from the string content.
return static the Pdf instance for method chaining

addToc() public method

Add a TOC object to the output
public addToc ( array $options = [] ) : static
$options array optional options for the table of contents
return static the Pdf instance for method chaining

createPdf() protected method

Run the Command to create the tmp PDF file
protected createPdf ( ) : boolean
return boolean whether creation was successful

getCommand() public method

public getCommand ( ) : Command
return Command the command instance that executes wkhtmltopdf

getError() public method

public getError ( ) : string
return string the detailed error message. Empty string if none.

getPdfFilename() public method

public getPdfFilename ( ) : string
return string the filename of the temporary PDF file

processInput() protected method

protected processInput ( string $input, string | null $type = null ) : mikehaertl\tmp\File | string
$input string
$type string | null a type hint if the input is a string of known type. This can either be `TYPE_HTML` or `TYPE_XML`. If `null` (default) the type is auto detected from the string content.
return mikehaertl\tmp\File | string a File object if the input is a HTML or XML string. The unchanged input otherwhise.

processOptions() protected method

protected processOptions ( array $options = [] ) : array
$options array list of options as name/value pairs
return array options with raw content converted to tmp files where neccessary

saveAs() public method

Save the PDF to given filename (triggers PDF creation)
public saveAs ( string $filename ) : boolean
$filename string to save PDF as
return boolean whether PDF was created successfully

send() public method

Send PDF to client, either inline or as download (triggers PDF creation)
public send ( string | null $filename = null, boolean $inline = false ) : boolean
$filename string | null the filename to send. If empty, the PDF is streamed inline.
$inline boolean whether to force inline display of the PDF, even if filename is present.
return boolean whether PDF was created successfully

setOptions() public method

Set global option(s)
public setOptions ( array $options = [] ) : static
$options array list of global PDF options to set as name/value pairs
return static the Pdf instance for method chaining

toString() public method

Get the raw PDF contents (triggers PDF creation).
public toString ( ) : string | boolean
return string | boolean The PDF content as a string or `false` if the PDF wasn't created successfully.

Property Details

$_command protected_oe property

the command instance that executes wkhtmltopdf
protected $_command

$_error protected_oe property

the detailed error message. Empty string if none.
protected $_error

$_isCreated protected_oe property

whether the PDF was created
protected $_isCreated

$_objects protected_oe property

list of wkhtmltopdf objects as arrays
protected $_objects

$_options protected_oe property

global options for wkhtmltopdf as array('--opt1', '--opt2'=>'val', ...)
protected $_options

$_tmpFiles protected_oe property

list of tmp file objects. This is here to keep a reference to File and thus avoid too early call of File::__destruct() if the file is not referenced anymore.
protected $_tmpFiles

$_tmpPdfFile protected_oe property

the temporary PDF file
protected $_tmpPdfFile

$binary public_oe property

the name of the wkhtmltopdf binary. Default is wkhtmltopdf. You can also configure a full path here.
public $binary

$commandOptions public_oe property

options to pass to the Command constructor. Default is none.
public $commandOptions

$ignoreWarnings public_oe property

whether to ignore any errors if some PDF file was still created. Default is false.
public $ignoreWarnings

$tmpDir public_oe property

the directory to use for temporary files. If null (default) the dir is autodetected.
public $tmpDir

$version9 public_oe property

whether the old version 9 of wkhtmltopdf is used (slightly different syntax). Default is false.
public $version9