Bladeren bron

licence export with dompdf ok

Olivier Massot 1 jaar geleden
bovenliggende
commit
a84696e7af

+ 7 - 5
config/services.yaml

@@ -21,8 +21,10 @@ services:
             $persistProcessor: '@api_platform.doctrine.orm.state.persist_processor'
             $removeProcessor: '@api_platform.doctrine.orm.state.remove_processor'
             $opentalentNoReplyEmailAddress: 'noreply@opentalent.fr'
-            $legacyBaseUrl: '%env(PUBLIC_API_LEG_BASE_URL)%'
-            $baseUrl: '%env(PUBLIC_API_BASE_URL)%'
+            $legacyBaseUrl: '%env(API_LEG_BASE_URL)%'
+            $publicLegacyBaseUrl: '%env(PUBLIC_API_LEG_BASE_URL)%'
+            $baseUrl: '%env(API_BASE_URL)%'
+            $publicBaseUrl: '%env(PUBLIC_API_BASE_URL)%'
 
     # makes classes in src/ available to be used as services
     # this creates a service per class whose id is the fully-qualified class name
@@ -51,9 +53,9 @@ services:
     Phpdocx\Create\CreateDocx:
         class: Phpdocx\Create\CreateDocx
 
-    App\Service\Export\Encoder\PdfEncoder:
-        arguments:
-            $phpDocx: '@Phpdocx\Create\CreateDocx'
+#    App\Service\Export\Encoder\PdfEncoder:
+#        arguments:
+#            $phpDocx: '@Phpdocx\Create\CreateDocx'
 
     App\Service\Organization\Utils:
         public: true

+ 49 - 41
src/Service/Export/Encoder/PdfEncoder.php

@@ -8,16 +8,13 @@ use App\Enum\Export\ExportFormatEnum;
 use App\Service\Utils\Path;
 use Dompdf\Dompdf;
 use Dompdf\Options;
-use Phpdocx\Create\CreateDocx;
 
 /**
  * Encode HTML to PDF.
  */
 class PdfEncoder implements EncoderInterface
 {
-    public function __construct(
-        private readonly CreateDocx $phpDocx
-    ) {}
+    public function __construct() {}
 
     public function support(string $format): bool
     {
@@ -26,49 +23,60 @@ class PdfEncoder implements EncoderInterface
 
     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->setDefaultFont('DejaVu Sans Bold');
         $options->setIsRemoteEnabled(true);
         $options->setChroot(Path::getProjectDir() . '/public');
+        $options->setIsRemoteEnabled(true);
 
         $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;
+
+//        $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;
     }
 }

+ 6 - 2
src/Service/File/Storage/ApiLegacyStorage.php

@@ -19,7 +19,8 @@ class ApiLegacyStorage implements FileStorageInterface
         private readonly ApiLegacyRequestService $apiLegacyRequestService,
         protected readonly DataManager $dataManager,
         protected readonly UrlBuilder $urlBuilder,
-        protected readonly string $legacyBaseUrl
+        protected readonly string $legacyBaseUrl,
+        protected readonly string $publicLegacyBaseUrl
     ) {
     }
 
@@ -45,7 +46,10 @@ class ApiLegacyStorage implements FileStorageInterface
     {
         $url = sprintf('api/files/%s/download/%s?relativePath=1', $file->getId(), $size);
 
-        return UrlBuilder::concat($this->legacyBaseUrl, [$this->apiLegacyRequestService->getContent($url)], []);
+        // L'url interne est l'équivalent d'un chemin relatif dans ce cas
+        $baseUrl = $relativePath ? $this->legacyBaseUrl : $this->publicLegacyBaseUrl;
+
+        return UrlBuilder::concat($baseUrl, [$this->apiLegacyRequestService->getContent($url)], []);
     }
 
     public function support(File $file): bool

+ 1 - 1
src/Service/Twig/AssetsExtension.php

@@ -57,6 +57,6 @@ class AssetsExtension extends AbstractExtension
      */
     public function fileImagePath(File $file, string $size): string
     {
-        return $this->fileManager->getImageUrl($file, $size, true);
+        return ltrim($this->fileManager->getImageUrl($file, $size, true), '/');
     }
 }

+ 3 - 2
src/Service/Utils/UrlBuilder.php

@@ -94,9 +94,10 @@ class UrlBuilder
     /**
      * Retourne l'URL relative sans le scheme et l'host.
      */
