Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
WkHtmlToPdf Class Reference

Public Member Functions

 __construct ($options=array())
 __destruct ()
 addPage ($input, $options=array())
 addCover ($input, $options=array())
 addToc ($options=array())
 saveAs ($filename)
 send ($filename=null)
 setOptions ($options)
 setPageOptions ($options=array())
 getError ()
 getTmpDir ()

Public Attributes

const REGEX_HTML = '/<.*html.*>/i'

Protected Member Functions

 getPdfFilename ()
 getCommand ($filename)
 createPdf ($fileName)
 createTmpFile ($content)
 renderOptions ($options)

Protected Attributes

 $bin = '/usr/bin/wkhtmltopdf'
 $options = array()
 $pageOptions = array()
 $objects = array()
 $tmp
 $tmpFile
 $tmpFiles = array()
 $error

Detailed Description

WkHtmlToPdf

This class is a slim wrapper around wkhtmltopdf.

It provides a simple and clean interface to ease PDF creation with wkhtmltopdf. The wkhtmltopdf binary must be installed and working on your system. The static binary is preferred but this class should also work with the non static version, even though a lot of features will be missing.

Basic use

 $pdf = new WkHtmlToPdf;

 // Add a HTML file, a HTML string, a page from URL or a PDF file
 $pdf->addPage('/home/joe/page.html');
 $pdf->addPage('<html>....</html>');
 $pdf->addPage('http://google.com');
 $pdf->addPage('/home/joe/my.pdf');

 // Add a cover (same sources as above are possible)
 $pdf->addCover('mycover.pdf');

 // Add a Table of contents
 $pdf->addToc();

 // Save the PDF, or ...
 $pdf->saveAs('/tmp/new.pdf');

 // ... send to client for inline display or ...
 $pdf->send();

 // ... send to client as file download
 $pdf->send('test.pdf');

Setting options

