Skip to content

Commit

Permalink
[DRAFT] Support for xobject templates (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni authored Aug 17, 2024
1 parent 967cd0e commit a9db5a8
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 118 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.0.68
8.0.69
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
"ext-date": "*",
"ext-pcre": "*",
"tecnickcom/tc-lib-barcode": "^2.2",
"tecnickcom/tc-lib-color": "^2.0",
"tecnickcom/tc-lib-pdf-image": "^2.0",
"tecnickcom/tc-lib-pdf-font": "^2.3",
"tecnickcom/tc-lib-color": "^2.2",
"tecnickcom/tc-lib-pdf-image": "^2.1",
"tecnickcom/tc-lib-pdf-font": "^2.4",
"tecnickcom/tc-lib-file": "^2.0",
"tecnickcom/tc-lib-pdf-encrypt": "^2.1",
"tecnickcom/tc-lib-unicode-data": "^2.0",
"tecnickcom/tc-lib-unicode": "^2.0",
"tecnickcom/tc-lib-pdf-page": "^4.0",
"tecnickcom/tc-lib-pdf-graph": "^2.0"
"tecnickcom/tc-lib-pdf-graph": "^2.1"
},
"require-dev": {
"pdepend/pdepend": "2.13.0",
Expand Down
37 changes: 37 additions & 0 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

$bfont1 = $pdf->font->insert($pdf->pon, 'helvetica', '', 12);


// ----------
// Add first page

Expand Down Expand Up @@ -1383,6 +1384,7 @@


// ----------

// Page signature

$pageC01 = $pdf->page->add();
Expand Down Expand Up @@ -1431,8 +1433,43 @@

$pdf->addEmptySignatureAppearance(30, 60, 20, 20, -1, 'test');


// ----------

// XOBject template (@TODO: fix the implementation)

$pageC02 = $pdf->page->add();

$tid = $pdf->newXObjectTemplate(80, 80, []);

$xcnz = $pdf->graph->getStartTransform();
$xcnz = $pdf->graph->getStarPolygon(50, 50, 40, 10, 3, 0, 'CNZ');
$timg = $pdf->image->add('../vendor/tecnickcom/tc-lib-pdf-image/test/images/200x100_GRAY.png');
$xcnz .= $pdf->image->getSetImage($timg, 10, 10, 80, 80, $pageC02['height']);
$xcnz .= $pdf->graph->getStopTransform();

$pdf->addXObjectImageID($tid, $timg);
$pdf->addXObjectContent($tid, $xcnz);

$pdf->exitXObjectTemplate();

$tmpl = $pdf->getXObjectTemplate(
$tid,
0,
0,
80,
80,
'T',
'L',
);

$pdf->page->addContent($tmpl);

// ----------

// =============================================================

// ----------
// get PDF document as raw string
$rawpdf = $pdf->getOutPDFString();

Expand Down
2 changes: 1 addition & 1 deletion resources/debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ Vcs-Git: https://github.com/~#VENDOR#~/~#PROJECT#~.git
Package: ~#PKGNAME#~
Provides: php-~#PROJECT#~
Architecture: all
Depends: php (>= 8.0.0), php-date, php-tecnickcom-tc-lib-barcode (<< 3.0.0), php-tecnickcom-tc-lib-barcode (>= 2.2.2), php-tecnickcom-tc-lib-color (<< 3.0.0), php-tecnickcom-tc-lib-color (>= 2.0.9), php-tecnickcom-tc-lib-pdf-image (<< 3.0.0), php-tecnickcom-tc-lib-pdf-image (>= 2.0.15), php-tecnickcom-tc-lib-pdf-font (<< 3.0.0), php-tecnickcom-tc-lib-pdf-font (>= 2.3.1), php-tecnickcom-tc-lib-file (<< 3.0.0), php-tecnickcom-tc-lib-file (>= 2.0.12), php-tecnickcom-tc-lib-pdf-encrypt (<< 3.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 2.1.3), php-tecnickcom-tc-lib-unicode-data (<< 3.0.0), php-tecnickcom-tc-lib-unicode-data (>= 2.0.12), php-tecnickcom-tc-lib-unicode (<< 3.0.0), php-tecnickcom-tc-lib-unicode (>= 2.0.12), php-tecnickcom-tc-lib-pdf-page (<< 5.0.0), php-tecnickcom-tc-lib-pdf-page (>= 4.0.10), php-tecnickcom-tc-lib-pdf-graph (<< 3.0.0), php-tecnickcom-tc-lib-pdf-graph (>= 2.0.13), ${misc:Depends}
Depends: php (>= 8.0.0), php-date, php-tecnickcom-tc-lib-barcode (<< 3.0.0), php-tecnickcom-tc-lib-barcode (>= 2.2.2), php-tecnickcom-tc-lib-color (<< 3.0.0), php-tecnickcom-tc-lib-color (>= 2.2.1), php-tecnickcom-tc-lib-pdf-image (<< 3.0.0), php-tecnickcom-tc-lib-pdf-image (>= 2.1.0), php-tecnickcom-tc-lib-pdf-font (<< 3.0.0), php-tecnickcom-tc-lib-pdf-font (>= 2.4.0), php-tecnickcom-tc-lib-file (<< 3.0.0), php-tecnickcom-tc-lib-file (>= 2.0.12), php-tecnickcom-tc-lib-pdf-encrypt (<< 3.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 2.1.3), php-tecnickcom-tc-lib-unicode-data (<< 3.0.0), php-tecnickcom-tc-lib-unicode-data (>= 2.0.12), php-tecnickcom-tc-lib-unicode (<< 3.0.0), php-tecnickcom-tc-lib-unicode (>= 2.0.12), php-tecnickcom-tc-lib-pdf-page (<< 5.0.0), php-tecnickcom-tc-lib-pdf-page (>= 4.0.10), php-tecnickcom-tc-lib-pdf-graph (<< 3.0.0), php-tecnickcom-tc-lib-pdf-graph (>= 2.1.0), ${misc:Depends}
Description: PHP Barcode library
This library includes PHP classes to generate PDF documents.
8 changes: 4 additions & 4 deletions resources/rpm/rpm.spec
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ Requires: php-pcre
Requires: php-composer(%{c_vendor}/tc-lib-barcode) < 3.0.0
Requires: php-composer(%{c_vendor}/tc-lib-barcode) >= 2.2.2
Requires: php-composer(%{c_vendor}/tc-lib-color) < 3.0.0
Requires: php-composer(%{c_vendor}/tc-lib-color) >= 2.0.9
Requires: php-composer(%{c_vendor}/tc-lib-color) >= 2.2.1
Requires: php-composer(%{c_vendor}/tc-lib-pdf-image) < 3.0.0
Requires: php-composer(%{c_vendor}/tc-lib-pdf-image) >= 2.0.15
Requires: php-composer(%{c_vendor}/tc-lib-pdf-image) >= 2.1.0
Requires: php-composer(%{c_vendor}/tc-lib-pdf-font) < 3.0.0
Requires: php-composer(%{c_vendor}/tc-lib-pdf-font) >= 2.3.1
Requires: php-composer(%{c_vendor}/tc-lib-pdf-font) >= 2.4.0
Requires: php-composer(%{c_vendor}/tc-lib-file) < 3.0.0
Requires: php-composer(%{c_vendor}/tc-lib-file) >= 2.0.12
Requires: php-composer(%{c_vendor}/tc-lib-pdf-encrypt) < 3.0.0
Expand All @@ -38,7 +38,7 @@ Requires: php-composer(%{c_vendor}/tc-lib-unicode) >= 2.0.12
Requires: php-composer(%{c_vendor}/tc-lib-pdf-page) < 5.0.0
Requires: php-composer(%{c_vendor}/tc-lib-pdf-page) >= 4.0.10
Requires: php-composer(%{c_vendor}/tc-lib-pdf-graph) < 3.0.0
Requires: php-composer(%{c_vendor}/tc-lib-pdf-graph) >= 2.0.13
Requires: php-composer(%{c_vendor}/tc-lib-pdf-graph) >= 2.1.0

Provides: php-composer(%{c_vendor}/%{gh_project}) = %{version}
Provides: php-%{gh_project} = %{version}
Expand Down
9 changes: 1 addition & 8 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ abstract class Base
/**
* TCPDF version.
*/
protected string $version = '8.0.68';
protected string $version = '8.0.69';

/**
* Time is seconds since EPOCH when the document was created.
Expand Down Expand Up @@ -344,13 +344,6 @@ abstract class Base
'xmp' => 0,
];

/**
* Store XObject.
*
* @var array<string, TXOBject>
*/
protected array $xobject = [];

/**
* Current XOBject template ID.
*
Expand Down
138 changes: 46 additions & 92 deletions src/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

use Com\Tecnick\Pdf\Exception as PdfException;
use Com\Tecnick\Pdf\Font\Output as OutFont;
use Com\Tecnick\Color\Pdf as ObjColor;
use Com\Tecnick\Pdf\Font\Stack as ObjFont;
use Com\Tecnick\Pdf\Graph\Draw as ObjGraph;
use Com\Tecnick\Pdf\Image\Import as ObjImage;

/**
* Com\Tecnick\Pdf\Output
Expand Down Expand Up @@ -341,14 +337,13 @@
* }
*
* @phpstan-type TXOBject array{
* 'colorspace'?: array<string>,
* 'extgstate'?: array<string>,
* 'font'?: array<string>,
* 'image'?: array<string>,
* 'pattern'?: array<string>,
* 'shading'?: array<string>,
* 'xobject'?: array<string>,
* 'annotations'?: array<int, TAnnot>,
* 'spot_colors': array<string>,
* 'extgstate': array<int>,
* 'gradient': array<int>,
* 'font': array<string>,
* 'image': array<int>,
* 'xobject': array<string>,
* 'annotations': array<int, TAnnot>,
* 'transparency'?: ?TGTransparency,
* 'id': string,
* 'outdata': string,
Expand Down Expand Up @@ -442,6 +437,13 @@
*/
abstract class Output extends \Com\Tecnick\Pdf\MetaInfo
{
/**
* Object to export fornt data.
*
* @var OutFont
*/
protected OutFont $outfont;

/**
* PDF layers.
*
Expand Down Expand Up @@ -557,13 +559,13 @@ protected function getOutPDFBody(): string
$out .= $this->graph->getOutExtGState($this->pon);
$this->pon = $this->graph->getObjectNumber();
$out .= $this->getOutOCG();
$outfont = new OutFont(
$this->outfont = new OutFont(
$this->font->getFonts(),
$this->pon,
$this->encrypt,
);
$out .= $outfont->getFontsBlock();
$this->pon = $outfont->getObjectNumber();
$out .= $this->outfont->getFontsBlock();
$this->pon = $this->outfont->getObjectNumber();
$out .= $this->image->getOutImagesBlock($this->pon);
$this->pon = $this->image->getObjectNumber();
$out .= $this->color->getPdfSpotObjects($this->pon);
Expand Down Expand Up @@ -975,6 +977,13 @@ protected function getOutAPXObjects(
$out = $oid . ' 0 obj' . "\n";
$tid = 'AX' . $oid;
$this->xobjects[$tid] = [
'spot_colors' => [],
'extgstate' => [],
'gradient' => [],
'font' => [],
'image' => [],
'xobject' => [],
'annotations' => [],
'id' => $tid,
'n' => $oid,
'x' => 0,
Expand Down Expand Up @@ -1011,12 +1020,12 @@ protected function getOutAPXObjects(
protected function getOutXObjects(): string
{
$out = '';
foreach ($this->xobject as $data) {
foreach ($this->xobjects as $data) {
if (empty($data['outdata'])) {
continue;
}

$out .= ' ' . $data['n'] . ' 0 R' . "\n"
$out .= $data['n'] . ' 0 obj' . "\n"
. '<<'
. ' /Type /XObject'
. ' /Subtype /Form'
Expand All @@ -1037,70 +1046,30 @@ protected function getOutXObjects(): string
$this->toPoints(($data['w'] + $data['x'])),
$this->toPoints(($data['h'] - $data['y']))
);
$out .= ' /Matrix [1 0 0 1 0 0] /Resources <<'
. ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';

if (empty($this->pdfa) || ($this->pdfa > 1)) {
if (!empty($data['extgstate'])) {
$out .= ' /ExtGState <<';
foreach ($data['extgstate'] as $objdic) {
$out .= $objdic;
}
$out .= ' >>';
}

if (!empty($data['pattern'])) {
$out .= ' /Pattern <<';
foreach ($data['pattern'] as $objdic) {
$out .= $objdic;
}
$out .= ' >>';
}

if (!empty($data['shading'])) {
$out .= ' /Shading <<';
foreach ($data['shading'] as $objdic) {
$out .= $objdic;
}
$out .= ' >>';
}
}

if (! empty($data['colorspace'])) {
$out .= ' /ColorSpace <<';
foreach ($data['colorspace'] as $objdic) {
$out .= $objdic;
}
$out .= ' >>';
}
$out .= ' /Matrix [1 0 0 1 0 0]'
. ' /Resources <<'
. ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';

if (! empty($data['font'])) {
$out .= ' /Font <<';
foreach ($data['font'] as $objdic) {
$out .= $objdic;
}
$out .= ' >>';
}
$out .= $this->graph->getOutExtGStateResourcesByKeys($data['extgstate']);
$out .= $this->graph->getOutGradientResourcesByKeys($data['gradient']);
$out .= $this->color->getPdfSpotResourcesByKeys($data['spot_colors']);
$out .= $this->outfont->getOutFontDictByKeys($data['font']);

if (! empty($data['image']) || ! empty($data['xobject'])) {
$out .= ' /XObject <<';
if (! empty($data['image'])) {
foreach ($data['image'] as $objdic) {
$out .= $objdic;
}
}

$out .= $this->image->getXobjectDictByKeys($data['image']);
if (! empty($data['xobject'])) {
foreach ($data['xobject'] as $objdic) {
$out .= $objdic;
foreach ($data['xobject'] as $xid) {
$out .= ' /' . $xid . ' ' . $this->xobjects[$xid]['n'] . ' 0 R';
}
}
$out .= ' >>';
}

$out .= ' >>'; // end of /Resources.

if (! empty($data['transparency'])) {
if (isset($data['transparency'])) {
// set transparency group
$out .= ' /Group << /Type /Group /S /Transparency';
if (!empty($data['transparency'])) {
Expand Down Expand Up @@ -1132,9 +1101,9 @@ protected function getOutResourcesDict(): string
return $this->objid['resdic'] . ' 0 obj' . "\n"
. '<<'
. ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]'
. $this->getOutFontDic()
. $this->getXObjectDic()
. $this->getLayerDic()
. $this->outfont->getOutFontDict()
. $this->getXObjectDict()
. $this->getLayerDict()
. $this->graph->getOutExtGStateResources()
. $this->graph->getOutGradientResources()
. $this->color->getPdfSpotResources()
Expand Down Expand Up @@ -2950,48 +2919,33 @@ protected function getOutSignatureInfo(int $oid): string
return $out;
}

/**
* Get the PDF output string for Font resources dictionary.
*/
protected function getOutFontDic(): string
{
$fonts = $this->font->getFonts();
if ($fonts === []) {
return '';
}

$out = ' /Font <<';
foreach ($fonts as $font) {
$out .= ' /F' . $font['i'] . ' ' . $font['n'] . ' 0 R';
}

return $out . ' >>';
}

/**
* Get the PDF output string for XObject resources dictionary.
*/
protected function getXObjectDic(): string
protected function getXObjectDict(): string
{
$out = ' /XObject <<';
foreach ($this->xobject as $id => $oid) {

foreach ($this->xobjects as $id => $oid) {
$out .= ' /' . $id . ' ' . $oid['n'] . ' 0 R';
}

$out .= $this->image->getXobjectDict();

return $out . ' >>';
}

/**
* Get the PDF output string for Layer resources dictionary.
*/
protected function getLayerDic(): string
protected function getLayerDict(): string
{
if (empty($this->pdflayer)) {
return '';
}

$out = ' /Properties <<';

foreach ($this->pdflayer as $layer) {
$out .= ' /' . $layer['layer'] . ' ' . $layer['objid'] . ' 0 R';
}
Expand Down
Loading

0 comments on commit a9db5a8

Please sign in to comment.