|
@@ -8,16 +8,13 @@ use App\Enum\Export\ExportFormatEnum;
|
|
|
use App\Service\Utils\Path;
|
|
use App\Service\Utils\Path;
|
|
|
use Dompdf\Dompdf;
|
|
use Dompdf\Dompdf;
|
|
|
use Dompdf\Options;
|
|
use Dompdf\Options;
|
|
|
-use Phpdocx\Create\CreateDocx;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Encode HTML to PDF.
|
|
* Encode HTML to PDF.
|
|
|
*/
|
|
*/
|
|
|
class PdfEncoder implements EncoderInterface
|
|
class PdfEncoder implements EncoderInterface
|
|
|
{
|
|
{
|
|
|
- public function __construct(
|
|
|
|
|
- private readonly CreateDocx $phpDocx
|
|
|
|
|
- ) {}
|
|
|
|
|
|
|
+ public function __construct() {}
|
|
|
|
|
|
|
|
public function support(string $format): bool
|
|
public function support(string $format): bool
|
|
|
{
|
|
{
|
|
@@ -26,49 +23,60 @@ class PdfEncoder implements EncoderInterface
|
|
|
|
|
|
|
|
public function encode(string $html, array $options = []): string
|
|
public function encode(string $html, array $options = []): string
|
|
|
{
|
|
{
|
|
|
-// file_put_contents(Path::getProjectDir() . '/content.tmp.html', $html);
|
|
|
|
|
-// die;
|
|
|
|
|
- $tempDir = Path::getProjectDir() . '/var/tmp';
|
|
|
|
|
- if (!is_dir($tempDir)) {
|
|
|
|
|
- mkdir($tempDir);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $tempFileName = $tempDir . '/' . uniqid();
|
|
|
|
|
-
|
|
|
|
|
- $docxFileName = $tempFileName . '.docx';
|
|
|
|
|
- $pfdFileName = $tempFileName . '.pdf';
|
|
|
|
|
-
|
|
|
|
|
- // @see https://www.phpdocx.com/documentation/introduction/html-to-word-PHP#
|
|
|
|
|
- $this->phpDocx->embedHTML($html);
|
|
|
|
|
- $this->phpDocx->createDocx($docxFileName);
|
|
|
|
|
-
|
|
|
|
|
$options = new Options();
|
|
$options = new Options();
|
|
|
- $options->setDefaultFont('DejaVu Sans Bold');
|
|
|
|
|
$options->setIsRemoteEnabled(true);
|
|
$options->setIsRemoteEnabled(true);
|
|
|
$options->setChroot(Path::getProjectDir() . '/public');
|
|
$options->setChroot(Path::getProjectDir() . '/public');
|
|
|
|
|
+ $options->setIsRemoteEnabled(true);
|
|
|
|
|
|
|
|
$dompdf = new Dompdf($options);
|
|
$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.');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $dompdf->setPaper('A4', 'portrait');
|
|
|
|
|
+ $dompdf->loadHtml($html);
|
|
|
|
|
+ $dompdf->render();
|
|
|
|
|
+ $pdfContent = $dompdf->output();
|
|
|
|
|
|
|
|
return $pdfContent;
|
|
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;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|