-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPdf.php
More file actions
131 lines (126 loc) · 3.01 KB
/
MPdf.php
File metadata and controls
131 lines (126 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* M PHP Framework
*
* @package M
* @subpackage MPdf
* @author Arnaud Sellenet <demental at github>
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @version 0.1
*/
// disable DOMPDF's internal autoloader if you are using Composer
define('DOMPDF_ENABLE_AUTOLOAD', false);
require __DIR__.'/../../dompdf/dompdf/dompdf_config.inc.php';
/**
*
* PDF generation class using dompdf external library and Mtpl as the template engine
*
*/
class MPdf extends Maman {
public $template;
protected $view;
protected $pdf;
function __construct($config,$settings = null) {
if(!is_array($settings)) {
$settings = array();
}
$defaults = array('format'=>'a4','orientation'=>'portrait');
foreach($defaults as $setting=>$default) {
if(!$settings[$setting]) {
$settings[$setting] = $default;
}
}
$this->setConfig($config);
$this->view = new Mtpl($this->getConfig('template_dir'));
$this->pdf = new DOMPDF();
$this->pdf->set_paper($settings['format'],$settings['orientation']);
}
public static function merge($mpdfs,$filename,$outputmode = 'I')
{
$p = new \fpdi\FPDI();
$n=0;
foreach($mpdfs AS $mpdf) {
if($mpdf instanceOf MPdf){
$mpdf->write(TMP_PATH.'/temmpdf'.$n.'.pdf');
$file = TMP_PATH.'/temmpdf'.$n.'.pdf';
} else {
$file=$mpdf;
}
$n++;
$files[]=$file;
$pagecount = $p->setSourceFile($file);
for ($i = 1; $i <= $pagecount; $i++) {
$tplidx = $p->ImportPage($i);
$p->AddPage();
$p->useTemplate($tplidx);
}
}
foreach($files as $v){
// @unlink($v);
}
$p->output($filename,$outputmode);
}
function setVars($vars) {
$this->fetched = false;
$this->view->assignArray($vars);
}
function setTemplate($tpl) {
$this->template = $tpl;
}
/**
* Returns a MPdf_Form instance
* @param string source file
*
*/
public static function &formfactory($formfile) {
$pdf = new MPdf_Form($formfile);
return $pdf;
}
public static function &factory ( $template,$settings = null )
{
$opt = array('all'=>PEAR::getStaticProperty('MPdf','global'));
$pdf = new MPdf($opt,$settings);
$pdf->setConfigValue('template',$template);
return $pdf;
}
public function serve($filename)
{
$this->fetch();
$this->pdf->stream($filename);
}
public function write($filename)
{
$this->fetch();
$res = $this->pdf->output();
$fp = fopen($filename,'w+');
fwrite($fp,$res);
fclose($fp);
}
public function fetch()
{
if($this->fetched) {return;}
$html = utf8_decode($this->view->fetch($this->getConfig('template')));
// $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
$this->pdf->load_html($html);
$this->pdf->render();
$this->fetched = 1;
}
public function &getPdf()
{
return $this->pdf;
}
public function getContents()
{
return $this->view->fetch($this->getConfig('template'));
}
public function __toString()
{
$this->fetch();
return $this->pdf->output();
}
public function __destruct()
{
unset($this->view);
unset($this->pdf);
}
}