|
@@ -21,62 +21,28 @@ class PdfEncoder implements EncoderInterface
|
|
|
return $format === ExportFormatEnum::PDF->value;
|
|
return $format === ExportFormatEnum::PDF->value;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Converts the provided HTML content into a PDF document.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $html The HTML content to be converted to PDF.
|
|
|
|
|
+ * @param array<string, mixed> $options Optional configuration settings for the PDF generation
|
|
|
|
|
+ * @see https://github.com/dompdf/dompdf/blob/master/src/Options.php
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return string The generated PDF content as a string.
|
|
|
|
|
+ */
|
|
|
public function encode(string $html, array $options = []): string
|
|
public function encode(string $html, array $options = []): string
|
|
|
{
|
|
{
|
|
|
- $options = new Options();
|
|
|
|
|
- $options->setIsRemoteEnabled(true);
|
|
|
|
|
- $options->setChroot(Path::getProjectDir() . '/public');
|
|
|
|
|
- $options->setIsRemoteEnabled(true);
|
|
|
|
|
-
|
|
|
|
|
- $dompdf = new Dompdf($options);
|
|
|
|
|
- $dompdf->setPaper('A4', 'portrait');
|
|
|
|
|
|
|
+ $defaultOptions = new Options();
|
|
|
|
|
+ $defaultOptions->setIsRemoteEnabled(true);
|
|
|
|
|
+ $defaultOptions->setChroot(Path::getProjectDir() . '/public');
|
|
|
|
|
+ $defaultOptions->setIsRemoteEnabled(true);
|
|
|
|
|
+ $defaultOptions->setDefaultPaperOrientation('portrait');
|
|
|
|
|
+ $defaultOptions->setDefaultPaperSize('A4');
|
|
|
|
|
+ $defaultOptions->set($options);
|
|
|
|
|
+
|
|
|
|
|
+ $dompdf = new Dompdf($defaultOptions);
|
|
|
$dompdf->loadHtml($html);
|
|
$dompdf->loadHtml($html);
|
|
|
$dompdf->render();
|
|
$dompdf->render();
|
|
|
- $pdfContent = $dompdf->output();
|
|
|
|
|
-
|
|
|
|
|
- return $pdfContent;
|
|
|
|
|
-
|
|
|
|
|
-// $tempDir = Path::getProjectDir() . '/var/tmp';
|
|
|
|
|
-// if (!is_dir($tempDir)) {
|
|
|
|
|
-// mkdir($tempDir);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// $tempFileName = $tempDir . '/' . uniqid();
|
|
|
|
|
-// $pfdFileName = $tempFileName . '.pdf';
|
|
|
|
|
-// $docxFileName = $tempFileName . '.docx';
|
|
|
|
|
-//
|
|
|
|
|
-// // @see https://www.phpdocx.com/documentation/introduction/html-to-word-PHP#
|
|
|
|
|
-// $this->phpDocx->modifyPageLayout('A4', ['marginRight' => 800, 'marginLeft' => 800]);
|
|
|
|
|
-// $this->phpDocx->embedHTML($html);
|
|
|
|
|
-// $this->phpDocx->createDocx($docxFileName);
|
|
|
|
|
-//
|
|
|
|
|
-// die;
|
|
|
|
|
-// $options = new Options();
|
|
|
|
|
-// $options->setIsRemoteEnabled(true);
|
|
|
|
|
-// $options->setChroot(Path::getProjectDir() . '/public');
|
|
|
|
|
-//
|
|
|
|
|
-// $dompdf = new Dompdf($options);
|
|
|
|
|
-//
|
|
|
|
|
-// // @see https://www.phpdocx.com/documentation/introduction/pdf-conversion-plugin-installation-guide
|
|
|
|
|
-// $this->phpDocx->transformDocument(
|
|
|
|
|
-// $docxFileName,
|
|
|
|
|
-// $pfdFileName,
|
|
|
|
|
-// 'native',
|
|
|
|
|
-// ['dompdf' => $dompdf]
|
|
|
|
|
-// );
|
|
|
|
|
-//
|
|
|
|
|
-// $pdfContent = file_get_contents($pfdFileName);
|
|
|
|
|
-// if ($pdfContent === false) {
|
|
|
|
|
-// throw new \RuntimeException('Pdf file can not be read.');
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-//// if (!unlink($docxFileName)) {
|
|
|
|
|
-//// throw new \RuntimeException('Temp file can not be deleted.');
|
|
|
|
|
-//// }
|
|
|
|
|
-// if (!unlink($pfdFileName)) {
|
|
|
|
|
-// throw new \RuntimeException('Temp file can not be deleted.');
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// return $pdfContent;
|
|
|
|
|
|
|
+ return $dompdf->output();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|