|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace CakePdf\Pdf\Engine; |
| 5 | + |
| 6 | +use Com\Tecnick\Pdf\Tcpdf; |
| 7 | + |
| 8 | +/** |
| 9 | + * Engine for the actively maintained `tecnickcom/tc-lib-pdf` library, the |
| 10 | + * successor of the now deprecated `tecnickcom/tcpdf` package used by |
| 11 | + * {@see \CakePdf\Pdf\Engine\TcpdfEngine}. |
| 12 | + * |
| 13 | + * Unlike the old TCPDF engine, tc-lib-pdf ships no fonts. The standard PDF |
| 14 | + * core fonts (Helvetica, Times, Courier, Symbol, ZapfDingbats) and any custom |
| 15 | + * font must be available as generated `*.json` font files in a directory |
| 16 | + * pointed to by the `K_PATH_FONTS` constant. Define it once during bootstrap: |
| 17 | + * |
| 18 | + * ```php |
| 19 | + * define('K_PATH_FONTS', '/path/to/generated/fonts'); |
| 20 | + * ``` |
| 21 | + * |
| 22 | + * See the tc-lib-pdf-font documentation for how to generate font files. |
| 23 | + * |
| 24 | + * Engine specific options are read from the `options` config key: |
| 25 | + * |
| 26 | + * - `unit`: Document unit of measure. Defaults to `mm`. |
| 27 | + * - `unicode`: Whether the document is in Unicode mode. Defaults to `true`. |
| 28 | + * - `subsetFont`: Whether to subset embedded fonts. Defaults to `false`. |
| 29 | + * - `compress`: Whether to compress the PDF output. Defaults to `true`. |
| 30 | + * - `mode`: PDF conformance mode (e.g. `pdfa2b`, `pdfx4`). Defaults to `''`. |
| 31 | + * - `font`: Default font as `['family' => ..., 'style' => ..., 'size' => ...]`. |
| 32 | + * - `margins`: Fallback page margins in document units as |
| 33 | + * `['left' => ..., 'top' => ..., 'right' => ..., 'bottom' => ...]`. The |
| 34 | + * standard CakePdf `margin` setting takes precedence when set. |
| 35 | + * - `metadata`: Document metadata as |
| 36 | + * `['author' => ..., 'creator' => ..., 'subject' => ..., 'keywords' => ...]`. |
| 37 | + */ |
| 38 | +class TcLibPdfEngine extends AbstractPdfEngine |
| 39 | +{ |
| 40 | + /** |
| 41 | + * @var array<string, mixed> |
| 42 | + */ |
| 43 | + protected array $_defaultConfig = [ |
| 44 | + 'options' => [ |
| 45 | + 'unit' => 'mm', |
| 46 | + 'unicode' => true, |
| 47 | + 'subsetFont' => false, |
| 48 | + 'compress' => true, |
| 49 | + 'mode' => '', |
| 50 | + 'font' => [ |
| 51 | + 'family' => 'helvetica', |
| 52 | + 'style' => '', |
| 53 | + 'size' => 12, |
| 54 | + ], |
| 55 | + 'margins' => [ |
| 56 | + 'left' => 15, |
| 57 | + 'top' => 15, |
| 58 | + 'right' => 15, |
| 59 | + 'bottom' => 15, |
| 60 | + ], |
| 61 | + 'metadata' => [], |
| 62 | + ], |
| 63 | + ]; |
| 64 | + |
| 65 | + /** |
| 66 | + * Generates Pdf from html |
| 67 | + * |
| 68 | + * @return string raw pdf data |
| 69 | + */ |
| 70 | + public function output(): string |
| 71 | + { |
| 72 | + $tcpdf = $this->_createInstance(); |
| 73 | + |
| 74 | + $this->_applyMetadata($tcpdf); |
| 75 | + $tcpdf->enableDefaultPageContent(); |
| 76 | + |
| 77 | + $font = (array)$this->getConfig('options.font'); |
| 78 | + $defaultFont = $tcpdf->font->insert( |
| 79 | + $tcpdf->pon, |
| 80 | + (string)($font['family'] ?? 'helvetica'), |
| 81 | + (string)($font['style'] ?? ''), |
| 82 | + (int)($font['size'] ?? 12), |
| 83 | + ); |
| 84 | + |
| 85 | + $tcpdf->addPage($this->_pageOptions()); |
| 86 | + $tcpdf->page->addContent($defaultFont['out']); |
| 87 | + |
| 88 | + $region = $tcpdf->page->getRegion(); |
| 89 | + $tcpdf->addHTMLCell( |
| 90 | + html: $this->_Pdf->html(), |
| 91 | + posx: (float)$region['RX'], |
| 92 | + posy: (float)$region['RY'], |
| 93 | + width: (float)$region['RW'], |
| 94 | + ); |
| 95 | + |
| 96 | + return $tcpdf->getOutPDFString(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Creates the tc-lib-pdf instance. |
| 101 | + * |
| 102 | + * @return \Com\Tecnick\Pdf\Tcpdf |
| 103 | + */ |
| 104 | + protected function _createInstance(): Tcpdf |
| 105 | + { |
| 106 | + return new Tcpdf( |
| 107 | + unit: (string)$this->getConfig('options.unit'), |
| 108 | + isunicode: (bool)$this->getConfig('options.unicode'), |
| 109 | + subsetfont: (bool)$this->getConfig('options.subsetFont'), |
| 110 | + compress: (bool)$this->getConfig('options.compress'), |
| 111 | + mode: (string)$this->getConfig('options.mode'), |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Builds the page options array from the CakePdf instance and engine config. |
| 117 | + * |
| 118 | + * The standard CakePdf `margin` setting takes precedence; the engine |
| 119 | + * `options.margins` config is used as a fallback for any unset side. |
| 120 | + * |
| 121 | + * @return array<string, mixed> |
| 122 | + */ |
| 123 | + protected function _pageOptions(): array |
| 124 | + { |
| 125 | + $orientation = $this->_Pdf->orientation() === 'landscape' ? 'L' : 'P'; |
| 126 | + |
| 127 | + $fallback = (array)$this->getConfig('options.margins'); |
| 128 | + $margin = (array)$this->_Pdf->margin(); |
| 129 | + $left = (float)($margin['left'] ?? $fallback['left'] ?? 0); |
| 130 | + $top = (float)($margin['top'] ?? $fallback['top'] ?? 0); |
| 131 | + $right = (float)($margin['right'] ?? $fallback['right'] ?? 0); |
| 132 | + $bottom = (float)($margin['bottom'] ?? $fallback['bottom'] ?? 0); |
| 133 | + |
| 134 | + return [ |
| 135 | + 'format' => $this->_Pdf->pageSize(), |
| 136 | + 'orientation' => $orientation, |
| 137 | + 'margin' => [ |
| 138 | + 'PL' => $left, |
| 139 | + 'PR' => $right, |
| 140 | + 'PT' => $top, |
| 141 | + 'PB' => $bottom, |
| 142 | + 'CT' => $top, |
| 143 | + 'CB' => $bottom, |
| 144 | + ], |
| 145 | + ]; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Applies document metadata to the tc-lib-pdf instance. |
| 150 | + * |
| 151 | + * @param \Com\Tecnick\Pdf\Tcpdf $tcpdf The tc-lib-pdf instance. |
| 152 | + * @return void |
| 153 | + */ |
| 154 | + protected function _applyMetadata(Tcpdf $tcpdf): void |
| 155 | + { |
| 156 | + $title = $this->_Pdf->title(); |
| 157 | + if ($title !== null && $title !== '') { |
| 158 | + $tcpdf->setTitle($title); |
| 159 | + } |
| 160 | + |
| 161 | + $metadata = (array)$this->getConfig('options.metadata'); |
| 162 | + if (isset($metadata['author'])) { |
| 163 | + $tcpdf->setAuthor((string)$metadata['author']); |
| 164 | + } |
| 165 | + if (isset($metadata['creator'])) { |
| 166 | + $tcpdf->setCreator((string)$metadata['creator']); |
| 167 | + } |
| 168 | + if (isset($metadata['subject'])) { |
| 169 | + $tcpdf->setSubject((string)$metadata['subject']); |
| 170 | + } |
| 171 | + if (isset($metadata['keywords'])) { |
| 172 | + $tcpdf->setKeywords((string)$metadata['keywords']); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments