webservice/library/wkhtmltopdf/wkhtmltopdf.php
2012-03-08 10:58:06 +00:00

68 lines
1.7 KiB
PHP

<?php
class wkhtmltopdf
{
protected $pathWkhtml;
protected $pathIn = '';
protected $pathOut = '';
protected $options = array();
public function __construct()
{
$this->pathWkhtml = DOC_WEB_LOCAL.'/wkhtml/';
}
/**
* Défini les options supplémentaires à l'execution de wkhtmltopdf
* -n, --disable-javascript Do not allow webpages to run javascript.
* --disable-internal-links Do no make local links
* --disable-external-links Do no make links to remote web pages
* --user-style-sheet <url> Specify a user style sheet, to load with every page.
* --print-media-type Use print media-type instead of screen.
* --header-left|right
* @param string $name
* @param string $value
*/
public function setOptions($name, $value = '')
{
$this->options[$name] = $value;
}
/**
* Imprime un fichier HTML en PDF avec l'utilitaire wkhtmltopdf
* @param string $fileIn
* @param string $fileOut
* @return string Nom du fichier
*/
public function exec($fileIn, $fileOut = '')
{
if (empty($fileOut)) {$fileOut = str_replace('.html', '.pdf', $fileIn); }
if(file_exists($fileOut)){ unlink($fileOut); }
$options = '--disable-internal-links';
if ( count($this->options) )
{
foreach ( $this->options as $name => $value )
{
$options.= ' --'.$name;
if ($value!= '') $options.= ' '.$value;
}
}
if (stristr(PHP_OS, 'WIN'))
{
$wkhtmltopdf = 'wkhtmltopdf.exe';
}
else
{
switch(WKHTMLTOPDF_ARCH)
{
default:
case 'amd64': $wkhtmltopdf = 'wkhtmltopdf-amd64'; break;
case 'i386': $wkhtmltopdf = 'wkhtmltopdf-i386'; break;
}
}
$cmd = $this->pathWkhtml.$wkhtmltopdf.' '.$options.' "'.$fileIn.'" "'.$fileOut.'"';
exec( $cmd );
return $fileOut;
}
}