-    public function getRelativeUrl(string $path): string
+    public function getRelativeUrl(string $url): string
     {
-        return UrlGenerator::getRelativePath($this->baseUrl, $path);
+        $parts = parse_url($url);
+        return ($parts['path'] ?? '') . (isset($parts['query']) ? '?'.$parts['query'] : '');
     }
 
     /**

+ 23 - 21
templates/export/licence_cmf.html.twig

@@ -6,16 +6,14 @@
 
         {% block style %}
         html {
-            width: 21cm;
-            height: 100%;
+            word-wrap: break-word;
         }
 
         @page {
-            margin: 180px 50px;
+            margin: 130px 50px 30px 50px;
         }
 
         body {
-            height: 100%;
         }
 
         .Style1 {
@@ -70,8 +68,8 @@
 
         #year_head {
             position: absolute;
-            bottom: 10px;
-            left: 100px;
+            top: 30px;
+            left: 90px;
             color: #9d1348;
             font-size: 25px;
             font-weight: bold;
@@ -84,7 +82,7 @@
 
         #year_card {
             position: absolute;
-            bottom: 75px;
+            bottom: 94px;
             left: 55px;
             color: #9d1348;
             font-size: 14px;
@@ -127,7 +125,7 @@
             z-index: 2;
         }
 
-        #scissor{
+        #scissor {
             position: absolute;
             top: 0;
             left: 15px;
@@ -158,7 +156,6 @@
             integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN"
             crossorigin="anonymous"
     />
-
     <title>Licence CMF</title>
 </head>
 
@@ -166,19 +163,21 @@
 {% block content %}
     {% for licence in model.licences %}
         <page data-iri="{{ licence.id }}">
-            <table width="793" border="0" cellspacing="0" cellpadding="0">
+            <table width="500" border="0" cellspacing="0" cellpadding="0">
                 <tbody>
                 <tr>
-                    <td width="693">
-                        <table width="680" border="0" align="center" cellpadding="0" cellspacing="0">
+                    <td width="500">
+                        <table width="500" align="center" border="0" cellpadding="0" cellspacing="0">
                             <tbody>
                             <tr>
-                                <td width="340" class="relative">
+                                <td width="250" class="relative">
                                     <img src="{{ 'images/cmf_licence.png' }}"
                                             width="170" height="86"/>
-                                    <span id="year_head">{{ licence.year }}</span>
+                                    <span id="year_head">
+                                        {{ licence.year }}
+                                    </span>
                                 </td>
-                                <td width="340">
+                                <td width="250">
                                     <div align="right">
                                         <img src="{{ 'images/cmf-reseau.png' }}"
                                                 width="200" height="86"/>
@@ -234,7 +233,7 @@
                     </tr>
                     <tr class="up">
                         {% if licence.isOrganizationLicence %}
-                            <td width="80" id="avatar">
+                            <td height="82" width="80" id="avatar">
                                 <div align="center">
                                     {% if(licence.logo is null) %}
                                         <img src="{{ 'images/picto_face.png' }}"
@@ -257,7 +256,7 @@
                                 </span>
                             </td>
                         {% else %}
-                            <td width="80" id="avatar">
+                            <td height="26" width="80" id="avatar">
                                 <div align="center">
                                     {% if(licence.personAvatar is null) %}
                                         <img
@@ -283,7 +282,7 @@
                     </tr>
 
                     <tr class="bottom">
-                        <td width="70" valign="middle"
+                        <td height="45" width="70" valign="middle"
                             style="vertical-align: top;">
                             <div align="center">
                                 <img src="{{ 'images/cmf_licence.png' }}"
@@ -299,7 +298,7 @@
                                 <p> ou flashez ce code</p></span>
                             </div>
                         </td>
-                        <td width="70" align="right" valign="middle" id="qrCode">
+                        <td height="65" width="70" align="right" valign="middle" id="qrCode">
                             {% if(licence.qrCode) %}
                                 <img style="margin-right: 10px;"
                                      src="{{ fileImagePath(licence.qrCode, 'sm') }}"
@@ -310,7 +309,8 @@
                     </tr>
 
                     <tr>
-                        <td colspan="3" align="center" bgcolor="{{ licence.color }}"><span class="Style3">CMF ● cmf@cmf-musique.org ● 01 55 58 22 82 ● www.cmf-musique.org</span>
+                        <td height="26" colspan="3" align="center" bgcolor="{{ licence.color }}">
+                            <span class="Style3">CMF ● cmf@cmf-musique.org ● 01 55 58 22 82 ● www.cmf-musique.org</span>
                         </td>
                     </tr>
 
@@ -322,7 +322,7 @@
             </div>
 
 
-            <table width="793" border="0" cellspacing="0" cellpadding="0">
+            <table width="500" border="0" cellspacing="0" cellpadding="0">
                 <tbody>
                 <tr>
                     <td></td>
@@ -338,7 +338,9 @@
                 </tr>
                 </tbody>
             </table>
+            {% if model.licences|length > 1 %}
             <div class="page_break"></div>
+            {% endif %}
         </page>
     {% endfor %}
 {% endblock content %}