Skip to content

Commit dc4218e

Browse files
authored
Merge pull request #45 from YetiForceCompany/developer
YetiForcePDF v0.1.41
2 parents 0775b80 + 6e12654 commit dc4218e

File tree

7 files changed

+102
-34
lines changed

7 files changed

+102
-34
lines changed

README.MD

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
[![Latest Stable Version](https://poser.pugx.org/yetiforce/yetiforcepdf/v/stable)](https://packagist.org/packages/yetiforce/yetiforcepdf)
33
[![Build Status](https://travis-ci.org/YetiForceCompany/YetiForcePDF.svg?branch=developer)](https://travis-ci.org/YetiForceCompany/YetiForcePDF)
44
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/YetiForceCompany/YetiForcePDF/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/YetiForceCompany/YetiForcePDF/?branch=master)
5-
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/b2e8645f5091496089ed203d05a05d61)](https://app.codacy.com/app/mariuszkrzaczkowski/YetiForcePDF?utm_source=github.com&utm_medium=referral&utm_content=YetiForceCompany/YetiForcePDF&utm_campaign=Badge_Grade_Settings)
65
[![Maintainability](https://api.codeclimate.com/v1/badges/af478ddd07cf7278841a/maintainability)](https://codeclimate.com/github/YetiForceCompany/YetiForcePDF/maintainability)
76

87
## PDF generation library for PHP
98
The best library in the world to generate PDF from HTML
109

10+
## Issues & bugs
11+
Report errors related to PDF in https://github.com/YetiForceCompany/YetiForceCRM/issues
12+
1113
## Basic usage (for more take a look at examples folder)
1214

1315
```php
@@ -196,3 +198,12 @@ When you want to place page number (in header or footer for example) you can do
196198
## License
197199

198200
Distributed under the MIT license. See LICENSE for details.
201+
202+
## 👥 Contributors
203+
204+
This project exists thanks to all the people who contribute.
205+
206+
<a href="https://github.com/YetiForceCompany/YetiForcePDF/graphs/contributors">
207+
<img src="https://contrib.rocks/image?repo=YetiForceCompany/YetiForcePDF" />
208+
</a>
209+

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"phenx/php-font-lib": "^0.5",
2929
"guzzlehttp/guzzle": "^7",
3030
"composer/ca-bundle": "^1",
31-
"milon/barcode": "^9",
31+
"milon/barcode": "^11",
3232
"sabberworm/php-css-parser": "^8"
3333
},
3434
"autoload": {

lib/Document.php

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
namespace YetiForcePDF;
1616

17+
use Exception;
18+
use YetiForcePDF\Html\Parser;
1719
use YetiForcePDF\Layout\FooterBox;
1820
use YetiForcePDF\Layout\HeaderBox;
1921
use YetiForcePDF\Layout\WatermarkBox;
@@ -31,34 +33,40 @@ class Document
3133
* @var int
3234
*/
3335
protected $actualId = 0;
36+
3437
/**
3538
* Main output buffer / content for pdf file.
3639
*
3740
* @var string
3841
*/
3942
protected $buffer = '';
43+
4044
/**
4145
* Main entry point - root element.
4246
*
4347
* @var \YetiForcePDF\Catalog
4448
*/
4549
protected $catalog;
50+
4651
/**
4752
* Pages dictionary.
4853
*
4954
* @var Pages
5055
*/
5156
protected $pagesObject;
57+
5258
/**
5359
* Current page object.
5460
*
5561
* @var Page
5662
*/
5763
protected $currentPageObject;
64+
5865
/**
5966
* @var string default page format
6067
*/
6168
protected $defaultFormat = 'A4';
69+
6270
/**
6371
* @var string default page orientation
6472
*/
@@ -68,6 +76,7 @@ class Document
6876
* @var Page[] all pages in the document
6977
*/
7078
protected $pages = [];
79+
7180
/**
7281
* Default page margins.
7382
*
@@ -79,68 +88,82 @@ class Document
7988
'right' => 40,
8089
'bottom' => 40,
8190
];
91+
8292
/**
8393
* All objects inside document.
8494
*
8595
* @var \YetiForcePDF\Objects\PdfObject[]
8696
*/
8797
protected $objects = [];
98+
8899
/**
89-
* @var \YetiForcePDF\Html\Parser
100+
* @var Parser
90101
*/
91102
protected $htmlParser;
103+
92104
/**
93105
* Fonts data.
94106
*
95107
* @var array
96108
*/
97109
protected $fontsData = [];
110+
98111
/**
99112
* @var array
100113
*/
101114
protected $fontInstances = [];
115+
102116
/**
103117
* Actual font id.
104118
*
105119
* @var int
106120
*/
107121
protected $actualFontId = 0;
122+
108123
/**
109124
* Actual graphic state id.
110125
*
111126
* @var int
112127
*/
113128
protected $actualGraphicStateId = 0;
129+
114130
/**
115131
* @var bool
116132
*/
117133
protected $debugMode = false;
134+
118135
/**
119136
* @var HeaderBox|null
120137
*/
121138
protected $header;
139+
122140
/**
123141
* @var FooterBox|null
124142
*/
125143
protected $footer;
144+
126145
/**
127146
* @var WatermarkBox|null
128147
*/
129148
protected $watermark;
149+
130150
/**
131151
* @var Meta
132152
*/
133153
protected $meta;
154+
134155
/**
135156
* @var bool
136157
*/
137158
protected $parsed = false;
159+
138160
/**
139161
* Characters int values cache for fonts.
140162
*
141163
* @var array
142164
*/
143-
protected $ordCache = [];
165+
public $ordCache = [];
166+
144167
/**
145168
* Css selectors like classes ids.
146169
*
@@ -444,7 +467,7 @@ public function getFontData(string $family, string $weight, string $style)
444467
*/
445468
public static function addFonts(array $fonts)
446469
{
447-
return \YetiForcePDF\Objects\Font::loadFromArray($fonts);
470+
\YetiForcePDF\Objects\Font::loadFromArray($fonts);
448471
}
449472

450473
/**
@@ -765,14 +788,19 @@ public function removeObject(PdfObject $object): self
765788
* Load html string.
766789
*
767790
* @param string $html
768-
* @param string $inputEncoding
791+
* @param string $fromEncoding
769792
*
770793
* @return $this
794+
* @throws Exception
771795
*/
772-
public function loadHtml(string $html, string $inputEncoding = 'UTF-8')
796+
public function loadHtml(string $html, string $fromEncoding = 'UTF-8'): self
773797
{
774-
$this->htmlParser = (new \YetiForcePDF\Html\Parser())->setDocument($this)->init();
775-
$this->htmlParser->loadHtml($html, $inputEncoding);
798+
if ($fromEncoding === '') {
799+
throw new Exception('Encoding can not be empty');
800+
}
801+
802+
$this->htmlParser = (new Parser())->setDocument($this)->init();
803+
$this->htmlParser->loadHtml($html, $fromEncoding);
776804

777805
return $this;
778806
}
@@ -872,34 +900,41 @@ public function parse()
872900
*/
873901
public function render(): string
874902
{
875-
$xref = $this->buffer = '';
903+
$xref = '';
904+
$this->buffer = '';
905+
876906
$this->buffer .= $this->getDocumentHeader();
877907
$this->parse();
878908
$objectSize = 0;
909+
879910
foreach ($this->objects as $object) {
880911
if (\in_array($object->getBasicType(), ['Dictionary', 'Stream', 'Array'])) {
881912
$xref .= sprintf("%010d 00000 n \n", \strlen($this->buffer));
882913
$this->buffer .= $object->render() . "\n";
883914
++$objectSize;
884915
}
885916
}
917+
886918
$offset = \strlen($this->buffer);
887919
$this->buffer .= implode("\n", [
888920
'xref',
889-
'0 ' . ($objectSize + 1),
921+
'0 ' . $objectSize,
890922
'0000000000 65535 f ',
891923
$xref,
892924
]);
925+
893926
$trailer = (new \YetiForcePDF\Objects\Trailer())
894927
->setDocument($this)->setRootObject($this->catalog)->setSize($objectSize);
928+
895929
$this->buffer .= $trailer->render() . "\n";
896-
// $this->buffer .= implode("\n", [
897-
// 'startxref',
898-
// $offset,
899-
// '',
900-
// ]);
930+
$this->buffer .= implode("\n", [
931+
'startxref',
932+
$offset,
933+
'',
934+
]);
901935
$this->buffer .= $this->getDocumentFooter();
902936
$this->removeObject($trailer);
937+
903938
return $this->buffer;
904939
}
905940

@@ -910,7 +945,7 @@ public function render(): string
910945
*
911946
* @return array
912947
*/
913-
public function getCssSelectorRules(string $selector)
948+
public function getCssSelectorRules(string $selector): array
914949
{
915950
$rules = [];
916951
foreach (explode(' ', $selector) as $className) {

lib/Html/Parser.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/**
56
* Parser class.
67
*
@@ -13,25 +14,29 @@
1314

1415
namespace YetiForcePDF\Html;
1516

17+
use YetiForcePDF\Base;
1618
use YetiForcePDF\Layout\PageGroupBox;
1719

1820
/**
1921
* Class Parser.
2022
*/
21-
class Parser extends \YetiForcePDF\Base
23+
class Parser extends Base
2224
{
2325
/**
2426
* @var \DOMDocument
2527
*/
2628
protected $domDocument;
29+
2730
/**
2831
* @var string
2932
*/
3033
protected $html = '';
34+
3135
/**
3236
* @var array page groups with html content divided
3337
*/
3438
protected $htmlPageGroups = [];
39+
3540
/**
3641
* @var array
3742
*/
@@ -57,13 +62,19 @@ protected function cleanUpHtml(string $html)
5762
* @param string $html
5863
* @param string $fromEncoding
5964
*
60-
* @return \YetiForcePDF\Html\Parser
65+
* @return Parser
6166
*/
62-
public function loadHtml(string $html, string $fromEncoding = ''): self
67+
public function loadHtml(string $html, string $fromEncoding): self
6368
{
64-
$html = htmlspecialchars_decode($html, ENT_HTML5);
69+
$this->html = htmlspecialchars_decode($html, ENT_HTML5);
6570
$this->html = $this->cleanUpHtml($html);
66-
$this->html = mb_convert_encoding($this->html, 'HTML-ENTITIES', $fromEncoding);
71+
72+
// 0x80 - start of unicode range
73+
// 0x10FFFF - end of unicode range
74+
// 0 - do not ommit any unicode char
75+
// ~0 - negated 0 - convert negation of nothing (so convert all)
76+
$this->html = mb_encode_numericentity($this->html, [0x80, 0x10FFFF, 0, ~0], $fromEncoding);
77+
6778
return $this;
6879
}
6980

@@ -72,7 +83,7 @@ public function loadHtml(string $html, string $fromEncoding = ''): self
7283
*
7384
* @return string
7485
*/
75-
public function getHtml()
86+
public function getHtml(): string
7687
{
7788
return $this->html;
7889
}

0 commit comments

Comments
 (0)