forked from spipu/html2pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtml2Pdf.php
More file actions
executable file
·6481 lines (5574 loc) · 212 KB
/
Html2Pdf.php
File metadata and controls
executable file
·6481 lines (5574 loc) · 212 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
/**
* Html2Pdf Library - main class
*
* HTML => PDF converter
* distributed under the OSL-3.0 License
*
* @package Html2pdf
* @author Laurent MINGUET <webmaster@html2pdf.fr>
* @copyright 2025 Laurent MINGUET
*/
namespace Spipu\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ImageException;
use Spipu\Html2Pdf\Exception\LongSentenceException;
use Spipu\Html2Pdf\Exception\TableException;
use Spipu\Html2Pdf\Exception\HtmlParsingException;
use Spipu\Html2Pdf\Extension\Core;
use Spipu\Html2Pdf\Extension\ExtensionInterface;
use Spipu\Html2Pdf\Parsing\HtmlLexer;
use Spipu\Html2Pdf\Parsing\Node;
use Spipu\Html2Pdf\Parsing\TagParser;
use Spipu\Html2Pdf\Parsing\TextParser;
use Spipu\Html2Pdf\Security\Security;
use Spipu\Html2Pdf\Security\SecurityInterface;
use Spipu\Html2Pdf\Tag\TagInterface;
use Spipu\Html2Pdf\Debug\DebugInterface;
use Spipu\Html2Pdf\Debug\Debug;
require_once dirname(__FILE__) . '/config/tcpdf.config.php';
class Html2Pdf
{
/**
* myPdf object, extends from TCPDF
* @var MyPdf
*/
public $pdf = null;
/**
* CSS parsing
* @var Parsing\Css
*/
public $parsingCss = null;
/**
* HTML parsing
* @var Parsing\Html
*/
public $parsingHtml = null;
/**
* @var Debug
*/
private $debug;
/**
* @var HtmlLexer
*/
private $lexer;
/**
* @var CssConverter
*/
private $cssConverter;
/**
* @var SvgDrawer
*/
private $svgDrawer;
/**
* @var SecurityInterface
*/
private $security;
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)
/**
* @var false|int
*/
protected $_pdfa;
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 $_fallbackImage = null; // fallback image to use in img tags
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 $_hideHeader = array(); // array : list of pages which the header gonna be hidden
protected $_hideFooter = array(); // array : list of pages which the footer gonna be hidden
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 $_sentenceMaxLines = 1000; // max number of lines for a sentence
/**
* @var Html2Pdf
*/
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
/**
* list of tag definitions
* @var ExtensionInterface[]
*/
protected $extensions = array();
/**
* List of tag objects
* @var TagInterface[]
*/
protected $tagObjects = array();
/**
* @var bool
*/
protected $extensionsLoaded = false;
/**
* class constructor
*
* @param string $orientation page orientation, same as TCPDF
* @param mixed $format The format used for pages, same as TCPDF
* @param string $lang Lang : 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 $margins Default margins (left, top, right, bottom)
* @param false|int $pdfa If TRUE set the document to PDF/A mode.
*
* @return Html2Pdf
*/
public function __construct(
$orientation = 'P',
$format = 'A4',
$lang = 'fr',
$unicode = true,
$encoding = 'UTF-8',
$margins = array(5, 5, 5, 8),
$pdfa = false
) {
// init the page number
$this->_page = 0;
$this->_firstPage = true;
// save the parameters
$this->_orientation = $orientation;
$this->_format = $format;
$this->_langue = strtolower($lang);
$this->_unicode = $unicode;
$this->_encoding = $encoding;
$this->_pdfa = $pdfa;
// load the Locale
Locale::load($this->_langue);
$this->security = new Security();
$this->pdf = new MyPdf($orientation, 'mm', $format, $unicode, $encoding, false, $pdfa);
$this->cssConverter = new CssConverter();
$textParser = new TextParser($encoding);
$this->parsingCss = new Parsing\Css(
$this->pdf,
new TagParser($textParser),
$this->cssConverter,
$this->security
);
$this->parsingCss->fontSet();
$this->_defList = array();
// init some tests
$this->setTestTdInOnePage(true);
$this->setTestIsImage(true);
// init the default font
$this->setDefaultFont(null);
$this->lexer = new HtmlLexer();
// init the HTML parsing object
$this->parsingHtml = new Parsing\Html($textParser);
$this->_subHtml = null;
$this->_subPart = false;
$this->setDefaultMargins($margins);
$this->setMargins();
$this->_marges = array();
// init the form's fields
$this->_lstField = array();
$this->svgDrawer = new SvgDrawer($this->pdf, $this->cssConverter);
$this->addExtension(new Core\HtmlExtension());
$this->addExtension(new Core\SvgExtension($this->svgDrawer));
return $this;
}
/**
* Gets the detailed version as array
*
* @return array
*/
public function getVersionAsArray()
{
return array(
'major' => 5,
'minor' => 3,
'revision' => 1,
);
}
/**
* Gets the current version as string
*
* @return string
*/
public function getVersion()
{
$v = $this->getVersionAsArray();
return $v['major'].'.'.$v['minor'].'.'.$v['revision'];
}
/**
* Clone to create a sub Html2Pdf from self::$_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);
}
/**
* Use a specific security interface
* @param SecurityInterface $security
* @return $this
*/
public function setSecurityService(SecurityInterface $security): self
{
$this->security = $security;
$this->parsingCss->setSecurityService($security);
return $this;
}
/**
* Set the max number of lines for a sentence
*
* @param int $nbLines
*
* @return $this
*/
public function setSentenceMaxLines($nbLines)
{
$this->_sentenceMaxLines = (int) $nbLines;
return $this;
}
/**
* Get the max number of lines for a sentence
*
* @return int
*/
public function getSentenceMaxLines()
{
return $this->_sentenceMaxLines;
}
/**
* @param ExtensionInterface $extension
*/
public function addExtension(ExtensionInterface $extension)
{
$name = strtolower($extension->getName());
$this->extensions[$name] = $extension;
}
/**
* Get the number of pages
* @return int
*/
public function getNbPages()
{
return $this->_page;
}
/**
* Initialize the registered extensions
*
* @throws Html2PdfException
*/
protected function loadExtensions()
{
if ($this->extensionsLoaded) {
return;
}
foreach ($this->extensions as $extension) {
foreach ($extension->getTags() as $tag) {
if (!$tag instanceof TagInterface) {
throw new Html2PdfException('The ExtensionInterface::getTags() method must return an array of TagInterface.');
}
$this->addTagObject($tag);
}
}
$this->extensionsLoaded = true;
}
/**
* register a tag object
*
* @param TagInterface $tagObject the object
*/
protected function addTagObject(TagInterface $tagObject)
{
$tagName = strtolower($tagObject->getName());
$this->tagObjects[$tagName] = $tagObject;
}
/**
* get the tag object from a tag name
*
* @param string $tagName tag name to load
*
* @return TagInterface|null
*/
protected function getTagObject($tagName)
{
if (!$this->extensionsLoaded) {
$this->loadExtensions();
}
if (!array_key_exists($tagName, $this->tagObjects)) {
return null;
}
$tagObject = $this->tagObjects[$tagName];
$tagObject->setParsingCssObject($this->parsingCss);
$tagObject->setCssConverterObject($this->cssConverter);
$tagObject->setPdfObject($this->pdf);
if (!is_null($this->debug)) {
$tagObject->setDebugObject($this->debug);
}
return $tagObject;
}
/**
* set the debug mode to On
*
* @param DebugInterface|null $debugObject
*
* @return Html2Pdf $this
*/
public function setModeDebug(?DebugInterface $debugObject = null)
{
if (is_null($debugObject)) {
$this->debug = new Debug();
} else {
$this->debug = $debugObject;
}
$this->debug->start();
return $this;
}
/**
* Set the test of TD that 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 default font to use, if no font is specified, or if the asked font does not exist
*
* @access public
* @param string $default name of the default font to use. If null : Arial if no font is specified, 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;
}
/**
* Set a fallback image
*
* @param string $fallback Path or URL to the fallback image
*
* @return $this
*/
public function setFallbackImage($fallback)
{
$this->_fallbackImage = $fallback;
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 $file 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
* @param string $marginTop margin top to use on the index page
* @return null
*/
public function createIndex(
$titre = 'Index',
$sizeTitle = 20,
$sizeBookmark = 15,
$bookmarkTitle = true,
$displayPage = true,
$onPage = null,
$fontName = null,
$marginTop = null
) {
if ($fontName === null) {
$fontName = 'helvetica';
}
$oldPage = $this->_INDEX_NewPage($onPage);
if ($marginTop !== null) {
$marginTop = $this->cssConverter->convertToMM($marginTop);
$this->pdf->SetY($this->pdf->GetY() + $marginTop);
}
$this->pdf->createIndex($this, $titre, $sizeTitle, $sizeBookmark, $bookmarkTitle, $displayPage, $onPage, $fontName);
if ($oldPage) {
$this->pdf->setPage($oldPage);
}
}
/**
* clean up the objects, if the method output can not be called because of an exception
*
* @return Html2Pdf
*/
public function clean()
{
self::$_subobj = null;
self::$_tables = array();
Locale::clean();
return $this;
}
/**
* 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
* E : return the document as base64 mime multi-part email attachment (RFC 2045)
*
* @param string $name The name of the file when saved.
* @param string $dest Destination where to send the document.
*
* @throws Html2PdfException
* @return string content of the PDF, if $dest=S
* @see TCPDF::close
*/
public function output($name = 'document.pdf', $dest = 'I')
{
// if on debug mode
if (!is_null($this->debug)) {
$this->debug->stop();
$this->pdf->Close();
return '';
}
//Normalize parameters
$dest = strtoupper($dest);
if (!in_array($dest, array('I', 'D', 'F', 'S', 'FI','FD', 'E'))) {
throw new Html2PdfException('The output destination mode ['.$dest.'] is invalid');
}
if ($dest !== 'S') {
// the name must be a PDF name
if (strtolower(substr($name, -4)) !== '.pdf') {
throw new Html2PdfException('The output document name [' . $name . '] is not a PDF name');
}
}
// if save on server: it must be an absolute path
if ($dest[0] === 'F') {
$isWindowsPath = preg_match("/^[A-Z]:\\\\/", $name);
// If windows is not saving on a remote file server
if($name[0] !== DIRECTORY_SEPARATOR && $isWindowsPath === false ){
$name = getcwd() . DIRECTORY_SEPARATOR . $name;
}
}
// call the output of TCPDF
$output = $this->pdf->Output($name, $dest);
// close the pdf and clean up
$this->clean();
return $output;
}
/**
* convert HTML to PDF
*
* @param string $html
*
* @return Html2Pdf
*/
public function writeHTML($html)
{
$html = $this->parsingHtml->prepareHtml($html);
$html = $this->parsingCss->extractStyle($html);
$this->parsingHtml->parse($this->lexer->tokenize($html));
$this->_makeHTMLcode();
return $this;
}
/**
* Preview the HTML before conversion
*
* @param string $html
*
* @return void
*/
public function previewHTML($html)
{
$html = $this->parsingHtml->prepareHtml($html);
$html = preg_replace('/<page([^>]*)>/isU', '<hr>Page : $1<hr><div$1>', $html);
$html = preg_replace('/<page_header([^>]*)>/isU', '<hr>Page Header : $1<hr><div$1>', $html);
$html = preg_replace('/<page_footer([^>]*)>/isU', '<hr>Page Footer : $1<hr><div$1>', $html);
$html = preg_replace('/<\/page([^>]*)>/isU', '</div><hr>', $html);
$html = preg_replace('/<\/page_header([^>]*)>/isU', '</div><hr>', $html);
$html = preg_replace('/<\/page_footer([^>]*)>/isU', '</div><hr>', $html);
$html = preg_replace('/<bookmark([^>]*)>/isU', '<hr>bookmark : $1<hr>', $html);
$html = preg_replace('/<\/bookmark([^>]*)>/isU', '', $html);
$html = preg_replace('/<barcode([^>]*)>/isU', '<hr>barcode : $1<hr>', $html);
$html = preg_replace('/<\/barcode([^>]*)>/isU', '', $html);
$html = preg_replace('/<qrcode([^>]*)>/isU', '<hr>qrcode : $1<hr>', $html);
$html = preg_replace('/<\/qrcode([^>]*)>/isU', '', $html);
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>HTML View</title>
<meta http-equiv="Content-Type" content="text/html; charset='.$this->_encoding.'" >
</head>
<body style="padding: 10px; font-size: 10pt;font-family: Verdana;">
'.$html.'
</body>
</html>';
}
/**
* init a sub Html2Pdf. do 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();
}
/**
* set the default margins of the page
*
* @param array|int $margins (mm, left top right bottom)
*/
protected function setDefaultMargins($margins)
{
if (!is_array($margins)) {
$margins = array($margins, $margins, $margins, $margins);
}
if (!isset($margins[2])) {
$margins[2] = $margins[0];
}
if (!isset($margins[3])) {
$margins[3] = 8;
}
$this->_defaultLeft = $this->cssConverter->convertToMM($margins[0].'mm');
$this->_defaultTop = $this->cssConverter->convertToMM($margins[1].'mm');
$this->_defaultRight = $this->cssConverter->convertToMM($margins[2].'mm');
$this->_defaultBottom = $this->cssConverter->convertToMM($margins[3].'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 parser (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->_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']) && is_array($this->_background['img'])) {
$imageWidth = $this->cssConverter->convertToMM($this->_background['width'], $this->pdf->getW());
$imageHeight = $imageWidth * $this->_background['img']['height'] / $this->_background['img']['width'];
$posX = $this->cssConverter->convertToMM($this->_background['posX'], $this->pdf->getW() - $imageWidth);
$posY = $this->cssConverter->convertToMM($this->_background['posY'], $this->pdf->getH() - $imageHeight);
$this->pdf->Image($this->_background['img']['file'], $posX, $posY, $imageWidth);
}
}
$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
*/
protected function setMargins()
{
// prepare the margins
$this->_margeLeft = $this->_defaultLeft + (isset($this->_background['left']) ? $this->_background['left'] : 0);
$this->_margeRight = $this->_defaultRight + (isset($this->_background['right']) ? $this->_background['right'] : 0);
$this->_margeTop = $this->_defaultTop + (isset($this->_background['top']) ? $this->_background['top'] : 0);
$this->_margeBottom = $this->_defaultBottom + (isset($this->_background['bottom']) ? $this->_background['bottom'] : 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);
}
}
/**
* 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()
{
if (!count($this->_subHEADER)) {
return false;
}
if (in_array($this->pdf->getPage(), $this->_hideHeader)) {
return false;
}
$oldParsePos = $this->_parsePos;
$oldParseCode = $this->parsingHtml->code;
$this->_parsePos = 0;
$this->parsingHtml->code = $this->_subHEADER;
$this->_makeHTMLcode();
$this->_parsePos = $oldParsePos;
$this->parsingHtml->code = $oldParseCode;
}
/**
* draw the PDF footer with the HTML in page_footer
*
* @access protected