forked from renatoac83/html2fpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml2fpdf.php
More file actions
2910 lines (2809 loc) · 112 KB
/
html2fpdf.php
File metadata and controls
2910 lines (2809 loc) · 112 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
*** General-use version
DEBUG HINT:
- Inside function printbuffer make $fill=1
- Inside function Cell make:
if($fill==1 or $border==1)
{
// if ($fill==1) $op=($border==1) ? 'B' : 'f';
// else $op='S';
$op='S';
- Following these 2 steps you will be able to see the cell's boundaries
WARNING: When adding a new tag support, also add its name inside the function DisableTags()'s very long string
ODDITIES (?):
. It seems like saved['border'] and saved['bgcolor'] are useless inside the FlowingBlock...
These 2 attributes do the same thing?!?:
. $this->currentfont - mine
. $this->CurrentFont - fpdf's
TODO (in the future...):
- Make font-family, font-size, lineheight customizable
- Increase number of HTML/CSS tags/properties, Image/Font Types, recognized/supported
- allow BMP support? (tried with http://phpthumb.sourceforge.net/ but failed)
- Improve CSS support
- support image side-by-side or one-below-another or both?
- Improve code clarity even more (modularize and get better var names like on textbuffer array's indexes for example)
//////////////////////////////////////////////////////////////////////////////
//////////////DO NOT MODIFY THE CONTENTS OF THIS BOX//////////////////////////
//////////////////////////////////////////////////////////////////////////////
// //
// HTML2FPDF is a php script to read a HTML text and generate a PDF file. //
// Copyright (C) 2004-2005 Renato AC //
// This script may be distributed as long as the following files are kept //
// together: //
// //
// fpdf.php, html2fpdf.php, gif.php,htmltoolkit.php,license.txt,credits.txt //
// //
//////////////////////////////////////////////////////////////////////////////
Misc. Observations:
- CSS + align = bug! (?)
OBS1: para textos de mais de 1 página, talvez tenha que juntar varios $texto_artigo
antes de mandar gerar o PDF, para que o PDF gerado seja completo.
OBS2: there are 2 types of spaces 32 and 160 (ascii values)
OBS3: //! is a special comment to be used with source2doc.php, a script I created
in order to generate the doc on the site html2fpdf.sf.net
OBS4: var $LineWidth; // line width in user unit - use this to make css thin/medium/thick work
OBS5: Images and Textareas: when they are inserted you can only type below them (==display:block)
OBS6: Optimized to 'A4' paper (default font: Arial , normal , size 11 )
OBS7: Regexp + Perl ([preg]accepts non-greedy quantifiers while PHP[ereg] does not)
Perl: '/regexp/x' where x == option ( x = i:ignore case , x = s: DOT gets \n as well)
========================END OF INITIAL COMMENTS=================================
*/
define('HTML2FPDF_VERSION','3.0(beta)');
if (!defined('RELATIVE_PATH')) define('RELATIVE_PATH','');
if (!defined('FPDF_FONTPATH')) define('FPDF_FONTPATH','font/');
require_once(RELATIVE_PATH.'fpdf.php');
require_once(RELATIVE_PATH.'htmltoolkit.php');
class HTML2FPDF extends FPDF
{
//internal attributes
var $HREF; //! string
var $pgwidth; //! float
var $fontlist; //! array
var $issetfont; //! bool
var $issetcolor; //! bool
var $titulo; //! string
var $oldx; //! float
var $oldy; //! float
var $B; //! int
var $U; //! int
var $I; //! int
var $tablestart; //! bool
var $tdbegin; //! bool
var $table; //! array
var $cell; //! array
var $col; //! int
var $row; //! int
var $divbegin; //! bool
var $divalign; //! char
var $divwidth; //! float
var $divheight; //! float
var $divbgcolor; //! bool
var $divcolor; //! bool
var $divborder; //! int
var $divrevert; //! bool
var $listlvl; //! int
var $listnum; //! int
var $listtype; //! string
//array(lvl,# of occurrences)
var $listoccur; //! array
//array(lvl,occurrence,type,maxnum)
var $listlist; //! array
//array(lvl,num,content,type)
var $listitem; //! array
var $buffer_on; //! bool
var $pbegin; //! bool
var $pjustfinished; //! bool
var $blockjustfinished; //! bool
var $SUP; //! bool
var $SUB; //! bool
var $toupper; //! bool
var $tolower; //! bool
var $dash_on; //! bool
var $dotted_on; //! bool
var $strike; //! bool
var $CSS; //! array
var $cssbegin; //! bool
var $backupcss; //! array
var $textbuffer; //! array
var $currentstyle; //! string
var $currentfont; //! string
var $colorarray; //! array
var $bgcolorarray; //! array
var $internallink; //! array
var $enabledtags; //! string
var $lineheight; //! int
var $basepath; //! string
// array('COLOR','WIDTH','OLDWIDTH')
var $outlineparam; //! array
var $outline_on; //! bool
var $specialcontent; //! string
var $selectoption; //! array
//options attributes
var $usecss; //! bool
var $usepre; //! bool
var $usetableheader; //! bool
var $shownoimg; //! bool
function HTML2FPDF($orientation='P',$unit='mm',$format='A4')
{
//! @desc Constructor
//! @return An object (a class instance)
//Call parent constructor
$this->FPDF($orientation,$unit,$format);
//To make the function Footer() work properly
$this->AliasNbPages();
//Enable all tags as default
$this->DisableTags();
//Set default display preferences
$this->DisplayPreferences('');
//Initialization of the attributes
$this->SetFont('Arial','',11); // Changeable?(not yet...)
$this->lineheight = 5; // Related to FontSizePt == 11
$this->pgwidth = $this->fw - $this->lMargin - $this->rMargin ;
$this->SetFillColor(255);
$this->HREF='';
$this->titulo='';
$this->oldx=-1;
$this->oldy=-1;
$this->B=0;
$this->U=0;
$this->I=0;
$this->listlvl=0;
$this->listnum=0;
$this->listtype='';
$this->listoccur=array();
$this->listlist=array();
$this->listitem=array();
$this->tablestart=false;
$this->tdbegin=false;
$this->table=array();
$this->cell=array();
$this->col=-1;
$this->row=-1;
$this->divbegin=false;
$this->divalign="L";
$this->divwidth=0;
$this->divheight=0;
$this->divbgcolor=false;
$this->divcolor=false;
$this->divborder=0;
$this->divrevert=false;
$this->fontlist=array("arial","times","courier","helvetica","symbol","monospace","serif","sans");
$this->issetfont=false;
$this->issetcolor=false;
$this->pbegin=false;
$this->pjustfinished=false;
$this->blockjustfinished = true; //in order to eliminate exceeding left-side spaces
$this->toupper=false;
$this->tolower=false;
$this->dash_on=false;
$this->dotted_on=false;
$this->SUP=false;
$this->SUB=false;
$this->buffer_on=false;
$this->strike=false;
$this->currentfont='';
$this->currentstyle='';
$this->colorarray=array();
$this->bgcolorarray=array();
$this->cssbegin=false;
$this->textbuffer=array();
$this->CSS=array();
$this->backupcss=array();
$this->internallink=array();
$this->basepath = "";
$this->outlineparam = array();
$this->outline_on = false;
$this->specialcontent = '';
$this->selectoption = array();
$this->shownoimg=false;
$this->usetableheader=false;
$this->usecss=true;
$this->usepre=true;
}
function setBasePath($str)
{
//! @desc Inform the script where the html file is (full path - e.g. http://www.google.com/dir1/dir2/dir3/file.html ) in order to adjust HREF and SRC links. No-Parameter: The directory where this script is.
//! @return void
$this->basepath = dirname($str) . "/";
$this->basepath = str_replace("\\","/",$this->basepath); //If on Windows
}
function ShowNOIMG_GIF($opt=true)
{
//! @desc Enable/Disable Displaying the no_img.gif when an image is not found. No-Parameter: Enable
//! @return void
$this->shownoimg=$opt;
}
function UseCSS($opt=true)
{
//! @desc Enable/Disable CSS recognition. No-Parameter: Enable
//! @return void
$this->usecss=$opt;
}
function UseTableHeader($opt=true)
{
//! @desc Enable/Disable Table Header to appear every new page. No-Parameter: Enable
//! @return void
$this->usetableheader=$opt;
}
function UsePRE($opt=true)
{
//! @desc Enable/Disable pre tag recognition. No-Parameter: Enable
//! @return void
$this->usepre=$opt;
}
//Page header
function Header($content='')
{
//! @return void
//! @desc The header is printed in every page.
if($this->usetableheader and $content != '')
{
$y = $this->y;
foreach($content as $tableheader)
{
$this->y = $y;
//Set some cell values
$x = $tableheader['x'];
$w = $tableheader['w'];
$h = $tableheader['h'];
$va = $tableheader['va'];
$mih = $tableheader['mih'];
$fill = $tableheader['bgcolor'];
$border = $tableheader['border'];
$align = $tableheader['a'];
//Align
$this->divalign=$align;
$this->x = $x;
//Vertical align
if (!isset($va) || $va=='M') $this->y += ($h-$mih)/2;
elseif (isset($va) && $va=='B') $this->y += $h-$mih;
if ($fill)
{
$color = ConvertColor($fill);
$this->SetFillColor($color['R'],$color['G'],$color['B']);
$this->Rect($x, $y, $w, $h, 'F');
}
//Border
if (isset($border) and $border != 'all') $this->_tableRect($x, $y, $w, $h, $border);
elseif (isset($border) && $border == 'all') $this->Rect($x, $y, $w, $h);
//Print cell content
$this->divwidth = $w-2;
$this->divheight = 1.1*$this->lineheight;
$textbuffer = $tableheader['textbuffer'];
if (!empty($textbuffer)) $this->printbuffer($textbuffer,false,true/*inside a table*/);
$textbuffer = array();
}
$this->y = $y + $h; //Update y coordinate
}//end of 'if usetableheader ...'
}
//Page footer
function Footer()
{
//! @return void
//! @desc The footer is printed in every page!
//Position at 1.0 cm from bottom
$this->SetY(-10);
//Copyright //especial para esta versão
$this->SetFont('Arial','B',9);
$this->SetTextColor(0);
//Arial italic 9
$this->SetFont('Arial','I',9);
//Page number
$this->Cell(0,10,$this->PageNo().'/{nb}',0,0,'C');
//Return Font to normal
$this->SetFont('Arial','',11);
}
///////////////////
/// HTML parser ///
///////////////////
function WriteHTML($html)
{
//! @desc HTML parser
//! @return void
/* $e == content */
$this->ReadMetaTags($html);
$html = AdjustHTML($html,$this->usepre); //Try to make HTML look more like XHTML
if ($this->usecss) $html = $this->ReadCSS($html);
//Add new supported tags in the DisableTags function
$html=str_replace('<?','< ',$html); //Fix '<?XML' bug from HTML code generated by MS Word
$html=strip_tags($html,$this->enabledtags); //remove all unsupported tags, but the ones inside the 'enabledtags' string
//Explode the string in order to parse the HTML code
$a=preg_split('/<(.*?)>/ms',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
foreach($a as $i => $e)
{
if($i%2==0)
{
//TEXT
//Adjust lineheight
// $this->lineheight = (5*$this->FontSizePt)/11; //should be inside printbuffer?
//Adjust text, if needed
if (strpos($e,"&") !== false) //HTML-ENTITIES decoding
{
if (strpos($e,"#") !== false) $e = value_entity_decode($e); // Decode value entities
//Avoid crashing the script on PHP 4.0
$version = phpversion();
$version = str_replace('.','',$version);
if ($version >= 430) $e = html_entity_decode($e,ENT_QUOTES,'cp1252'); // changes and the like by their respective char
else $e = lesser_entity_decode($e);
}
$e = str_replace(chr(160),chr(32),$e); //unify ascii code of spaces (in order to recognize all of them correctly)
if (strlen($e) == 0) continue;
if ($this->divrevert) $e = strrev($e);
if ($this->toupper) $e = strtoupper($e);
if ($this->tolower) $e = strtolower($e);
//Start of 'if/elseif's
if($this->titulo) $this->SetTitle($e);
elseif($this->specialcontent)
{
if ($this->specialcontent == "type=select" and $this->selectoption['ACTIVE'] == true) //SELECT tag (form element)
{
$stringwidth = $this->GetStringWidth($e);
if (!isset($this->selectoption['MAXWIDTH']) or $stringwidth > $this->selectoption['MAXWIDTH']) $this->selectoption['MAXWIDTH'] = $stringwidth;
if (!isset($this->selectoption['SELECTED']) or $this->selectoption['SELECTED'] == '') $this->selectoption['SELECTED'] = $e;
}
else $this->textbuffer[] = array("»¤¬"/*identifier*/.$this->specialcontent."»¤¬".$e);
}
elseif($this->tablestart)
{
if($this->tdbegin)
{
$this->cell[$this->row][$this->col]['textbuffer'][] = array($e,$this->HREF,$this->currentstyle,$this->colorarray,$this->currentfont,$this->SUP,$this->SUB,''/*internal link*/,$this->strike,$this->outlineparam,$this->bgcolorarray);
$this->cell[$this->row][$this->col]['text'][] = $e;
$this->cell[$this->row][$this->col]['s'] += $this->GetStringWidth($e);
}
//Ignore content between <table>,<tr> and a <td> tag (this content is usually only a bunch of spaces)
}
elseif($this->pbegin or $this->HREF or $this->divbegin or $this->SUP or $this->SUB or $this->strike or $this->buffer_on) $this->textbuffer[] = array($e,$this->HREF,$this->currentstyle,$this->colorarray,$this->currentfont,$this->SUP,$this->SUB,''/*internal link*/,$this->strike,$this->outlineparam,$this->bgcolorarray); //Accumulate text on buffer
else
{
if ($this->blockjustfinished) $e = ltrim($e);
if ($e != '')
{
$this->Write($this->lineheight,$e); //Write text directly in the PDF
if ($this->pjustfinished) $this->pjustfinished = false;
}
}
}
else
{
//Tag
if($e{0}=='/') $this->CloseTag(strtoupper(substr($e,1)));
else
{
$regexp = '|=\'(.*?)\'|s'; // eliminate single quotes, if any
$e = preg_replace($regexp,"=\"\$1\"",$e);
$regexp = '| (\\w+?)=([^\\s>"]+)|si'; // changes anykey=anyvalue to anykey="anyvalue" (only do this when this happens inside tags)
$e = preg_replace($regexp," \$1=\"\$2\"",$e);
//Fix path values, if needed
if ((stristr($e,"href=") !== false) or (stristr($e,"src=") !== false) )
{
$regexp = '/ (href|src)="(.*?)"/i';
preg_match($regexp,$e,$auxiliararray);
$path = $auxiliararray[2];
$path = str_replace("\\","/",$path); //If on Windows
//Get link info and obtain its absolute path
$regexp = '|^./|';
$path = preg_replace($regexp,'',$path);
if($path{0} != '#') //It is not an Internal Link
{
if (strpos($path,"../") !== false ) //It is a Relative Link
{
$backtrackamount = substr_count($path,"../");
$maxbacktrack = substr_count($this->basepath,"/") - 1;
$filepath = str_replace("../",'',$path);
$path = $this->basepath;
//If it is an invalid relative link, then make it go to directory root
if ($backtrackamount > $maxbacktrack) $backtrackamount = $maxbacktrack;
//Backtrack some directories
for( $i = 0 ; $i < $backtrackamount + 1 ; $i++ ) $path = substr( $path, 0 , strrpos($path,"/") );
$path = $path . "/" . $filepath; //Make it an absolute path
}
elseif( strpos($path,":/") === false) //It is a Local Link
{
$path = $this->basepath . $path;
}
//Do nothing if it is an Absolute Link
}
$regexp = '/ (href|src)="(.*?)"/i';
$e = preg_replace($regexp,' \\1="'.$path.'"',$e);
}//END of Fix path values
//Extract attributes
$contents=array();
preg_match_all('/\\S*=["\'][^"\']*["\']/',$e,$contents);
preg_match('/\\S+/',$e,$a2);
$tag=strtoupper($a2[0]);
$attr=array();
if (!empty($contents))
{
foreach($contents[0] as $v)
{
if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
{
$attr[strtoupper($a3[1])]=$a3[2];
}
}
}
$this->OpenTag($tag,$attr);
}
}
}//end of foreach($a as $i=>$e)
//Create Internal Links, if needed
if (!empty($this->internallink) )
{
foreach($this->internallink as $k=>$v)
{
if (strpos($k,"#") !== false ) continue; //ignore
$ypos = $v['Y'];
$pagenum = $v['PAGE'];
$sharp = "#";
while (array_key_exists($sharp.$k,$this->internallink))
{
$internallink = $this->internallink[$sharp.$k];
$this->SetLink($internallink,$ypos,$pagenum);
$sharp .= "#";
}
}
}
}
function OpenTag($tag,$attr)
{
//! @return void
// What this gets: < $tag $attr['WIDTH']="90px" > does not get content here </closeTag here>
$align = array('left'=>'L','center'=>'C','right'=>'R','top'=>'T','middle'=>'M','bottom'=>'B','justify'=>'J');
$this->blockjustfinished=false;
//Opening tag
switch($tag){
case 'PAGE_BREAK': //custom-tag
case 'NEWPAGE': //custom-tag
$this->blockjustfinished = true;
$this->AddPage();
break;
case 'OUTLINE': //custom-tag (CSS2 property - browsers don't support it yet - Jan2005)
//Usage: (default: width=normal color=white)
//<outline width="(thin|medium|thick)" color="(usualcolorformat)" >Text</outline>
//Mix this tag with the <font color="(usualcolorformat)"> tag to get mixed colors on outlined text!
$this->buffer_on = true;
if (isset($attr['COLOR'])) $this->outlineparam['COLOR'] = ConvertColor($attr['COLOR']);
else $this->outlineparam['COLOR'] = array('R'=>255,'G'=>255,'B'=>255); //white
$this->outlineparam['OLDWIDTH'] = $this->LineWidth;
if (isset($attr['WIDTH']))
{
switch(strtoupper($attr['WIDTH']))
{
case 'THIN': $this->outlineparam['WIDTH'] = 0.75*$this->LineWidth; break;
case 'MEDIUM': $this->outlineparam['WIDTH'] = $this->LineWidth; break;
case 'THICK': $this->outlineparam['WIDTH'] = 1.75*$this->LineWidth; break;
}
}
else $this->outlineparam['WIDTH'] = $this->LineWidth; //width == oldwidth
break;
case 'BDO':
if (isset($attr['DIR']) and (strtoupper($attr['DIR']) == 'RTL' )) $this->divrevert = true;
break;
case 'S':
case 'STRIKE':
case 'DEL':
$this->strike=true;
break;
case 'SUB':
$this->SUB=true;
break;
case 'SUP':
$this->SUP=true;
break;
case 'CENTER':
$this->buffer_on = true;
if ($this->tdbegin) $this->cell[$this->row][$this->col]['a'] = $align['center'];
else
{
$this->divalign = $align['center'];
if ($this->x != $this->lMargin) $this->Ln($this->lineheight);
}
break;
case 'ADDRESS':
$this->buffer_on = true;
$this->SetStyle('I',true);
if (!$this->tdbegin and $this->x != $this->lMargin) $this->Ln($this->lineheight);
break;
case 'TABLE': // TABLE-BEGIN
if ($this->x != $this->lMargin) $this->Ln($this->lineheight);
$this->tablestart = true;
$this->table['nc'] = $this->table['nr'] = 0;
if (isset($attr['REPEAT_HEADER']) and $attr['REPEAT_HEADER'] == true) $this->UseTableHeader(true);
if (isset($attr['WIDTH'])) $this->table['w'] = ConvertSize($attr['WIDTH'],$this->pgwidth);
if (isset($attr['HEIGHT'])) $this->table['h'] = ConvertSize($attr['HEIGHT'],$this->pgwidth);
if (isset($attr['ALIGN'])) $this->table['a'] = $align[strtolower($attr['ALIGN'])];
if (isset($attr['BORDER'])) $this->table['border'] = $attr['BORDER'];
if (isset($attr['BGCOLOR'])) $this->table['bgcolor'][-1] = $attr['BGCOLOR'];
break;
case 'TR':
$this->row++;
$this->table['nr']++;
$this->col = -1;
if (isset($attr['BGCOLOR']))$this->table['bgcolor'][$this->row] = $attr['BGCOLOR'];
break;
case 'TH':
$this->SetStyle('B',true);
if (!isset($attr['ALIGN'])) $attr['ALIGN'] = "center";
case 'TD':
$this->tdbegin = true;
$this->col++;
while (isset($this->cell[$this->row][$this->col])) $this->col++;
//Update number column
if ($this->table['nc'] < $this->col+1) $this->table['nc'] = $this->col+1;
$this->cell[$this->row][$this->col] = array();
$this->cell[$this->row][$this->col]['text'] = array();
$this->cell[$this->row][$this->col]['s'] = 3;
if (isset($attr['WIDTH'])) $this->cell[$this->row][$this->col]['w'] = ConvertSize($attr['WIDTH'],$this->pgwidth);
if (isset($attr['HEIGHT'])) $this->cell[$this->row][$this->col]['h'] = ConvertSize($attr['HEIGHT'],$this->pgwidth);
if (isset($attr['ALIGN'])) $this->cell[$this->row][$this->col]['a'] = $align[strtolower($attr['ALIGN'])];
if (isset($attr['VALIGN'])) $this->cell[$this->row][$this->col]['va'] = $align[strtolower($attr['VALIGN'])];
if (isset($attr['BORDER'])) $this->cell[$this->row][$this->col]['border'] = $attr['BORDER'];
if (isset($attr['BGCOLOR'])) $this->cell[$this->row][$this->col]['bgcolor'] = $attr['BGCOLOR'];
$cs = $rs = 1;
if (isset($attr['COLSPAN']) && $attr['COLSPAN']>1) $cs = $this->cell[$this->row][$this->col]['colspan'] = $attr['COLSPAN'];
if (isset($attr['ROWSPAN']) && $attr['ROWSPAN']>1) $rs = $this->cell[$this->row][$this->col]['rowspan'] = $attr['ROWSPAN'];
//Chiem dung vi tri de danh cho cell span (¿mais hein?)
for ($k=$this->row ; $k < $this->row+$rs ;$k++)
for($l=$this->col; $l < $this->col+$cs ;$l++)
{
if ($k-$this->row || $l-$this->col) $this->cell[$k][$l] = 0;
}
if (isset($attr['NOWRAP'])) $this->cell[$this->row][$this->col]['nowrap']= 1;
break;
case 'OL':
if ( !isset($attr['TYPE']) or $attr['TYPE'] == '' ) $this->listtype = '1'; //OL default == '1'
else $this->listtype = $attr['TYPE']; // ol and ul types are mixed here
case 'UL':
if ( (!isset($attr['TYPE']) or $attr['TYPE'] == '') and $tag=='UL')
{
//Insert UL defaults
if ($this->listlvl == 0) $this->listtype = 'disc';
elseif ($this->listlvl == 1) $this->listtype = 'circle';
else $this->listtype = 'square';
}
elseif (isset($attr['TYPE']) and $tag=='UL') $this->listtype = $attr['TYPE'];
$this->buffer_on = false;
if ($this->listlvl == 0)
{
//First of all, skip a line
if (!$this->pjustfinished)
{
if ($this->x != $this->lMargin) $this->Ln($this->lineheight);
$this->Ln($this->lineheight);
}
$this->oldx = $this->x;
$this->listlvl++; // first depth level
$this->listnum = 0; // reset
$this->listoccur[$this->listlvl] = 1;
$this->listlist[$this->listlvl][1] = array('TYPE'=>$this->listtype,'MAXNUM'=>$this->listnum);
}
else
{
if (!empty($this->textbuffer))
{
$this->listitem[] = array($this->listlvl,$this->listnum,$this->textbuffer,$this->listoccur[$this->listlvl]);
$this->listnum++;
}
$this->textbuffer = array();
$occur = $this->listoccur[$this->listlvl];
$this->listlist[$this->listlvl][$occur]['MAXNUM'] = $this->listnum; //save previous lvl's maxnum
$this->listlvl++;
$this->listnum = 0; // reset
if ($this->listoccur[$this->listlvl] == 0) $this->listoccur[$this->listlvl] = 1;
else $this->listoccur[$this->listlvl]++;
$occur = $this->listoccur[$this->listlvl];
$this->listlist[$this->listlvl][$occur] = array('TYPE'=>$this->listtype,'MAXNUM'=>$this->listnum);
}
break;
case 'LI':
//Observation: </LI> is ignored
if ($this->listlvl == 0) //in case of malformed HTML code. Example:(...)</p><li>Content</li><p>Paragraph1</p>(...)
{
//First of all, skip a line
if (!$this->pjustfinished and $this->x != $this->lMargin) $this->Ln(2*$this->lineheight);
$this->oldx = $this->x;
$this->listlvl++; // first depth level
$this->listnum = 0; // reset
$this->listoccur[$this->listlvl] = 1;
$this->listlist[$this->listlvl][1] = array('TYPE'=>'disc','MAXNUM'=>$this->listnum);
}
if ($this->listnum == 0)
{
$this->buffer_on = true; //activate list 'bufferization'
$this->listnum++;
$this->textbuffer = array();
}
else
{
$this->buffer_on = true; //activate list 'bufferization'
if (!empty($this->textbuffer))
{
$this->listitem[] = array($this->listlvl,$this->listnum,$this->textbuffer,$this->listoccur[$this->listlvl]);
$this->listnum++;
}
$this->textbuffer = array();
}
break;
case 'H1': // 2 * fontsize
case 'H2': // 1.5 * fontsize
case 'H3': // 1.17 * fontsize
case 'H4': // 1 * fontsize
case 'H5': // 0.83 * fontsize
case 'H6': // 0.67 * fontsize
//Values obtained from: http://www.w3.org/TR/REC-CSS2/sample.html
if(isset($attr['ALIGN'])) $this->divalign = $align[strtolower($attr['ALIGN'])];
$this->buffer_on = true;
if ($this->x != $this->lMargin) $this->Ln(2*$this->lineheight);
elseif (!$this->pjustfinished) $this->Ln($this->lineheight);
$this->SetStyle('B',true);
switch($tag)
{
case 'H1':
$this->SetFontSize(2*$this->FontSizePt);
$this->lineheight *= 2;
break;
case 'H2':
$this->SetFontSize(1.5*$this->FontSizePt);
$this->lineheight *= 1.5;
break;
case 'H3':
$this->SetFontSize(1.17*$this->FontSizePt);
$this->lineheight *= 1.17;
break;
case 'H4':
$this->SetFontSize($this->FontSizePt);
break;
case 'H5':
$this->SetFontSize(0.83*$this->FontSizePt);
$this->lineheight *= 0.83;
break;
case 'H6':
$this->SetFontSize(0.67*$this->FontSizePt);
$this->lineheight *= 0.67;
break;
}
break;
case 'HR': //Default values: width=100% align=center color=gray
//Skip a line, if needed
if ($this->x != $this->lMargin) $this->Ln($this->lineheight);
$this->Ln(0.2*$this->lineheight);
$hrwidth = $this->pgwidth;
$hralign = 'C';
$hrcolor = array('R'=>200,'G'=>200,'B'=>200);
if($attr['WIDTH'] != '') $hrwidth = ConvertSize($attr['WIDTH'],$this->pgwidth);
if($attr['ALIGN'] != '') $hralign = $align[strtolower($attr['ALIGN'])];
if($attr['COLOR'] != '') $hrcolor = ConvertColor($attr['COLOR']);
$this->SetDrawColor($hrcolor['R'],$hrcolor['G'],$hrcolor['B']);
$x = $this->x;
$y = $this->y;
switch($hralign)
{
case 'L':
case 'J':
break;
case 'C':
$empty = $this->pgwidth - $hrwidth;
$empty /= 2;
$x += $empty;
break;
case 'R':
$empty = $this->pgwidth - $hrwidth;
$x += $empty;
break;
}
$oldlinewidth = $this->LineWidth;
$this->SetLineWidth(0.3);
$this->Line($x,$y,$x+$hrwidth,$y);
$this->SetLineWidth($oldlinewidth);
$this->Ln(0.2*$this->lineheight);
$this->SetDrawColor(0);
$this->blockjustfinished = true; //Eliminate exceeding left-side spaces
break;
case 'INS':
$this->SetStyle('U',true);
break;
case 'SMALL':
$newsize = $this->FontSizePt - 1;
$this->SetFontSize($newsize);
break;
case 'BIG':
$newsize = $this->FontSizePt + 1;
$this->SetFontSize($newsize);
case 'STRONG':
$this->SetStyle('B',true);
break;
case 'CITE':
case 'EM':
$this->SetStyle('I',true);
break;
case 'TITLE':
$this->titulo = true;
break;
case 'B':
case 'I':
case 'U':
if( isset($attr['CLASS']) or isset($attr['ID']) or isset($attr['STYLE']) )
{
$this->cssbegin=true;
if (isset($attr['CLASS'])) $properties = $this->CSS[$attr['CLASS']];
elseif (isset($attr['ID'])) $properties = $this->CSS[$attr['ID']];
//Read Inline CSS
if (isset($attr['STYLE'])) $properties = $this->readInlineCSS($attr['STYLE']);
//Look for name in the $this->CSS array
$this->backupcss = $properties;
if (!empty($properties)) $this->setCSS($properties); //name found in the CSS array!
}
$this->SetStyle($tag,true);
break;
case 'A':
if (isset($attr['NAME']) and $attr['NAME'] != '') $this->textbuffer[] = array('','','',array(),'',false,false,$attr['NAME']); //an internal link (adds a space for recognition)
if (isset($attr['HREF'])) $this->HREF=$attr['HREF'];
break;
case 'DIV':
//in case of malformed HTML code. Example:(...)</div><li>Content</li><div>DIV1</div>(...)
if ($this->listlvl > 0) // We are closing (omitted) OL/UL tag(s)
{
$this->buffer_on = false;
if (!empty($this->textbuffer)) $this->listitem[] = array($this->listlvl,$this->listnum,$this->textbuffer,$this->listoccur[$this->listlvl]);
$this->textbuffer = array();
$this->listlvl--;
$this->printlistbuffer();
$this->pjustfinished = true; //act as if a paragraph just ended
}
$this->divbegin=true;
if ($this->x != $this->lMargin) $this->Ln($this->lineheight);
if( isset($attr['ALIGN']) and $attr['ALIGN'] != '' ) $this->divalign = $align[strtolower($attr['ALIGN'])];
if( isset($attr['CLASS']) or isset($attr['ID']) or isset($attr['STYLE']) )
{
$this->cssbegin=true;
if (isset($attr['CLASS'])) $properties = $this->CSS[$attr['CLASS']];
elseif (isset($attr['ID'])) $properties = $this->CSS[$attr['ID']];
//Read Inline CSS
if (isset($attr['STYLE'])) $properties = $this->readInlineCSS($attr['STYLE']);
//Look for name in the $this->CSS array
if (!empty($properties)) $this->setCSS($properties); //name found in the CSS array!
}
break;
case 'IMG':
if(!empty($this->textbuffer) and !$this->tablestart)
{
//Output previously buffered content and output image below
//Set some default values
$olddivwidth = $this->divwidth;
$olddivheight = $this->divheight;
if ( $this->divwidth == 0) $this->divwidth = $this->pgwidth - $x + $this->lMargin;
if ( $this->divheight == 0) $this->divheight = $this->lineheight;
//Print content
$this->printbuffer($this->textbuffer,true/*is out of a block (e.g. DIV,P etc.)*/);
$this->textbuffer=array();
//Reset values
$this->divwidth = $olddivwidth;
$this->divheight = $olddivheight;
$this->textbuffer=array();
$this->Ln($this->lineheight);
}
if(isset($attr['SRC']))
{
$srcpath = $attr['SRC'];
if(!isset($attr['WIDTH'])) $attr['WIDTH'] = 0;
else $attr['WIDTH'] = ConvertSize($attr['WIDTH'],$this->pgwidth);//$attr['WIDTH'] /= 4;
if(!isset($attr['HEIGHT'])) $attr['HEIGHT'] = 0;
else $attr['HEIGHT'] = ConvertSize($attr['HEIGHT'],$this->pgwidth);//$attr['HEIGHT'] /= 4;
if ($this->tdbegin)
{
$bak_x = $this->x;
$bak_y = $this->y;
//Check whether image exists locally or on the URL
$f_exists = @fopen($srcpath,"rb");
if (!$f_exists) //Show 'image not found' icon instead
{
if(!$this->shownoimg) break;
$srcpath = str_replace("\\","/",dirname(__FILE__)) . "/";
$srcpath .= 'no_img.gif';
}
$sizesarray = $this->Image($srcpath, $this->GetX(), $this->GetY(), $attr['WIDTH'], $attr['HEIGHT'],'','',false);
$this->y = $bak_y;
$this->x = $bak_x;
}
elseif($this->pbegin or $this->divbegin)
{
//In order to support <div align='center'><img ...></div>
$ypos = 0;
$bak_x = $this->x;
$bak_y = $this->y;
//Check whether image exists locally or on the URL
$f_exists = @fopen($srcpath,"rb");
if (!$f_exists) //Show 'image not found' icon instead
{
if(!$this->shownoimg) break;
$srcpath = str_replace("\\","/",dirname(__FILE__)) . "/";
$srcpath .= 'no_img.gif';
}
$sizesarray = $this->Image($srcpath, $this->GetX(), $this->GetY(), $attr['WIDTH'], $attr['HEIGHT'],'','',false);
$this->y = $bak_y;
$this->x = $bak_x;
$xpos = '';
switch($this->divalign)
{
case "C":
$spacesize = $this->CurrentFont[ 'cw' ][ ' ' ] * ( $this->FontSizePt / 1000 );
$empty = ($this->pgwidth - $sizesarray['WIDTH'])/2;
$xpos = 'xpos='.$empty.',';
break;
case "R":
$spacesize = $this->CurrentFont[ 'cw' ][ ' ' ] * ( $this->FontSizePt / 1000 );
$empty = ($this->pgwidth - $sizesarray['WIDTH']);
$xpos = 'xpos='.$empty.',';
break;
default: break;
}
$numberoflines = (integer)ceil($sizesarray['HEIGHT']/$this->lineheight) ;
$ypos = $numberoflines * $this->lineheight;
$this->textbuffer[] = array("»¤¬"/*identifier*/."type=image,ypos=$ypos,{$xpos}width=".$sizesarray['WIDTH'].",height=".$sizesarray['HEIGHT']."»¤¬".$sizesarray['OUTPUT']);
while($numberoflines) {$this->textbuffer[] = array("\n",$this->HREF,$this->currentstyle,$this->colorarray,$this->currentfont,$this->SUP,$this->SUB,''/*internal link*/,$this->strike,$this->outlineparam,$this->bgcolorarray);$numberoflines--;}
}
else
{
$imgborder = 0;
if (isset($attr['BORDER'])) $imgborder = ConvertSize($attr['BORDER'],$this->pgwidth);
//Check whether image exists locally or on the URL
$f_exists = @fopen($srcpath,"rb");
if (!$f_exists) //Show 'image not found' icon instead
{
$srcpath = str_replace("\\","/",dirname(__FILE__)) . "/";
$srcpath .= 'no_img.gif';
}
$sizesarray = $this->Image($srcpath, $this->GetX(), $this->GetY(), $attr['WIDTH'], $attr['HEIGHT'],'',$this->HREF); //Output Image
$ini_x = $sizesarray['X'];
$ini_y = $sizesarray['Y'];
if ($imgborder)
{
$oldlinewidth = $this->LineWidth;
$this->SetLineWidth($imgborder);
$this->Rect($ini_x,$ini_y,$sizesarray['WIDTH'],$sizesarray['HEIGHT']);
$this->SetLineWidth($oldlinewidth);
}
}
if ($sizesarray['X'] < $this->x) $this->x = $this->lMargin;
if ($this->tablestart)
{
$this->cell[$this->row][$this->col]['textbuffer'][] = array("»¤¬"/*identifier*/."type=image,width=".$sizesarray['WIDTH'].",height=".$sizesarray['HEIGHT']."»¤¬".$sizesarray['OUTPUT']);
$this->cell[$this->row][$this->col]['s'] += $sizesarray['WIDTH'] + 1;// +1 == margin
$this->cell[$this->row][$this->col]['form'] = true; // in order to make some width adjustments later
if (!isset($this->cell[$this->row][$this->col]['w'])) $this->cell[$this->row][$this->col]['w'] = $sizesarray['WIDTH'] + 3;
if (!isset($this->cell[$this->row][$this->col]['h'])) $this->cell[$this->row][$this->col]['h'] = $sizesarray['HEIGHT'] + 3;
}
}
break;
case 'BLOCKQUOTE':
case 'BR':
if($this->tablestart)
{
$this->cell[$this->row][$this->col]['textbuffer'][] = array("\n",$this->HREF,$this->currentstyle,$this->colorarray,$this->currentfont,$this->SUP,$this->SUB,''/*internal link*/,$this->strike,$this->outlineparam,$this->bgcolorarray);
$this->cell[$this->row][$this->col]['text'][] = "\n";
if (!isset($this->cell[$this->row][$this->col]['maxs'])) $this->cell[$this->row][$this->col]['maxs'] = $this->cell[$this->row][$this->col]['s'] +2; //+2 == margin
elseif($this->cell[$this->row][$this->col]['maxs'] < $this->cell[$this->row][$this->col]['s']) $this->cell[$this->row][$this->col]['maxs'] = $this->cell[$this->row][$this->col]['s']+2;//+2 == margin
$this->cell[$this->row][$this->col]['s'] = 0;// reset
}
elseif($this->divbegin or $this->pbegin or $this->buffer_on) $this->textbuffer[] = array("\n",$this->HREF,$this->currentstyle,$this->colorarray,$this->currentfont,$this->SUP,$this->SUB,''/*internal link*/,$this->strike,$this->outlineparam,$this->bgcolorarray);
else {$this->Ln($this->lineheight);$this->blockjustfinished = true;}
break;
case 'P':
//in case of malformed HTML code. Example:(...)</p><li>Content</li><p>Paragraph1</p>(...)
if ($this->listlvl > 0) // We are closing (omitted) OL/UL tag(s)
{
$this->buffer_on = false;
if (!empty($this->textbuffer)) $this->listitem[] = array($this->listlvl,$this->listnum,$this->textbuffer,$this->listoccur[$this->listlvl]);
$this->textbuffer = array();
$this->listlvl--;
$this->printlistbuffer();
$this->pjustfinished = true; //act as if a paragraph just ended
}
if ($this->tablestart)
{
$this->cell[$this->row][$this->col]['textbuffer'][] = array($e,$this->HREF,$this->currentstyle,$this->colorarray,$this->currentfont,$this->SUP,$this->SUB,''/*internal link*/,$this->strike,$this->outlineparam,$this->bgcolorarray);
$this->cell[$this->row][$this->col]['text'][] = "\n";
break;
}
$this->pbegin=true;
if ($this->x != $this->lMargin) $this->Ln(2*$this->lineheight);
elseif (!$this->pjustfinished) $this->Ln($this->lineheight);
//Save x,y coords in case we need to print borders...
$this->oldx = $this->x;
$this->oldy = $this->y;
if(isset($attr['ALIGN'])) $this->divalign = $align[strtolower($attr['ALIGN'])];
if(isset($attr['CLASS']) or isset($attr['ID']) or isset($attr['STYLE']) )
{
$this->cssbegin=true;
if (isset($attr['CLASS'])) $properties = $this->CSS[$attr['CLASS']];
elseif (isset($attr['ID'])) $properties = $this->CSS[$attr['ID']];
//Read Inline CSS
if (isset($attr['STYLE'])) $properties = $this->readInlineCSS($attr['STYLE']);
//Look for name in the $this->CSS array
$this->backupcss = $properties;
if (!empty($properties)) $this->setCSS($properties); //name(id/class/style) found in the CSS array!
}
break;
case 'SPAN':
$this->buffer_on = true;
//Save x,y coords in case we need to print borders...
$this->oldx = $this->x;
$this->oldy = $this->y;
if( isset($attr['CLASS']) or isset($attr['ID']) or isset($attr['STYLE']) )
{
$this->cssbegin=true;
if (isset($attr['CLASS'])) $properties = $this->CSS[$attr['CLASS']];
elseif (isset($attr['ID'])) $properties = $this->CSS[$attr['ID']];
//Read Inline CSS
if (isset($attr['STYLE'])) $properties = $this->readInlineCSS($attr['STYLE']);
//Look for name in the $this->CSS array
$this->backupcss = $properties;
if (!empty($properties)) $this->setCSS($properties); //name found in the CSS array!
}
break;
case 'PRE':
if($this->tablestart)
{
$this->cell[$this->row][$this->col]['textbuffer'][] = array("\n",$this->HREF,$this->currentstyle,$this->colorarray,$this->currentfont,$this->SUP,$this->SUB,''/*internal link*/,$this->strike,$this->outlineparam,$this->bgcolorarray);
$this->cell[$this->row][$this->col]['text'][] = "\n";
}
elseif($this->divbegin or $this->pbegin or $this->buffer_on) $this->textbuffer[] = array("\n",$this->HREF,$this->currentstyle,$this->colorarray,$this->currentfont,$this->SUP,$this->SUB,''/*internal link*/,$this->strike,$this->outlineparam,$this->bgcolorarray);
else
{
if ($this->x != $this->lMargin) $this->Ln(2*$this->lineheight);
elseif (!$this->pjustfinished) $this->Ln($this->lineheight);
$this->buffer_on = true;