extranet/includes/print/print.php

171 lines
4.4 KiB
PHP

<?php
define('PRINT_FILETIME', 8);
/**
* Envoi la page à imprimer au navigateur
* @param $filename
* @param $type
* @param $page
* @return void
*/
function print_page($filename, $type = 'pdf')
{
global $firephp;
$path = PATH_SITE.'/cache';
$file = $path.'/'.$filename.'-'.$_SESSION['tabInfo']['login'].'.html';
print_write_content($file);
switch($type)
{
case 'pdf':
$output_file = print_pdf($file);
$content_type = 'application/pdf';
break;
default: exit; break;
}
//Envoi au navigateur
if(file_exists($output_file))
{
$dest = 'I';
switch($dest)
{
case 'I': //Send to standard output
header('Content-type: '.$content_type.'');
header('Content-Length: '.filesize($output_file));
header('Content-MD5: '.base64_encode(md5_file($output_file)));
header('Content-Disposition: inline; filename="'.basename($output_file).'"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
echo file_get_contents($output_file);
exit; // nécessaire pour être certain de ne pas envoyer de fichier corrompu
break;
case 'D': //Download file
header('Content-Tranfer-Encoding: none');
header('Content-Length: '.filesize($output_file));
header('Content-MD5: '.base64_encode(md5_file($output_file)));
header('Content-Type: application/octetstream; name="'.basename($output_file).'"');
header('Content-Disposition: attachment; filename="'.basename($output_file).'"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
//header('Date: '.gmdate(DATE_RFC1123););
//header('Expires: '.gmdate(DATE_RFC1123, time()+1));
//header('Last-Modified: '.gmdate(DATE_RFC1123, filemtime($output_file)));
echo file_get_contents($output_file);
exit; // nécessaire pour être certain de ne pas envoyer de fichier corrompu
break;
}
}
}
/**
* Démarre la capture du contenu
* @return unknown_type
*/
function print_capture()
{
if (ob_start()===FALSE){
//TODO : Gestion des erreurs
}
}
/**
* Retourne si le fichier est périmé ou non
* @param string $fichier
* @return boolean
*/
function print_filetimeover($fichier)
{
$timeover = false;
$dateFile = filemtime($fichier);
$now = mktime(date('G'), date('i'), date('s'), date('m') , date('d'), date('Y'));
$maxTime = mktime(date('G',$dateFile)+PRINT_FILETIME, date('i',$dateFile), date('s',$dateFile), date("m",$dateFile), date("d",$dateFile), date("Y",$dateFile));
if($now>$maxTime) $timeover = true;
return $timeover;
}
/**
* Ecrit le contenu capturé dans un fichier
* @param $file
* @return void
*/
function print_write_content($file)
{
$length = ob_get_length();
$content = ob_get_contents();
if($content !== FALSE){
if( !file_put_contents($file, $content))
{
//TODO : Gestion des erreurs
}
}
if(ob_end_clean()===FALSE){
//TODO : Gestion des erreurs
}
}
/**
* Impression en PDF
* @param $file
* @return unknown_type
*/
function print_pdf($file){
$method = 'wkhtmltopdf';
return $method($file);
}
/**
* Impression en HTML
* @param $file
* @return void
*/
function print_html($file){}
/**
* Imprime un fichier HTML en PDF avec l'utilitaire wkhtmltopdf
* @param $file
* @return unknown_type
*/
function wkhtmltopdf($file)
{
$date = date('d/m/Y H:i:s');
$output_file = str_replace('.html', '.pdf', $file);
if(file_exists($output_file)){ unlink($output_file); }
/*
* -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
*/
$options = '--disable-internal-links';
$options.= ' --footer-right "Page [page] sur [toPage]"';
$options.= ' --header-right "'.$date.'"';
if (stristr(PHP_OS, 'WIN'))
{
$wkhtmltopdf = 'windows/wkhtmltopdf.exe';
}
else
{
switch(WKHTMLTOPDF_ARCH)
{
case 'amd64': $wkhtmltopdf = 'linux/wkhtmltopdf-amd64'; break;
default:
case 'i386': $wkhtmltopdf = 'linux/wkhtmltopdf-i386'; break;
}
}
exec( realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.
$wkhtmltopdf.' '.$options.' "'.$file.'" "'.$output_file.'"');
return $output_file;
}