Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| HtmlPrintRendererDriver | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
| renderPdf | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Modules\Pdf\Infrastructure\Driver; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | /** |
| 12 | * Universal HTML Print Renderer Driver. |
| 13 | * |
| 14 | * Wraps HTML content in a self-contained CSS Paged Media container |
| 15 | * with instant browser print integration and high-fidelity print styles. |
| 16 | * |
| 17 | * @package App\Modules\Pdf\Infrastructure\Driver |
| 18 | */ |
| 19 | final class HtmlPrintRendererDriver implements PdfRendererDriverInterface |
| 20 | { |
| 21 | /** |
| 22 | * Renders self-contained printable HTML document. |
| 23 | * |
| 24 | * @param string $html Composed HTML content. |
| 25 | * @param array<string, mixed> $options Rendering configuration options. |
| 26 | * @return string Printable HTML markup document. |
| 27 | */ |
| 28 | public function renderPdf(string $html, array $options = []): string |
| 29 | { |
| 30 | $format = (string)($options['format'] ?? 'A4'); |
| 31 | $orientation = (string)($options['orientation'] ?? 'portrait'); |
| 32 | $marginTop = (int)($options['margin_top'] ?? 15); |
| 33 | $marginBottom = (int)($options['margin_bottom'] ?? 15); |
| 34 | $marginLeft = (int)($options['margin_left'] ?? 15); |
| 35 | $marginRight = (int)($options['margin_right'] ?? 15); |
| 36 | $headerHtml = (string)($options['header_html'] ?? ''); |
| 37 | $footerHtml = (string)($options['footer_html'] ?? ''); |
| 38 | $cssStyles = (string)($options['css_styles'] ?? ''); |
| 39 | |
| 40 | return '<!DOCTYPE html>' |
| 41 | . '<html lang="pl"><head><meta charset="UTF-8">' |
| 42 | . '<title>' . htmlspecialchars((string)($options['title'] ?? 'Dokument'), ENT_QUOTES, 'UTF-8') . '</title>' |
| 43 | . '<style>' |
| 44 | . '@page { size: ' . $format . ' ' . $orientation . '; margin: ' |
| 45 | . $marginTop . 'mm ' . $marginRight . 'mm ' . $marginBottom . 'mm ' . $marginLeft . 'mm; }' |
| 46 | . 'body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; ' |
| 47 | . 'font-size: 11pt; color: #1e293b; background: #ffffff; margin: 0; padding: 0; }' |
| 48 | . '.pdf-page-container { width: 100%; box-sizing: border-box; }' |
| 49 | . '.pdf-header-container { margin-bottom: 20px; border-bottom: 1px solid #e2e8f0; padding-bottom: 10px; }' |
| 50 | . '.pdf-footer-container { margin-top: 30px; border-top: 1px solid #e2e8f0; padding-top: 10px; ' |
| 51 | . 'font-size: 9pt; color: #64748b; }' |
| 52 | . '.pdf-page-break { page-break-after: always; break-after: page; }' |
| 53 | . '@media print { body { -webkit-print-color-adjust: exact; print-color-adjust: exact; } }' |
| 54 | . $cssStyles |
| 55 | . '</style></head><body>' |
| 56 | . '<main class="pdf-page-container">' |
| 57 | . ($headerHtml !== '' ? '<header class="pdf-header-container">' . $headerHtml . '</header>' : '') |
| 58 | . '<section class="pdf-body-content">' . $html . '</section>' |
| 59 | . ($footerHtml !== '' ? '<footer class="pdf-footer-container">' . $footerHtml . '</footer>' : '') |
| 60 | . '</main></body></html>'; |
| 61 | } |
| 62 | } |