- system/ vervangen door CodeIgniter 3.4.5 (pocketarc/codeigniter, PHP 8.2-8.5 compatibel) - config.php, mimes.php, user_agents.php en error-views bijgewerkt naar 3.4.5 - composer.json: php-constraint "^7.4 || ^8.0" zodat composer install op 8.3 werkt - PHP 8-fatals opgelost: count()/array-functies op FALSE/null, rekenen met lege strings, delen door nul, property zetten op FALSE, curly-brace string offsets, ongedefinieerde constanten, foute curl_setopt in Phantom_model::create_img - dynamic properties gedeclareerd in libraries, optionele parameters voor verplichte opgelost - vergelijkingen waar '' == 0 in PHP 8 anders uitvalt gecast naar (int) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
176 lines
5.6 KiB
PHP
176 lines
5.6 KiB
PHP
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
/**
|
|
* Description of Phantom_model
|
|
*
|
|
* @author Theo Weijmer
|
|
* @date juni 28, 2013 1:41 PM
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
|
|
class Phantom_model extends CI_Model{
|
|
|
|
protected $rootpath ;
|
|
protected $phantomPath ='/jslibrary/phantom/pdfPrint.js';
|
|
protected $pagesize = 'A4';//'A3, A4, A5, Legal, Letter, Tabloid'
|
|
protected $margins = array("1.5cm", "1.5cm", "1.0cm", "1.0cm");
|
|
protected $orientation = 'portrait'; // 'portrait, landscape'
|
|
protected $pdffolder = '/pdf/';
|
|
|
|
|
|
function __construct(){
|
|
parent::__construct();
|
|
$models = array();
|
|
$this->load->model($models);
|
|
$this->rootpath = $_SERVER['DOCUMENT_ROOT'];
|
|
// $this->rootpath = '/Users/liesbeth2/Sites/frameworkpdfs';
|
|
}
|
|
|
|
function get_pdf($data){
|
|
$data = $this->set_variables($data);
|
|
$filename = $data['filename'];
|
|
|
|
$data['filename'] = $this->get_filename($data['filename'], $data['timestamp']);
|
|
$data['header'] = $this->get_header($data['header']);
|
|
$data['pagesize'] = $this->get_page_size($data['pagesize']);
|
|
$data['margins'] = $this->get_margins($data['margins']);
|
|
$data['orientation']= $this->get_orientation($data['orientation']);
|
|
|
|
$this->make_pdf($data);
|
|
$this->download_pdf($data['filename'], $filename);
|
|
}
|
|
|
|
private function set_variables($vars){
|
|
$data = array('url'=>$vars['url'], 'filename'=>$vars['filename']);
|
|
if(!isset($vars['timestamp'])) $vars['timestamp'] = TRUE;
|
|
if(!isset($vars['header'])) $vars['header'] = TRUE;
|
|
if(!isset($vars['pagesize'])) $vars['pagesize'] = FALSE;
|
|
if(!isset($vars['margins'])) $vars['margins'] = FALSE;
|
|
if(!isset($vars['orientation'])) $vars['orientation']= FALSE;
|
|
|
|
$data['timestamp'] = $vars['timestamp'] ;
|
|
$data['header'] = $vars['header'] ;
|
|
$data['pagesize'] = $vars['pagesize'] ;
|
|
$data['margins'] = $vars['margins'] ;
|
|
$data['orientation']= $vars['orientation'];
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function make_pdf($data){
|
|
$strexec = $this->config->item('phantompath').'phantomjs '.$this->rootpath.$this->phantomPath.' ';
|
|
// echo var_dump($data);
|
|
$strexec.= "'".json_encode($data)."'";
|
|
// echo $strexec;
|
|
|
|
exec($strexec);
|
|
}
|
|
|
|
private function download_pdf($filename, $downloadname){
|
|
$this->load->helper('download');
|
|
|
|
$data = file_get_contents($filename); // Read the file's contents
|
|
|
|
force_download($downloadname, $data);
|
|
|
|
chmod($filename, 0777);
|
|
|
|
unlink($filename);
|
|
}
|
|
|
|
private function get_filename($filename, $timestamp){
|
|
if(strtoupper(substr($filename, -4))=='.PDF'){
|
|
$filename = substr($filename, 0, (strlen($filename)-4));
|
|
}
|
|
if($timestamp){
|
|
date_default_timezone_set("UTC");
|
|
$date = new DateTime();
|
|
|
|
$filename.='_'.$date->format('U');
|
|
}else{
|
|
$filename;
|
|
}
|
|
|
|
return $this->rootpath.$this->pdffolder.$filename.'.pdf';
|
|
}
|
|
|
|
private function get_header($header){
|
|
if(!$header){
|
|
return FALSE;
|
|
}elseif(strlen($header)<=1){
|
|
return $this->load->view('reports/default_header', '', TRUE);
|
|
}else{
|
|
return $header;
|
|
}
|
|
}
|
|
|
|
private function get_footer($header){
|
|
if(!$header){
|
|
return FALSE;
|
|
}elseif(strlen($header)<=1){
|
|
return $this->load->view('reports/default_footer', '', TRUE);
|
|
}else{
|
|
return $header;
|
|
}
|
|
}
|
|
|
|
private function get_page_size($pagesize){
|
|
if(!$pagesize){
|
|
return $this->pagesize;
|
|
}elseif(!in_array($pagesize, array('A3', 'A4', 'A5', 'Legal', 'Letter', 'Tabloid'))){
|
|
return $this->pagesize;
|
|
}else{
|
|
return $pagesize;
|
|
}
|
|
|
|
}
|
|
|
|
private function get_margins($margins){
|
|
$add='';
|
|
if(!$margins){
|
|
return $this->margins;
|
|
}elseif(!is_array($margins)){
|
|
$margins = str_replace('cm', '', $margins);
|
|
|
|
if(!is_numeric($margins)) return $this->margins;
|
|
|
|
return array($margins.'cm',$margins.'cm',$margins.'cm',$margins.'cm');
|
|
}elseif(count($margins)==2){
|
|
$margin1 = str_replace('cm', '', $margins[0]);
|
|
$margin2 = str_replace('cm', '', $margins[1]);
|
|
|
|
if(!is_numeric($margin1) && !is_numeric($margins2)) return $this->margins;
|
|
|
|
if(!is_numeric($margin1)){
|
|
return array($this->margins[0].'cm', $margins2.'cm', $this->margins[2].'cm', $margins2.'cm');
|
|
}elseif(!is_numeric($margin1)){
|
|
return array($margins1.'cm', $this->margins[1].'cm', $margins1.'cm', $this->margins[3].'cm');
|
|
}else{
|
|
return array($margins1.'cm', $margins2.'cm', $margins1.'cm', $margins2.'cm');
|
|
}
|
|
}elseif(count($margins)==4){
|
|
for($i=0; $i<=3; $i++){
|
|
if(is_numeric(str_replace('cm', '',$margins[$i]))){
|
|
$newmargins[$i] = str_replace('cm', '',$margins[$i]). 'cm';
|
|
}else{
|
|
$newmargins[$i] = str_replace('cm', '',$this->margins[$i]). 'cm';
|
|
}
|
|
}
|
|
|
|
return $newmargins;
|
|
}else{
|
|
return $this->margins;
|
|
}
|
|
}
|
|
|
|
private function get_orientation($orientation){
|
|
if(in_array($orientation, array('portrait', 'landscape'))){
|
|
return $orientation;
|
|
}else{
|
|
return $this->orientation;
|
|
}
|
|
}
|
|
|
|
} |