| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Services; |
| 6 |
|
| 7 |
class PdfService |
| 8 |
{ |
| 9 |
public function isAvailable(): bool |
| 10 |
{ |
| 11 |
return class_exists('Dompdf\\Dompdf') || class_exists('Mpdf\\Mpdf'); |
| 12 |
} |
| 13 |
|
| 14 |
public function renderHtmlToPdf(string $html, array $options = []): string |
| 15 |
{ |
| 16 |
$paper = (string) ($options['paper'] ?? 'A4'); |
| 17 |
$orientation = (string) ($options['orientation'] ?? 'portrait'); |
| 18 |
$defaultFont = (string) ($options['default_font'] ?? 'DejaVu Sans'); |
| 19 |
|
| 20 |
if (class_exists('Dompdf\\Dompdf')) { |
| 21 |
$optionsClass = 'Dompdf\\Options'; |
| 22 |
$dompdfClass = 'Dompdf\\Dompdf'; |
| 23 |
|
| 24 |
$dompdfOptions = class_exists($optionsClass) ? new $optionsClass() : null; |
| 25 |
if ($dompdfOptions) { |
| 26 |
$dompdfOptions->set('isRemoteEnabled', true); |
| 27 |
$dompdfOptions->set('isHtml5ParserEnabled', true); |
| 28 |
$dompdfOptions->set('defaultFont', $defaultFont); |
| 29 |
} |
| 30 |
|
| 31 |
$dompdf = $dompdfOptions ? new $dompdfClass($dompdfOptions) : new $dompdfClass(); |
| 32 |
$dompdf->loadHtml($html, 'UTF-8'); |
| 33 |
$dompdf->setPaper($paper, $orientation); |
| 34 |
$dompdf->render(); |
| 35 |
return (string) $dompdf->output(); |
| 36 |
} |
| 37 |
|
| 38 |
if (class_exists('Mpdf\\Mpdf')) { |
| 39 |
$mpdfClass = 'Mpdf\\Mpdf'; |
| 40 |
$mpdf = new $mpdfClass(['format' => $paper]); |
| 41 |
$mpdf->WriteHTML($html); |
| 42 |
return (string) $mpdf->Output('', 'S'); |
| 43 |
} |
| 44 |
|
| 45 |
throw new \RuntimeException('PDF engine is not available'); |
| 46 |
} |
| 47 |
|
| 48 |
public function renderHtmlToPdfSafely(string $html, array $options = []): string |
| 49 |
{ |
| 50 |
$originalErrorReporting = error_reporting(); |
| 51 |
$originalDisplayErrors = ini_get('display_errors'); |
| 52 |
$originalHtmlErrors = ini_get('html_errors'); |
| 53 |
$startObLevel = ob_get_level(); |
| 54 |
|
| 55 |
ini_set('display_errors', '0'); |
| 56 |
ini_set('html_errors', '0'); |
| 57 |
error_reporting($originalErrorReporting & ~E_DEPRECATED & ~E_USER_DEPRECATED); |
| 58 |
ob_start(); |
| 59 |
|
| 60 |
$previousErrorHandler = set_error_handler(static function (int $errno) { |
| 61 |
if ($errno === E_DEPRECATED || $errno === E_USER_DEPRECATED) { |
| 62 |
return true; |
| 63 |
} |
| 64 |
return false; |
| 65 |
}); |
| 66 |
|
| 67 |
try { |
| 68 |
return $this->renderHtmlToPdf($html, $options); |
| 69 |
} finally { |
| 70 |
if ($previousErrorHandler !== null) { |
| 71 |
restore_error_handler(); |
| 72 |
} |
| 73 |
|
| 74 |
while (ob_get_level() > $startObLevel) { |
| 75 |
ob_end_clean(); |
| 76 |
} |
| 77 |
|
| 78 |
error_reporting($originalErrorReporting); |
| 79 |
if ($originalDisplayErrors !== false) { |
| 80 |
ini_set('display_errors', (string) $originalDisplayErrors); |
| 81 |
} |
| 82 |
if ($originalHtmlErrors !== false) { |
| 83 |
ini_set('html_errors', (string) $originalHtmlErrors); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
public function renderTemplate(string $templatePath, array $data = []): string |
| 89 |
{ |
| 90 |
$templateFile = YATRA_PLUGIN_PATH . 'templates/' . $templatePath; |
| 91 |
|
| 92 |
if (!file_exists($templateFile)) { |
| 93 |
throw new \InvalidArgumentException("Template file not found: {$templateFile}"); |
| 94 |
} |
| 95 |
|
| 96 |
// Extract data variables for template |
| 97 |
extract($data, EXTR_OVERWRITE); |
| 98 |
|
| 99 |
// Capture output |
| 100 |
ob_start(); |
| 101 |
try { |
| 102 |
include $templateFile; |
| 103 |
return ob_get_clean(); |
| 104 |
} catch (\Throwable $e) { |
| 105 |
ob_end_clean(); |
| 106 |
throw $e; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public function renderTemplateToPdf(string $templatePath, array $data = [], array $options = []): string |
| 111 |
{ |
| 112 |
$html = $this->renderTemplate($templatePath, $data); |
| 113 |
return $this->renderHtmlToPdf($html, $options); |
| 114 |
} |
| 115 |
|
| 116 |
public function renderTemplateToPdfSafely(string $templatePath, array $data = [], array $options = []): string |
| 117 |
{ |
| 118 |
$html = $this->renderTemplate($templatePath, $data); |
| 119 |
return $this->renderHtmlToPdfSafely($html, $options); |
| 120 |
} |
| 121 |
|
| 122 |
public function outputPdfDownload(string $pdfBinary, string $filename, bool $inline = false): void |
| 123 |
{ |
| 124 |
header('Content-Type: application/pdf'); |
| 125 |
header('Content-Disposition: ' . ($inline ? 'inline' : 'attachment') . '; filename="' . $filename . '"'); |
| 126 |
header('Cache-Control: no-cache, no-store, must-revalidate'); |
| 127 |
header('Pragma: no-cache'); |
| 128 |
header('Expires: 0'); |
| 129 |
|
| 130 |
echo $pdfBinary; |
| 131 |
exit; |
| 132 |
} |
| 133 |
} |
| 134 |
|