forked from iafan/html2pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html2pdf.class.php
6610 lines (5625 loc) · 250 KB
/
html2pdf.class.php
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
/**
* HTML2PDF Librairy - main class
*
* HTML => PDF convertor
* distributed under the LGPL License
*
* @author Laurent MINGUET <[email protected]>
* @version 4.03
*/
if (!defined('__CLASS_HTML2PDF__')) {
define('__CLASS_HTML2PDF__', '4.03');
define('HTML2PDF_USED_TCPDF_VERSION', '5.9.206');
require_once(dirname(__FILE__).'/_class/exception.class.php');
require_once(dirname(__FILE__).'/_class/locale.class.php');
require_once(dirname(__FILE__).'/_class/myPdf.class.php');
require_once(dirname(__FILE__).'/_class/parsingHtml.class.php');
require_once(dirname(__FILE__).'/_class/parsingCss.class.php');
class HTML2PDF
{
/**
* HTML2PDF_myPdf object, extends from TCPDF
* @var HTML2PDF_myPdf
*/
public $pdf = null;
/**
* CSS parsing
* @var HTML2PDF_parsingCss
*/
public $parsingCss = null;
/**
* HTML parsing
* @var HTML2PDF_parsingHtml
*/
public $parsingHtml = null;
protected $_langue = 'fr'; // locale of the messages
protected $_orientation = 'P'; // page orientation : Portrait ou Landscape
protected $_format = 'A4'; // page format : A4, A3, ...
protected $_encoding = ''; // charset encoding
protected $_unicode = true; // means that the input text is unicode (default = true)
protected $_testTdInOnepage = true; // test of TD that can not take more than one page
protected $_testIsImage = true; // test if the images exist or not
protected $_testIsDeprecated = false; // test the deprecated functions
protected $_parsePos = 0; // position in the parsing
protected $_tempPos = 0; // temporary position for complex table
protected $_page = 0; // current page number
protected $_subHtml = null; // sub html
protected $_subPart = false; // sub HTML2PDF
protected $_subHEADER = array(); // sub action to make the header
protected $_subFOOTER = array(); // sub action to make the footer
protected $_subSTATES = array(); // array to save some parameters
protected $_isSubPart = false; // flag : in a sub html2pdf
protected $_isInThead = false; // flag : in a thead
protected $_isInTfoot = false; // flag : in a tfoot
protected $_isInOverflow = false; // flag : in a overflow
protected $_isInFooter = false; // flag : in a footer
protected $_isInDraw = null; // flag : in a draw (svg)
protected $_isAfterFloat = false; // flag : is just after a float
protected $_isInForm = false; // flag : is in a float. false / action of the form
protected $_isInLink = ''; // flag : is in a link. empty / href of the link
protected $_isInParagraph = false; // flag : is in a paragraph
protected $_isForOneLine = false; // flag : in a specific sub html2pdf to have the height of the next line
protected $_maxX = 0; // maximum X of the current zone
protected $_maxY = 0; // maximum Y of the current zone
protected $_maxE = 0; // number of elements in the current zone
protected $_maxH = 0; // maximum height of the line in the current zone
protected $_maxSave = array(); // save the maximums of the current zone
protected $_currentH = 0; // height of the current line
protected $_defaultLeft = 0; // default marges of the page
protected $_defaultTop = 0;
protected $_defaultRight = 0;
protected $_defaultBottom = 0;
protected $_defaultFont = null; // default font to use, is the asked font does not exist
protected $_margeLeft = 0; // current marges of the page
protected $_margeTop = 0;
protected $_margeRight = 0;
protected $_margeBottom = 0;
protected $_marges = array(); // save the different marges of the current page
protected $_pageMarges = array(); // float marges of the current page
protected $_background = array(); // background informations
protected $_firstPage = true; // flag : first page
protected $_defList = array(); // table to save the stats of the tags UL and OL
protected $_lstAnchor = array(); // list of the anchors
protected $_lstField = array(); // list of the fields
protected $_lstSelect = array(); // list of the options of the current select
protected $_previousCall = null; // last action called
protected $_debugActif = false; // flag : mode debug is active
protected $_debugOkUsage = false; // flag : the function memory_get_usage exist
protected $_debugOkPeak = false; // flag : the function memory_get_peak_usage exist
protected $_debugLevel = 0; // level in the debug
protected $_debugStartTime = 0; // debug start time
protected $_debugLastTime = 0; // debug stop time
static protected $_subobj = null; // object html2pdf prepared in order to accelerate the creation of sub html2pdf
static protected $_tables = array(); // static table to prepare the nested html tables
/**
* class constructor
*
* @access public
* @param string $orientation page orientation, same as TCPDF
* @param mixed $format The format used for pages, same as TCPDF
* @param $tring $langue Langue : fr, en, it...
* @param boolean $unicode TRUE means that the input text is unicode (default = true)
* @param String $encoding charset encoding; default is UTF-8
* @param array $marges Default marges (left, top, right, bottom)
* @return HTML2PDF $this
*/
public function __construct($orientation = 'P', $format = 'A4', $langue='fr', $unicode=true, $encoding='UTF-8', $marges = array(5, 5, 5, 8))
{
// init the page number
$this->_page = 0;
$this->_firstPage = true;
$this->_firstPageInSet = array();
$this->_isCalculationPass = true;
$this->_rootPathForURLs = '';
// save the parameters
$this->_orientation = $orientation;
$this->_format = $format;
$this->_langue = strtolower($langue);
$this->_unicode = $unicode;
$this->_encoding = $encoding;
// load the Local
HTML2PDF_locale::load($this->_langue);
// create the HTML2PDF_myPdf object
$this->pdf = new HTML2PDF_myPdf($orientation, 'mm', $format, $unicode, $encoding);
// init the CSS parsing object
$this->parsingCss = new HTML2PDF_parsingCss($this->pdf);
$this->parsingCss->fontSet();
$this->_defList = array();
// init some tests
$this->setTestTdInOnePage(true);
$this->setTestIsImage(true);
$this->setTestIsDeprecated(true);
// init the default font
$this->setDefaultFont(null);
// init the HTML parsing object
$this->parsingHtml = new HTML2PDF_parsingHtml($this->_encoding);
$this->_subHtml = null;
$this->_subPart = false;
// init the marges of the page
if (!is_array($marges)) $marges = array($marges, $marges, $marges, $marges);
$this->_setDefaultMargins($marges[0], $marges[1], $marges[2], $marges[3]);
$this->_setMargins();
$this->_marges = array();
// init the form's fields
$this->_lstField = array();
return $this;
}
/**
* Destructor
*
* @access public
* @return null
*/
public function __destruct()
{
}
/**
* Clone to create a sub HTML2PDF from HTML2PDF::$_subobj
*
* @access public
*/
public function __clone()
{
$this->pdf = clone $this->pdf;
$this->parsingHtml = clone $this->parsingHtml;
$this->parsingCss = clone $this->parsingCss;
$this->parsingCss->setPdfParent($this->pdf);
}
/**
* set the debug mode to On
*
* @access public
* @return HTML2PDF $this
*/
public function setModeDebug()
{
$time = microtime(true);
$this->_debugActif = true;
$this->_debugOkUsage = function_exists('memory_get_usage');
$this->_debugOkPeak = function_exists('memory_get_peak_usage');
$this->_debugStartTime = $time;
$this->_debugLastTime = $time;
$this->_DEBUG_stepline('step', 'time', 'delta', 'memory', 'peak');
$this->_DEBUG_add('Init debug');
return $this;
}
/**
* Set the test of TD thdat can not take more than one page
*
* @access public
* @param boolean $mode
* @return HTML2PDF $this
*/
public function setTestTdInOnePage($mode = true)
{
$this->_testTdInOnepage = $mode ? true : false;
return $this;
}
/**
* Set the test if the images exist or not
*
* @access public
* @param boolean $mode
* @return HTML2PDF $this
*/
public function setTestIsImage($mode = true)
{
$this->_testIsImage = $mode ? true : false;
return $this;
}
/**
* Set the test on deprecated functions
*
* @access public
* @param boolean $mode
* @return HTML2PDF $this
*/
public function setTestIsDeprecated($mode = true)
{
$this->_testIsDeprecated = $mode ? true : false;
return $this;
}
/**
* Set the default font to use, if no font is specify, or if the asked font does not exist
*
* @access public
* @param string $default name of the default font to use. If null : Arial is no font is specify, and error if the asked font does not exist
* @return HTML2PDF $this
*/
public function setDefaultFont($default = null)
{
$this->_defaultFont = $default;
$this->parsingCss->setDefaultFont($default);
return $this;
}
/**
* add a font, see TCPDF function addFont
*
* @access public
* @param string $family Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
* @param string $style Font style. Possible values are (case insensitive):<ul><li>empty string: regular (default)</li><li>B: bold</li><li>I: italic</li><li>BI or IB: bold italic</li></ul>
* @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
* @return HTML2PDF $this
* @see TCPDF::addFont
*/
public function addFont($family, $style='', $file='')
{
$this->pdf->AddFont($family, $style, $file);
return $this;
}
/**
* display a automatic index, from the bookmarks
*
* @access public
* @param string $titre index title
* @param int $sizeTitle font size of the index title, in mm
* @param int $sizeBookmark font size of the index, in mm
* @param boolean $bookmarkTitle add a bookmark for the index, at his beginning
* @param boolean $displayPage display the page numbers
* @param int $onPage if null : at the end of the document on a new page, else on the $onPage page
* @param string $fontName font name to use
* @return null
*/
public function createIndex($titre = 'Index', $sizeTitle = 20, $sizeBookmark = 15, $bookmarkTitle = true, $displayPage = true, $onPage = null, $fontName = 'helvetica')
{
$oldPage = $this->_INDEX_NewPage($onPage);
$this->pdf->createIndex($this, $titre, $sizeTitle, $sizeBookmark, $bookmarkTitle, $displayPage, $onPage, $fontName);
if ($oldPage) $this->pdf->setPage($oldPage);
}
/**
* clean up the objects
*
* @access protected
*/
protected function _cleanUp()
{
HTML2PDF::$_subobj = null;
HTML2PDF::$_tables = array();
}
/**
* Send the document to a given destination: string, local file or browser.
* Dest can be :
* I : send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
* D : send to the browser and force a file download with the name given by name.
* F : save to a local server file with the name given by name.
* S : return the document as a string. name is ignored.
* FI: equivalent to F + I option
* FD: equivalent to F + D option
* true => I
* false => S
*
* @param string $name The name of the file when saved.
* @param string $dest Destination where to send the document.
* @return string content of the PDF, if $dest=S
* @see TCPDF::close
* @access public
*/
public function Output($name = '', $dest = false)
{
// close the pdf and clean up
$this->_cleanUp();
// if on debug mode
if ($this->_debugActif) {
$this->_DEBUG_add('Before output');
$this->pdf->Close();
exit;
}
// complete parameters
if ($dest===false) $dest = 'I';
if ($dest===true) $dest = 'S';
if ($dest==='') $dest = 'I';
if ($name=='') $name='document.pdf';
// clean up the destination
$dest = strtoupper($dest);
if (!in_array($dest, array('I', 'D', 'F', 'S', 'FI','FD'))) $dest = 'I';
// the name must be a PDF name
if (strtolower(substr($name, -4))!='.pdf') {
throw new HTML2PDF_exception(0, 'The output document name "'.$name.'" is not a PDF name');
}
// call the output of TCPDF
return $this->pdf->Output($name, $dest);
}
/**
* convert HTML to PDF
*
* @access public
* @param string $html
* @param boolean $debugVue enable the HTML debug vue
* @return null
*/
public function writeHTML($html, $debugVue = false)
{
// if it is a real html page, we have to convert it
if (preg_match('/<body/isU', $html))
$html = $this->getHtmlFromPage($html);
$html = str_replace('[[date_y]]', date('Y'), $html);
$html = str_replace('[[date_m]]', date('m'), $html);
$html = str_replace('[[date_d]]', date('d'), $html);
$html = str_replace('[[date_h]]', date('H'), $html);
$html = str_replace('[[date_i]]', date('i'), $html);
$html = str_replace('[[date_s]]', date('s'), $html);
// If we are in HTML debug vue : display the HTML
if ($debugVue) {
return $this->_vueHTML($html);
}
// convert HTMl to PDF
$this->parsingCss->readStyle($html);
$this->parsingHtml->setHTML($html);
$this->parsingHtml->parse();
$this->_makeHTMLcode();
}
/**
* convert the HTML of a real page, to a code adapted to HTML2PDF
*
* @access public
* @param string HTML of a real page
* @return string HTML adapted to HTML2PDF
*/
public function getHtmlFromPage($html)
{
$html = str_replace('<BODY', '<body', $html);
$html = str_replace('</BODY', '</body', $html);
// extract the content
$res = explode('<body', $html);
if (count($res)<2) return $html;
$content = '<page'.$res[1];
$content = explode('</body', $content);
$content = $content[0].'</page>';
// extract the link tags
preg_match_all('/<link([^>]*)>/isU', $html, $match);
foreach ($match[0] as $src)
$content = $src.'</link>'.$content;
// extract the css style tags
preg_match_all('/<style[^>]*>(.*)<\/style[^>]*>/isU', $html, $match);
foreach ($match[0] as $src)
$content = $src.$content;
return $content;
}
/**
* init a sub HTML2PDF. does not use it directly. Only the method createSubHTML must use it
*
* @access public
* @param string $format
* @param string $orientation
* @param array $marge
* @param integer $page
* @param array $defLIST
* @param integer $myLastPageGroup
* @param integer $myLastPageGroupNb
*/
public function initSubHtml($format, $orientation, $marge, $page, $defLIST, $myLastPageGroup, $myLastPageGroupNb)
{
$this->_isSubPart = true;
$this->parsingCss->setOnlyLeft();
$this->_setNewPage($format, $orientation, null, null, ($myLastPageGroup!==null));
$this->_saveMargin(0, 0, $marge);
$this->_defList = $defLIST;
$this->_page = $page;
$this->pdf->setMyLastPageGroup($myLastPageGroup);
$this->pdf->setMyLastPageGroupNb($myLastPageGroupNb);
$this->pdf->setXY(0, 0);
$this->parsingCss->fontSet();
}
/**
* display the content in HTML moden for debug
*
* @access protected
* @param string $contenu
*/
protected function _vueHTML($content)
{
$content = preg_replace('/<page_header([^>]*)>/isU', '<hr>'.HTML2PDF_locale::get('vue01').' : $1<hr><div$1>', $content);
$content = preg_replace('/<page_footer([^>]*)>/isU', '<hr>'.HTML2PDF_locale::get('vue02').' : $1<hr><div$1>', $content);
$content = preg_replace('/<page([^>]*)>/isU', '<hr>'.HTML2PDF_locale::get('vue03').' : $1<hr><div$1>', $content);
$content = preg_replace('/<\/page([^>]*)>/isU', '</div><hr>', $content);
$content = preg_replace('/<bookmark([^>]*)>/isU', '<hr>bookmark : $1<hr>', $content);
$content = preg_replace('/<\/bookmark([^>]*)>/isU', '', $content);
$content = preg_replace('/<barcode([^>]*)>/isU', '<hr>barcode : $1<hr>', $content);
$content = preg_replace('/<\/barcode([^>]*)>/isU', '', $content);
$content = preg_replace('/<qrcode([^>]*)>/isU', '<hr>qrcode : $1<hr>', $content);
$content = preg_replace('/<\/qrcode([^>]*)>/isU', '', $content);
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>'.HTML2PDF_locale::get('vue04').' HTML</title>
<meta http-equiv="Content-Type" content="text/html; charset='.$this->_encoding.'" >
</head>
<body style="padding: 10px; font-size: 10pt;font-family: Verdana;">
'.$content.'
</body>
</html>';
exit;
}
/**
* set the default margins of the page
*
* @access protected
* @param int $left (mm, left margin)
* @param int $top (mm, top margin)
* @param int $right (mm, right margin, if null => left=right)
* @param int $bottom (mm, bottom margin, if null => bottom=8mm)
*/
protected function _setDefaultMargins($left, $top, $right = null, $bottom = null)
{
if ($right===null) $right = $left;
if ($bottom===null) $bottom = 8;
$this->_defaultLeft = $this->parsingCss->ConvertToMM($left.'mm');
$this->_defaultTop = $this->parsingCss->ConvertToMM($top.'mm');
$this->_defaultRight = $this->parsingCss->ConvertToMM($right.'mm');
$this->_defaultBottom = $this->parsingCss->ConvertToMM($bottom.'mm');
}
/**
* create a new page
*
* @access protected
* @param mixed $format
* @param string $orientation
* @param array $background background information
* @param integer $curr real position in the html parseur (if break line in the write of a text)
* @param boolean $resetPageNumber
*/
protected function _setNewPage($format = null, $orientation = '', $background = null, $curr = null, $resetPageNumber=false)
{
$this->_firstPage = false;
$this->_format = $format ? $format : $this->_format;
$this->_orientation = $orientation ? $orientation : $this->_orientation;
$this->_background = $background!==null ? $background : $this->_background;
$this->_maxY = 0;
$this->_maxX = 0;
$this->_maxH = 0;
$this->_maxE = 0;
$this->pdf->SetMargins($this->_defaultLeft, $this->_defaultTop, $this->_defaultRight);
if ($resetPageNumber) {
$this->pdf->startPageGroup();
}
$this->pdf->AddPage($this->_orientation, $this->_format);
if ($resetPageNumber) {
$this->pdf->myStartPageGroup();
}
$this->_page++;
if (!$this->_isSubPart) {
if ($this->_isCalculationPass) {
if ($resetPageNumber || $this->_page == 1) {
$this->_firstPageInSet []= $this->_page;
}
$this->_maxPage = $this->_page;
}
}
if (!$this->_subPart && !$this->_isSubPart) {
if (is_array($this->_background)) {
if (isset($this->_background['color']) && $this->_background['color']) {
$this->pdf->setFillColorArray($this->_background['color']);
$this->pdf->Rect(0, 0, $this->pdf->getW(), $this->pdf->getH(), 'F');
}
if (isset($this->_background['img']) && $this->_background['img'])
$this->pdf->Image($this->_background['img'], $this->_background['posX'], $this->_background['posY'], $this->_background['width']);
}
$this->_setPageHeader();
$this->_setPageFooter();
}
$this->_setMargins();
$this->pdf->setY($this->_margeTop);
$this->_setNewPositionForNewLine($curr);
$this->_maxH = 0;
}
/**
* set the real margin, using the default margins and the page margins
*
* @access protected
*/
protected function _setMargins()
{
if (!$this->_isSubPart) {
$first = in_array($this->_page, $this->_firstPageInSet);
// optimized to calculate/set page margins only once per page
$suffix = $first ? '-first' : '';
// prepare the margins
$this->_margeLeft = $this->_defaultLeft + (isset($this->_background['left'.$suffix]) ? $this->_background['left'.$suffix] : 0);
$this->_margeRight = $this->_defaultRight + (isset($this->_background['right'.$suffix]) ? $this->_background['right'.$suffix] : 0);
$this->_margeTop = $this->_defaultTop + (isset($this->_background['top'.$suffix]) ? $this->_background['top'.$suffix] : 0);
$this->_margeBottom = $this->_defaultBottom + (isset($this->_background['bottom'.$suffix]) ? $this->_background['bottom'.$suffix] : 0);
// set the PDF margins
$this->pdf->SetMargins($this->_margeLeft, $this->_margeTop, $this->_margeRight);
$this->pdf->SetAutoPageBreak(false, $this->_margeBottom);
}
// set the float Margins
$this->_pageMarges = array();
if ($this->_isInParagraph!==false) {
$this->_pageMarges[floor($this->_margeTop*100)] = array($this->_isInParagraph[0], $this->pdf->getW()-$this->_isInParagraph[1]);
} else {
$this->_pageMarges[floor($this->_margeTop*100)] = array($this->_margeLeft, $this->pdf->getW()-$this->_margeRight);
}
}
/**
* add a debug step
*
* @access protected
* @param string $name step name
* @param boolean $level (true=up, false=down, null=nothing to do)
* @return $this
*/
protected function _DEBUG_add($name, $level=null)
{
// if true : UP
if ($level===true) $this->_debugLevel++;
$name = str_repeat(' ', $this->_debugLevel). $name.($level===true ? ' Begin' : ($level===false ? ' End' : ''));
$time = microtime(true);
$usage = ($this->_debugOkUsage ? memory_get_usage() : 0);
$peak = ($this->_debugOkPeak ? memory_get_peak_usage() : 0);
$this->_DEBUG_stepline(
$name,
number_format(($time - $this->_debugStartTime)*1000, 1, '.', ' ').' ms',
number_format(($time - $this->_debugLastTime)*1000, 1, '.', ' ').' ms',
number_format($usage/1024, 1, '.', ' ').' Ko',
number_format($peak/1024, 1, '.', ' ').' Ko'
);
$this->_debugLastTime = $time;
// it false : DOWN
if ($level===false) $this->_debugLevel--;
return $this;
}
/**
* display a debug line
*
*
* @access protected
* @param string $name
* @param string $timeTotal
* @param string $timeStep
* @param string $memoryUsage
* @param string $memoryPeak
*/
protected function _DEBUG_stepline($name, $timeTotal, $timeStep, $memoryUsage, $memoryPeak)
{
$txt = str_pad($name, 30, ' ', STR_PAD_RIGHT).
str_pad($timeTotal, 12, ' ', STR_PAD_LEFT).
str_pad($timeStep, 12, ' ', STR_PAD_LEFT).
str_pad($memoryUsage, 15, ' ', STR_PAD_LEFT).
str_pad($memoryPeak, 15, ' ', STR_PAD_LEFT);
echo '<pre style="padding:0; margin:0">'.$txt.'</pre>';
}
/**
* get the Min and Max X, for Y (use the float margins)
*
* @access protected
* @param float $y
* @return array(float, float)
*/
protected function _getMargins($y)
{
$y = floor($y*100);
$x = array($this->pdf->getlMargin(), $this->pdf->getW()-$this->pdf->getrMargin());
foreach ($this->_pageMarges as $mY => $mX)
if ($mY<=$y) $x = $mX;
return $x;
}
/**
* Add margins, for a float
*
* @access protected
* @param string $float (left / right)
* @param float $xLeft
* @param float $yTop
* @param float $xRight
* @param float $yBottom
*/
protected function _addMargins($float, $xLeft, $yTop, $xRight, $yBottom)
{
// get the current float margins, for top and bottom
$oldTop = $this->_getMargins($yTop);
$oldBottom = $this->_getMargins($yBottom);
// update the top float margin
if ($float=='left' && $oldTop[0]<$xRight) $oldTop[0] = $xRight;
if ($float=='right' && $oldTop[1]>$xLeft) $oldTop[1] = $xLeft;
$yTop = floor($yTop*100);
$yBottom = floor($yBottom*100);
// erase all the float margins that are smaller than the new one
foreach ($this->_pageMarges as $mY => $mX) {
if ($mY<$yTop) continue;
if ($mY>$yBottom) break;
if ($float=='left' && $this->_pageMarges[$mY][0]<$xRight) unset($this->_pageMarges[$mY]);
if ($float=='right' && $this->_pageMarges[$mY][1]>$xLeft) unset($this->_pageMarges[$mY]);
}
// save the new Top and Bottom margins
$this->_pageMarges[$yTop] = $oldTop;
$this->_pageMarges[$yBottom] = $oldBottom;
// sort the margins
ksort($this->_pageMarges);
// we are just after float
$this->_isAfterFloat = true;
}
/**
* Save old margins (push), and set new ones
*
* @access protected
* @param float $ml left margin
* @param float $mt top margin
* @param float $mr right margin
*/
protected function _saveMargin($ml, $mt, $mr)
{
// save old margins
$this->_marges[] = array('l' => $this->pdf->getlMargin(), 't' => $this->pdf->gettMargin(), 'r' => $this->pdf->getrMargin(), 'page' => $this->_pageMarges);
// set new ones
$this->pdf->SetMargins($ml, $mt, $mr);
// prepare for float margins
$this->_pageMarges = array();
$this->_pageMarges[floor($mt*100)] = array($ml, $this->pdf->getW()-$mr);
}
/**
* load the last saved margins (pop)
*
* @access protected
*/
protected function _loadMargin()
{
$old = array_pop($this->_marges);
if ($old) {
$ml = $old['l'];
$mt = $old['t'];
$mr = $old['r'];
$mP = $old['page'];
} else {
$ml = $this->_margeLeft;
$mt = 0;
$mr = $this->_margeRight;
$mP = array($mt => array($ml, $this->pdf->getW()-$mr));
}
$this->pdf->SetMargins($ml, $mt, $mr);
$this->_pageMarges = $mP;
}
/**
* save the current maxs (push)
*
* @access protected
*/
protected function _saveMax()
{
$this->_maxSave[] = array($this->_maxX, $this->_maxY, $this->_maxH, $this->_maxE);
}
/**
* load the last saved current maxs (pop)
*
* @access protected
*/
protected function _loadMax()
{
$old = array_pop($this->_maxSave);
if ($old) {
$this->_maxX = $old[0];
$this->_maxY = $old[1];
$this->_maxH = $old[2];
$this->_maxE = $old[3];
} else {
$this->_maxX = 0;
$this->_maxY = 0;
$this->_maxH = 0;
$this->_maxE = 0;
}
}
/**
* draw the PDF header with the HTML in page_header
*
* @access protected
*/
protected function _setPageHeader()
{
$first = in_array($this->_page, $this->_firstPageInSet);
// if there's no 'first' header defined, fallback to normal header
if ($first && !count($this->{'_subFIRST_HEADER'})) {
$first = false;
}
$prop = $first ? '_subFIRST_HEADER' : '_subHEADER';
if (!count($this->{$prop})) return false;
$oldParsePos = $this->_parsePos;
$oldParseCode = $this->parsingHtml->code;
$this->_parsePos = 0;
$this->parsingHtml->code = $this->{$prop};
$this->_makeHTMLcode();
$this->_parsePos = $oldParsePos;
$this->parsingHtml->code = $oldParseCode;
}
/**
* draw the PDF footer with the HTML in page_footer
*
* @access protected
*/
protected function _setPageFooter()
{
$last = (!$this->_isCalculationPass && $this->_page == $this->_maxPage);
// if there's no 'last' footer defined, fallback to normal footer
if ($last && !count($this->{'_subLAST_FOOTER'})) {
$last = false;
}
$prop = $last ? '_subLAST_FOOTER' : '_subFOOTER';
if (!count($this->{$prop})) return false;
$oldParsePos = $this->_parsePos;
$oldParseCode = $this->parsingHtml->code;
$this->_parsePos = 0;
$this->parsingHtml->code = $this->{$prop};
$this->_isInFooter = true;
$this->_makeHTMLcode();
$this->_isInFooter = false;
$this->_parsePos = $oldParsePos;
$this->parsingHtml->code = $oldParseCode;
}
/**
* new line, with a specific height
*
* @access protected
* @param float $h
* @param integer $curr real current position in the text, if new line in the write of a text
*/
protected function _setNewLine($h, $curr = null)
{
$this->pdf->Ln($h);
$this->_setNewPositionForNewLine($curr);
}
/**
* calculate the start position of the next line, depending on the text-align
*
* @access protected
* @param integer $curr real current position in the text, if new line in the write of a text
*/
protected function _setNewPositionForNewLine($curr = null)
{
// get the margins for the current line
list($lx, $rx) = $this->_getMargins($this->pdf->getY());
$this->pdf->setX($lx);
$wMax = $rx-$lx;
$this->_currentH = 0;
// if subPart => return because align left
if ($this->_subPart || $this->_isSubPart || $this->_isForOneLine) {
$this->pdf->setWordSpacing(0);
return null;
}
// create the sub object
$sub = null;
$this->_createSubHTML($sub);
$sub->_saveMargin(0, 0, $sub->pdf->getW()-$wMax);
$sub->_isForOneLine = true;
$sub->_parsePos = $this->_parsePos;
$sub->parsingHtml->code = $this->parsingHtml->code;
// if $curr => adapt the current position of the parsing
if ($curr!==null && $sub->parsingHtml->code[$this->_parsePos]['name']=='write') {
$txt = $sub->parsingHtml->code[$this->_parsePos]['param']['txt'];
$txt = str_replace('[[page_cu]]', $sub->pdf->getMyNumPage($this->_page), $txt);
$sub->parsingHtml->code[$this->_parsePos]['param']['txt'] = substr($txt, $curr+1);
} else
$sub->_parsePos++;
// for each element of the parsing => load the action
$res = null;
for ($sub->_parsePos; $sub->_parsePos<count($sub->parsingHtml->code); $sub->_parsePos++) {
$action = $sub->parsingHtml->code[$sub->_parsePos];
$res = $sub->_executeAction($action);
if (!$res) break;
}
$w = $sub->_maxX; // max width
$h = $sub->_maxH; // max height
$e = ($res===null ? $sub->_maxE : 0); // maxnumber of elemets on the line
// destroy the sub HTML
$this->_destroySubHTML($sub);
// adapt the start of the line, depending on the text-align
if ($this->parsingCss->value['text-align']=='center')
$this->pdf->setX(($rx+$this->pdf->getX()-$w)*0.5-0.01);
else if ($this->parsingCss->value['text-align']=='right')
$this->pdf->setX($rx-$w-0.01);
else
$this->pdf->setX($lx);
// set the height of the line
$this->_currentH = $h;
// if justify => set the word spacing
if ($this->parsingCss->value['text-align']=='justify' && $e>1) {
$this->pdf->setWordSpacing(($wMax-$w)/($e-1));
} else {
$this->pdf->setWordSpacing(0);
}
}
/**
* prepare HTML2PDF::$_subobj (used for create the sub HTML2PDF objects
*
* @access protected
*/
protected function _prepareSubObj()
{
$pdf = null;
// create the sub object
HTML2PDF::$_subobj = new HTML2PDF(
$this->_orientation,
$this->_format,
$this->_langue,
$this->_unicode,
$this->_encoding,
array($this->_defaultLeft,$this->_defaultTop,$this->_defaultRight,$this->_defaultBottom)
);
// init
HTML2PDF::$_subobj->setTestTdInOnePage($this->_testTdInOnepage);
HTML2PDF::$_subobj->setTestIsImage($this->_testIsImage);
HTML2PDF::$_subobj->setTestIsDeprecated($this->_testIsDeprecated);
HTML2PDF::$_subobj->setDefaultFont($this->_defaultFont);
HTML2PDF::$_subobj->parsingCss->css = &$this->parsingCss->css;
HTML2PDF::$_subobj->parsingCss->cssKeys = &$this->parsingCss->cssKeys;
// copy global parameters
HTML2PDF::$_subobj->_isCalculationPass = $this->_isCalculationPass;
HTML2PDF::$_subobj->_rootPathForURLs = $this->_rootPathForURLs;
// clone font from the original PDF
HTML2PDF::$_subobj->pdf->cloneFontFrom($this->pdf);
// remove the link to the parent
HTML2PDF::$_subobj->parsingCss->setPdfParent($pdf);
}