|
1
|
|
|
<?php |
|
2
|
|
|
// $Id: makepdf_class.php 8112 2011-11-06 13:41:14Z beckmi $ |
|
3
|
|
|
// ------------------------------------------------------------------------ // |
|
4
|
|
|
// XOOPS - PHP Content Management System // |
|
5
|
|
|
// Copyright (c) 2000 XOOPS.org // |
|
6
|
|
|
// <http://www.xoops.org/> // |
|
7
|
|
|
// ------------------------------------------------------------------------ // |
|
8
|
|
|
// This program is free software; you can redistribute it and/or modify // |
|
9
|
|
|
// it under the terms of the GNU General Public License as published by // |
|
10
|
|
|
// the Free Software Foundation; either version 2 of the License, or // |
|
11
|
|
|
// (at your option) any later version. // |
|
12
|
|
|
// // |
|
13
|
|
|
// You may not change or alter any portion of this comment or credits // |
|
14
|
|
|
// of supporting developers from this source code or any supporting // |
|
15
|
|
|
// source code which is considered copyrighted (c) material of the // |
|
16
|
|
|
// original comment or credit authors. // |
|
17
|
|
|
// // |
|
18
|
|
|
// This program is distributed in the hope that it will be useful, // |
|
19
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|
20
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
|
21
|
|
|
// GNU General Public License for more details. // |
|
22
|
|
|
// // |
|
23
|
|
|
// You should have received a copy of the GNU General Public License // |
|
24
|
|
|
// along with this program; if not, write to the Free Software // |
|
25
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
|
26
|
|
|
// ------------------------------------------------------------------------ // |
|
27
|
|
|
// based on: |
|
28
|
|
|
// ------------------------------------------------- |
|
29
|
|
|
// St@neCold |
|
30
|
|
|
// HTML2PDF by Cl�ment Lavoillotte |
|
31
|
|
|
// [email protected] |
|
32
|
|
|
// [email protected] |
|
33
|
|
|
// http://www.streetpc.tk |
|
34
|
|
|
if (!defined('XOOPS_ROOT_PATH')) { |
|
35
|
|
|
die('XOOPS root path not defined'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
//function hex2dec |
|
39
|
|
|
//returns an associative array (keys: R,G,B) from |
|
40
|
|
|
//a hex html code (e.g. #3FE5AA) |
|
41
|
|
|
function hex2dec($couleur = '#000000'){ |
|
42
|
|
|
$R = substr($couleur, 1, 2); |
|
43
|
|
|
$rouge = hexdec($R); |
|
44
|
|
|
$V = substr($couleur, 3, 2); |
|
45
|
|
|
$vert = hexdec($V); |
|
46
|
|
|
$B = substr($couleur, 5, 2); |
|
47
|
|
|
$bleu = hexdec($B); |
|
48
|
|
|
$tbl_couleur = array(); |
|
49
|
|
|
$tbl_couleur['R']=$rouge; |
|
50
|
|
|
$tbl_couleur['G']=$vert; |
|
51
|
|
|
$tbl_couleur['B']=$bleu; |
|
52
|
|
|
|
|
53
|
|
|
return $tbl_couleur; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
//conversion pixel -> millimeter in 72 dpi |
|
57
|
|
|
function px2mm($px){ |
|
58
|
|
|
return $px*25.4/72; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
function txtentities($html){ |
|
62
|
|
|
$trans = get_html_translation_table(HTML_ENTITIES); |
|
63
|
|
|
$trans = array_flip($trans); |
|
64
|
|
|
|
|
65
|
|
|
return strtr($html, $trans); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
//////////////////////////////////// |
|
69
|
|
|
|
|
70
|
|
|
//class PDF extends FPDF |
|
71
|
|
|
class PDF extends PDF_language |
|
72
|
|
|
{ |
|
73
|
|
|
//variables of html parser |
|
74
|
|
|
var $B; |
|
75
|
|
|
var $I; |
|
76
|
|
|
var $U; |
|
77
|
|
|
var $HREF; |
|
78
|
|
|
var $CENTER=''; |
|
79
|
|
|
var $ALIGN=''; |
|
80
|
|
|
var $IMG; |
|
81
|
|
|
var $SRC; |
|
82
|
|
|
var $WIDTH; |
|
83
|
|
|
var $HEIGHT; |
|
84
|
|
|
var $fontList; |
|
85
|
|
|
var $issetfont; |
|
86
|
|
|
var $issetcolor; |
|
87
|
|
|
var $iminfo=array(0,0); |
|
88
|
|
|
|
|
89
|
|
|
function PDF($orientation='P',$unit='mm',$format='A4') |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
//Call parent constructor |
|
92
|
|
|
$this->PDF_language($orientation,$unit,$format); |
|
93
|
|
|
//Initialization |
|
94
|
|
|
$this->B=0; |
|
95
|
|
|
$this->I=0; |
|
96
|
|
|
$this->U=0; |
|
97
|
|
|
$this->HREF=''; |
|
98
|
|
|
$this->CENTER=''; |
|
99
|
|
|
$this->ALIGN=''; |
|
100
|
|
|
$this->IMG=''; |
|
101
|
|
|
$this->SRC=''; |
|
102
|
|
|
$this->WIDTH=''; |
|
103
|
|
|
$this->HEIGHT=''; |
|
104
|
|
|
$this->fontlist=array('arial', 'times', 'courier', 'helvetica', 'symbol'); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$this->issetfont=false; |
|
107
|
|
|
$this->issetcolor=false; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
////////////////////////////////////// |
|
111
|
|
|
//html parser |
|
112
|
|
|
|
|
113
|
|
|
function WriteHTML($html,$scale) |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
// $html=strip_tags($html,"<b><u><i><a><img><p><br><strong><em><font><tr><blockquote>"); //remove all unsupported tags |
|
116
|
|
|
$html=str_replace("\n",' ',$html); //replace carriage returns by spaces |
|
117
|
|
|
$a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //explodes the string |
|
118
|
|
|
foreach($a as $i=>$e) |
|
119
|
|
|
{ |
|
120
|
|
|
if($i%2==0) |
|
121
|
|
|
{ |
|
122
|
|
|
//Text |
|
123
|
|
|
if($this->HREF) |
|
124
|
|
|
$this->PutLink($this->HREF,$e); |
|
125
|
|
|
elseif($this->IMG) |
|
126
|
|
|
$this->PutImage($this->SRC,$scale); |
|
127
|
|
|
elseif($this->CENTER) |
|
128
|
|
|
$this->Cell(0,5,$e,0,1,'C'); |
|
129
|
|
|
elseif($this->ALIGN == 'center') |
|
130
|
|
|
$this->Cell(0,5,$e,0,1,'C'); |
|
131
|
|
|
elseif($this->ALIGN == 'right') |
|
132
|
|
|
$this->Cell(0,5,$e,0,1,'R'); |
|
133
|
|
|
elseif($this->ALIGN == 'left') |
|
134
|
|
|
$this->Cell(0,5,$e,0,1,'L'); |
|
135
|
|
|
else |
|
136
|
|
|
$this->Write(5,stripslashes(txtentities($e))); |
|
137
|
|
|
} |
|
138
|
|
|
else |
|
139
|
|
|
{ |
|
140
|
|
|
//Tag |
|
141
|
|
|
if($e{0}=='/') |
|
142
|
|
|
$this->CloseTag(strtoupper(substr($e,1))); |
|
143
|
|
|
else |
|
144
|
|
|
{ |
|
145
|
|
|
//Extract attributes |
|
146
|
|
|
$a2=explode(' ',$e); |
|
147
|
|
|
$tag=strtoupper(array_shift($a2)); |
|
148
|
|
|
$attr=array(); |
|
149
|
|
|
foreach($a2 as $v) |
|
150
|
|
|
if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3)) |
|
151
|
|
|
$attr[strtoupper($a3[1])]=$a3[2]; |
|
152
|
|
|
$this->OpenTag($tag,$attr,$scale); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
function OpenTag($tag,$attr,$scale) |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
//Opening tag |
|
161
|
|
|
switch($tag){ |
|
162
|
|
|
case 'STRONG': |
|
163
|
|
|
$this->SetStyle('B',true); |
|
164
|
|
|
break; |
|
165
|
|
|
case 'EM': |
|
166
|
|
|
$this->SetStyle('I',true); |
|
167
|
|
|
break; |
|
168
|
|
|
case 'B': |
|
169
|
|
|
case 'I': |
|
170
|
|
|
case 'U': |
|
171
|
|
|
$this->SetStyle($tag,true); |
|
172
|
|
|
break; |
|
173
|
|
|
case 'A': |
|
174
|
|
|
$this->HREF=$attr['HREF']; |
|
175
|
|
|
break; |
|
176
|
|
|
case 'P': |
|
177
|
|
|
$this->ALIGN=$attr['ALIGN']; |
|
178
|
|
|
break; |
|
179
|
|
|
case 'IMG': |
|
180
|
|
|
$this->IMG=$attr['IMG']; |
|
181
|
|
|
$this->SRC=$attr['SRC']; |
|
182
|
|
|
$this->WIDTH=$attr['WIDTH']; |
|
183
|
|
|
$this->HEIGHT=$attr['HEIGHT']; |
|
184
|
|
|
$this->PutImage($attr[SRC],$scale); |
|
185
|
|
|
break; |
|
186
|
|
|
case 'TR': |
|
187
|
|
|
case 'BLOCKQUOTE': |
|
188
|
|
|
case 'BR': |
|
189
|
|
|
$this->Ln(5); |
|
190
|
|
|
break; |
|
191
|
|
|
case 'HR': |
|
192
|
|
|
if( $attr['WIDTH'] != '' ) $Width = $attr['WIDTH']; |
|
193
|
|
|
else $Width = $this->w - $this->lMargin-$this->rMargin; |
|
194
|
|
|
$this->Ln(2); |
|
195
|
|
|
$x = $this->GetX(); |
|
196
|
|
|
$y = $this->GetY(); |
|
197
|
|
|
$this->SetLineWidth(0.4); |
|
198
|
|
|
$this->Line($x,$y,$x+$Width,$y); |
|
199
|
|
|
$this->SetLineWidth(0.2); |
|
200
|
|
|
$this->Ln(2); |
|
201
|
|
|
break; |
|
202
|
|
|
case 'FONT': |
|
203
|
|
|
if (isset($attr['COLOR']) and $attr['COLOR']!='') { |
|
204
|
|
|
$coul=hex2dec($attr['COLOR']); |
|
205
|
|
|
$this->SetTextColor($coul['R'],$coul['G'],$coul['B']); |
|
206
|
|
|
$this->issetcolor=true; |
|
207
|
|
|
} |
|
208
|
|
|
if (isset($attr['FACE']) and in_array(strtolower($attr['FACE']), $this->fontlist)) { |
|
|
|
|
|
|
209
|
|
|
$this->SetFont(strtolower($attr['FACE'])); |
|
210
|
|
|
$this->issetfont=true; |
|
211
|
|
|
} |
|
212
|
|
|
break; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
function CloseTag($tag) |
|
|
|
|
|
|
217
|
|
|
{ |
|
218
|
|
|
//Closing tag |
|
219
|
|
|
if($tag=='STRONG') |
|
220
|
|
|
$tag='B'; |
|
221
|
|
|
if($tag=='EM') |
|
222
|
|
|
$tag='I'; |
|
223
|
|
|
if($tag=='B' or $tag=='I' or $tag=='U') |
|
224
|
|
|
$this->SetStyle($tag,false); |
|
225
|
|
|
if($tag=='A') |
|
226
|
|
|
$this->HREF=''; |
|
227
|
|
|
if($tag=='P') |
|
228
|
|
|
$this->ALIGN=''; |
|
229
|
|
|
if($tag=='IMG'){ |
|
230
|
|
|
$this->IMG=''; |
|
231
|
|
|
$this->SRC=''; |
|
232
|
|
|
$this->WIDTH=''; |
|
233
|
|
|
$this->HEIGHT=''; |
|
234
|
|
|
} |
|
235
|
|
|
if($tag=='FONT'){ |
|
236
|
|
|
if ($this->issetcolor==true) { |
|
|
|
|
|
|
237
|
|
|
$this->SetTextColor(0); |
|
238
|
|
|
} |
|
239
|
|
|
if ($this->issetfont) { |
|
240
|
|
|
$this->SetFont('arial'); |
|
241
|
|
|
$this->issetfont=false; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
function SetStyle($tag,$enable) |
|
|
|
|
|
|
247
|
|
|
{ |
|
248
|
|
|
//Modify style and select corresponding font |
|
249
|
|
|
$this->$tag+=($enable ? 1 : -1); |
|
250
|
|
|
$style=''; |
|
251
|
|
|
foreach(array('B','I','U') as $s) |
|
252
|
|
|
if($this->$s>0) |
|
253
|
|
|
$style.=$s; |
|
254
|
|
|
$this->SetFont('',$style); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
function PutLink($URL,$txt) |
|
|
|
|
|
|
258
|
|
|
{ |
|
259
|
|
|
//Put a hyperlink |
|
260
|
|
|
$this->SetTextColor(0,0,255); |
|
261
|
|
|
$this->SetStyle('U',true); |
|
262
|
|
|
$this->Write(5,$txt,$URL); |
|
263
|
|
|
$this->SetStyle('U',false); |
|
264
|
|
|
$this->SetTextColor(0); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
//put the image in pdf with scaling... |
|
268
|
|
|
//width and height-options inside the IMG-Tag are ignored, |
|
269
|
|
|
//we get the image info directly from PHP... |
|
270
|
|
|
//$scale is the global scaling factor, passing through from WriteHTML() |
|
271
|
|
|
//(c)2004/03/12 by St@neCold |
|
272
|
|
|
function PutImage($url,$scale) |
|
|
|
|
|
|
273
|
|
|
{ |
|
274
|
|
|
if($scale<0) $scale=0; |
|
275
|
|
|
//$scale<=0: put NO image inside the pdf! |
|
276
|
|
|
if($scale>0){ |
|
277
|
|
|
$xsflag=0; |
|
|
|
|
|
|
278
|
|
|
$ysflag=0; |
|
|
|
|
|
|
279
|
|
|
$yhflag=0; |
|
|
|
|
|
|
280
|
|
|
$xscale=1; |
|
281
|
|
|
$yscale=1; |
|
282
|
|
|
//get image info |
|
283
|
|
|
$oposy=$this->GetY(); |
|
284
|
|
|
$url = str_replace(XOOPS_URL, XOOPS_ROOT_PATH, $url); |
|
285
|
|
|
$iminfo=@getimagesize($url); |
|
286
|
|
|
$iw=$scale * px2mm($iminfo[0]); |
|
287
|
|
|
$ih=$scale * px2mm($iminfo[1]); |
|
288
|
|
|
$iw = $iw ?$iw:1; |
|
289
|
|
|
$ih = $ih ?$ih:1; |
|
290
|
|
|
$nw=$iw; |
|
291
|
|
|
$nh=$ih; |
|
292
|
|
|
//resizing in x-direction |
|
293
|
|
|
$xsflag=0; |
|
294
|
|
View Code Duplication |
if($iw>150) { |
|
|
|
|
|
|
295
|
|
|
$xscale=150 / $iw; |
|
296
|
|
|
$yscale=$xscale; |
|
297
|
|
|
$nw=$xscale * $iw; |
|
298
|
|
|
$nh=$xscale * $ih; |
|
299
|
|
|
$xsflag=1; |
|
300
|
|
|
} |
|
301
|
|
|
//now eventually resizing in y-direction |
|
302
|
|
|
$ysflag=0; |
|
303
|
|
View Code Duplication |
if(($oposy+$nh)>250){ |
|
|
|
|
|
|
304
|
|
|
$yscale=(250-$oposy)/$ih; |
|
305
|
|
|
$nw=$yscale * $iw; |
|
306
|
|
|
$nh=$yscale * $ih; |
|
307
|
|
|
$ysflag=1; |
|
308
|
|
|
} |
|
309
|
|
|
//uups, if the scaling factor of resized image is < 0.33 |
|
310
|
|
|
//remark: without(!) the global factor $scale! |
|
311
|
|
|
//that's hard -> on the next page please... |
|
312
|
|
|
$yhflag=0; |
|
313
|
|
|
if($yscale<0.33 and ($xsflag==1 or $ysflag==1)) { |
|
314
|
|
|
$nw=$xscale * $iw; |
|
315
|
|
|
$nh=$xscale * $ih; |
|
316
|
|
|
$ysflag==0; |
|
317
|
|
|
$xsflag==1; |
|
318
|
|
|
$yhflag=1; |
|
319
|
|
|
} |
|
320
|
|
|
if($yhflag==1) $this->AddPage(); |
|
321
|
|
|
$oposy=$this->GetY(); |
|
322
|
|
|
$this->Image($url, $this->GetX(), $this->GetY(), $nw, $nh); |
|
323
|
|
|
$this->SetY($oposy+$nh); |
|
324
|
|
|
if($yhflag==0 and $ysflag==1) $this->AddPage(); |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
function Footer() |
|
|
|
|
|
|
329
|
|
|
{ |
|
330
|
|
|
//print footer |
|
331
|
|
|
// |
|
332
|
|
|
global $pdf_config; |
|
333
|
|
|
|
|
334
|
|
|
//date+time |
|
335
|
|
|
$printpdfdate = date($pdf_config['dateformat']); |
|
336
|
|
|
//Position and Font |
|
337
|
|
|
$this->SetXY(25,-25); |
|
338
|
|
|
$this->SetTextColor(0,0,255); |
|
339
|
|
|
$this->SetFont($pdf_config['font']['footer']['family'],$pdf_config['font']['footer']['style'],$pdf_config['font']['footer']['size']); |
|
340
|
|
|
//Link+Page number |
|
341
|
|
|
$this->Cell(0,10,$pdf_config['url'],'T',0,'L',0,$pdf_config['url']); |
|
342
|
|
|
$pn=$this->PageNo(); |
|
343
|
|
|
$out=$printpdfdate; |
|
344
|
|
|
$out.=' - '; |
|
345
|
|
|
$out.=$pn; |
|
346
|
|
|
$this->SetFont($pdf_config['font']['footer']['family'],$pdf_config['font']['footer']['style'],$pdf_config['font']['footer']['size']); |
|
347
|
|
|
$this->Cell(0,10,$out,'T',0,'R',0,$pdf_config['mail']); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
function _parsegif($file) |
|
|
|
|
|
|
351
|
|
|
{ |
|
352
|
|
|
require_once 'gif.php'; //GIF class in pure PHP from Yamasoft (http://www.yamasoft.com/php-gif.zip) |
|
353
|
|
|
|
|
354
|
|
|
$h=0; |
|
|
|
|
|
|
355
|
|
|
$w=0; |
|
|
|
|
|
|
356
|
|
|
$gif=new CGIF(); |
|
357
|
|
|
if(!$gif){ |
|
358
|
|
|
$this->Error("GIF parser: unable to open file $file"); |
|
359
|
|
|
|
|
360
|
|
|
return null; |
|
361
|
|
|
} |
|
362
|
|
|
if(empty($gif->m_img->m_data)) return null; |
|
363
|
|
|
|
|
364
|
|
|
if($gif->m_img->m_gih->m_bLocalClr) { |
|
365
|
|
|
$nColors = $gif->m_img->m_gih->m_nTableSize; |
|
366
|
|
|
$pal = $gif->m_img->m_gih->m_colorTable->toString(); |
|
367
|
|
|
if($bgColor != -1) { |
|
|
|
|
|
|
368
|
|
|
$bgColor = $gif->m_img->m_gih->m_colorTable->colorIndex($bgColor); |
|
|
|
|
|
|
369
|
|
|
} |
|
370
|
|
|
$colspace='Indexed'; |
|
371
|
|
|
} elseif($gif->m_gfh->m_bGlobalClr) { |
|
372
|
|
|
$nColors = $gif->m_gfh->m_nTableSize; |
|
373
|
|
|
$pal = $gif->m_gfh->m_colorTable->toString(); |
|
374
|
|
|
if($bgColor != -1) { |
|
|
|
|
|
|
375
|
|
|
$bgColor = $gif->m_gfh->m_colorTable->colorIndex($bgColor); |
|
|
|
|
|
|
376
|
|
|
} |
|
377
|
|
|
$colspace='Indexed'; |
|
378
|
|
|
} else { |
|
379
|
|
|
$nColors = 0; |
|
380
|
|
|
$bgColor = -1; |
|
|
|
|
|
|
381
|
|
|
$colspace='DeviceGray'; |
|
382
|
|
|
$pal=''; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
$trns=''; |
|
386
|
|
|
if($gif->m_img->m_bTrans && ($nColors > 0)) { |
|
387
|
|
|
$trns=array($gif->m_img->m_nTrans); |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
$data=$gif->m_img->m_data; |
|
391
|
|
|
$w=$gif->m_gfh->m_nWidth; |
|
392
|
|
|
$h=$gif->m_gfh->m_nHeight; |
|
393
|
|
|
|
|
394
|
|
|
if($colspace=='Indexed' and empty($pal)) |
|
395
|
|
|
$this->Error('Missing palette in '.$file); |
|
396
|
|
|
|
|
397
|
|
|
if ($this->compress) { |
|
398
|
|
|
$data=gzcompress($data); |
|
399
|
|
|
|
|
400
|
|
|
return array( 'w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>8, 'f'=>'FlateDecode', 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data); |
|
401
|
|
|
} else { |
|
402
|
|
|
return array( 'w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>8, 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data); |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
} |
|
406
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.