The wkhtmltopdf binary has some global options (e.g. to set the document's DPI) and options for each PDF page (e.g. to supply a custom CSS file). Please see "wkhtmltopdf -H" to get a list of all available options.

In addition this class also supports global page options: You can set default page options that will be applied to every page you add. You can also override these defaults per page:

 $pdf = new WkHtmlToPdf($options);   // Set global PDF options
 $pdf->setOptions($options);         // Set global PDF options (alternative)
 $pdf->setPageOptions($options);     // Set default page options
 $pdf->addPage($page, $options);     // Add page with options (overrides default page options)

Special global options

You can use these special global options to set up some file paths:

 bin: path to the wkhtmltopdf binary. Defaults to /usr/bin/wkhtmltopdf.
 tmp: path to tmp directory. Defaults to PHP temp dir.

Error handling

saveAs() and save() will return false on error. In this case the detailed error message from wkhtmltopdf can be obtained through getError().

Author
Michael Härtl haert.nosp@m.l.mi.nosp@m.ke@gm.nosp@m.ail..nosp@m.com (sponsored by PeoplePerHour.com)
Version
1.1.0 http://www.opensource.org/licenses/MIT

Definition at line 74 of file WkHtmlToPdf.php.

Constructor & Destructor Documentation

WkHtmlToPdf::__construct (   $options = array())
Parameters
array$optionsglobal options for wkhtmltopdf (optional)

Definition at line 94 of file WkHtmlToPdf.php.

References setOptions().

{
if($options!==array())
$this->setOptions($options);
}
WkHtmlToPdf::__destruct ( )

Remove temporary PDF file and pages when script completes

Definition at line 103 of file WkHtmlToPdf.php.

{
if($this->tmpFile!==null)
unlink($this->tmpFile);
foreach($this->tmpFiles as $tmp)
unlink($tmp);
}

Member Function Documentation

WkHtmlToPdf::addCover (   $input,
  $options = array() 
)

Add a cover page object to the output

Parameters
string$inputeither a URL or a PDF filename
array$optionsoptional options for this page

Definition at line 130 of file WkHtmlToPdf.php.

{
$options['input'] = "cover $input";
$this->objects[] = array_merge($this->pageOptions,$options);
}
WkHtmlToPdf::addPage (   $input,
  $options = array() 
)

Add a page object to the output

Parameters
string$inputeither a URL, a HTML string or a PDF/HTML filename
array$optionsoptional options for this page

Definition at line 118 of file WkHtmlToPdf.php.

References createTmpFile().

{
$options['input'] = preg_match(self::REGEX_HTML, $input) ? $this->createTmpFile($input) : $input;
$this->objects[] = array_merge($this->pageOptions,$options);
}
WkHtmlToPdf::addToc (   $options = array())

Add a TOC object to the output

Parameters
array$optionsoptional options for the table of contents

Definition at line 141 of file WkHtmlToPdf.php.

{
$options['input'] = "toc";
$this->objects[] = $options;
}
WkHtmlToPdf::createPdf (   $fileName)
protected

Create the temporary PDF file

Definition at line 273 of file WkHtmlToPdf.php.

References getCommand().

Referenced by getPdfFilename().

{
$command = $this->getCommand($fileName);
// we use proc_open with pipes to fetch error output
$descriptors = array(
1 => array('pipe','w'),
2 => array('pipe','w'),
);
$process = proc_open($command, $descriptors, $pipes);
if(is_resource($process)) {
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$result = proc_close($process);
if($result!==0)
$this->error = "Could not run command $command:\n$stderr";
} else
$this->error = "Could not run command $command";
return $this->error===null;
}
WkHtmlToPdf::createTmpFile (   $content)
protected

Create a tmp file with given content

Parameters
string$contentthe file content
Returns
string the path to the created file

Definition at line 307 of file WkHtmlToPdf.php.

References getTmpDir().

Referenced by addPage().

{
$tmpFile = tempnam($this->getTmpDir(),'tmp_WkHtmlToPdf_');
rename($tmpFile, ($tmpFile.='.html'));
file_put_contents($tmpFile, $content);
$this->tmpFiles[] = $tmpFile;
return $tmpFile;
}
WkHtmlToPdf::getCommand (   $filename)
protected
Parameters
string$filenamethe filename of the output file
Returns
string the wkhtmltopdf command string

Definition at line 254 of file WkHtmlToPdf.php.

References renderOptions().

Referenced by createPdf().

{
$command = $this->bin;
$command .= $this->renderOptions($this->options);
foreach($this->objects as $object)
{
$command .= ' '.$object['input'];
unset($object['input']);
$command .= $this->renderOptions($object);
}
return $command.' '.$filename;
}
WkHtmlToPdf::getError ( )
Returns
mixed the detailled error message including the wkhtmltopdf command or null if none

Definition at line 216 of file WkHtmlToPdf.php.

{
return $this->error;
}
WkHtmlToPdf::getPdfFilename ( )
protected
Returns
mixed the temporary PDF filename or false on error (triggers PDf creation)

Definition at line 235 of file WkHtmlToPdf.php.

References createPdf(), and getTmpDir().

Referenced by saveAs(), and send().

{
if($this->tmpFile===null)
{
$tmpFile = tempnam($this->getTmpDir(),'tmp_WkHtmlToPdf_');
if($this->createPdf($tmpFile)===true)
$this->tmpFile = $tmpFile;
else
return false;
}
return $this->tmpFile;
}
WkHtmlToPdf::getTmpDir ( )
Returns
string path to temp directory

Definition at line 224 of file WkHtmlToPdf.php.

Referenced by createTmpFile(), and getPdfFilename().

{
if($this->tmp===null)
$this->tmp = sys_get_temp_dir();
return $this->tmp;
}
WkHtmlToPdf::renderOptions (   $options)
protected
Parameters
array$optionsfor a wkhtml, either global or for an object
Returns
string the string with options

Definition at line 322 of file WkHtmlToPdf.php.

Referenced by getCommand().

{
$out = '';
foreach($options as $key=>$val)
if(is_numeric($key))
$out .= " --$val";
else
$out .= " --$key $val";
return $out;
}
WkHtmlToPdf::saveAs (   $filename)

Save the PDF to given filename (triggers PDF creation)

Parameters
string$filenameto save PDF as
Returns
bool wether PDF was created successfully

Definition at line 153 of file WkHtmlToPdf.php.

References getPdfFilename().

{
if(($pdfFile = $this->getPdfFilename())===false)
return false;
copy($pdfFile,$filename);
return true;
}
WkHtmlToPdf::send (   $filename = null)

Send PDF to client, either inline or as download (triggers PDF creation)

Parameters
mixed$filenamethe filename to send. If empty, the PDF is streamed.
Returns
bool wether PDF was created successfully

Definition at line 168 of file WkHtmlToPdf.php.

References getPdfFilename().

{
if(($pdfFile = $this->getPdfFilename())===false)
return false;
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/pdf');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($pdfFile));
if($filename!==null)
header("Content-Disposition: attachment; filename=\"$filename\"");
readfile($pdfFile);
return true;
}
WkHtmlToPdf::setOptions (   $options)

Set global option(s)

Parameters
array$optionslist of global options to set as name/value pairs

Definition at line 192 of file WkHtmlToPdf.php.

Referenced by __construct().

{
foreach($options as $key=>$val)
if($key==='bin')
$this->bin = $val;
elseif($key==='tmp')
$this->tmp = $val;
elseif(is_int($key))
$this->options[] = $val;
else
$this->options[$key] = $val;
}
WkHtmlToPdf::setPageOptions (   $options = array())
Parameters
array$optionsthat should be applied to all pages as name/value pairs

Definition at line 208 of file WkHtmlToPdf.php.

{
$this->pageOptions = $options;
}

The documentation for this class was generated from the following file: