src/AppBundle/EventListener/PDFConfigListener.php line 25

Open in your IDE?
  1. <?php 
  2. namespace AppBundle\EventListener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. class PDFConfigListener implements EventSubscriberInterface
  5. {
  6.     /**
  7.      * @inheritDoc
  8.      */
  9.     public static function getSubscribedEvents()
  10.     {
  11.         return [
  12.             \Pimcore\Event\DocumentEvents::PRINT_MODIFY_PROCESSING_OPTIONS => 'modifyProcessingOptions',
  13.             \Pimcore\Event\DocumentEvents::PRINT_MODIFY_PROCESSING_CONFIG => 'modifyConfig',
  14.             \Pimcore\Event\DocumentEvents::PRINT_PRE_PDF_GENERATION => 'prePdfGeneration',
  15.             \Pimcore\Event\DocumentEvents::PRINT_POST_PDF_GENERATION  => 'postPdfGeneration'
  16.         ];
  17.     }
  18.     public function modifyProcessingOptions(\Pimcore\Event\Model\PrintConfigEvent $event) {
  19.         // https://github.com/pimcore/pimcore/blob/6f8b0925f60cfa64a4f64689617cbb6bf65f79ac/lib/Event/DocumentEvents.php#L141
  20.     }
  21.     // https://pimcore.com/docs/6.x/Development_Documentation/Best_Practice/Web2Print_Extending_Config_for_PDFX_conformance.html
  22.     // https://github.com/pimcore/pimcore/pull/1833
  23.     public function modifyConfig(\Pimcore\Event\Model\PrintConfigEvent $event) {
  24.         // https://github.com/pimcore/pimcore/blob/6f8b0925f60cfa64a4f64689617cbb6bf65f79ac/lib/Event/DocumentEvents.php#L163
  25.         // Utilizzare per impostare determinati parametri al pdf
  26.         // $event->setArgument("options", "{$event->getArgument("options")} --page-width 200mm --page-height 200mm");
  27.     }
  28.     public function prePdfGeneration(\Pimcore\Event\Model\DocumentEvent $event) {
  29.         // https://github.com/pimcore/pimcore/blob/6f8b0925f60cfa64a4f64689617cbb6bf65f79ac/lib/Event/DocumentEvents.php#L116
  30.         $_listOfDocumentID = array(
  31.             574// Single - Cartellino - Palazzetti
  32.             575// Single - Cartellino - Royal
  33.             576// Multiple - Cartellino - Palazzetti
  34.             577// Multiple - Cartellino - Royal
  35.             578// Filtered - Cartellino - Palazzetti
  36.             579  // Filtered - Cartellino - Royal
  37.         );
  38.         $_eventDocument $event->getDocument();
  39.         $_eventDocumentID $_eventDocument->getId();
  40.         // var_dump($event->getArgument("processor")->getOptions());
  41.         if (in_array($_eventDocumentID$_listOfDocumentID)) {
  42.             $_processor $event->getArgument("processor");
  43.             $_processor->setOptions("{$_processor->getOptions()} --page-width 200mm --page-height 200mm");
  44.         }
  45.     }
  46.     public function postPdfGeneration(\Pimcore\Event\Model\DocumentEvent $event) {
  47.         // https://github.com/pimcore/pimcore/blob/6f8b0925f60cfa64a4f64689617cbb6bf65f79ac/lib/Event/DocumentEvents.php#L129
  48.         $_listOfDocumentID = array(
  49.             574// Single - Cartellino - Palazzetti
  50.             575// Single - Cartellino - Royal
  51.             576// Multiple - Cartellino - Palazzetti
  52.             577// Multiple - Cartellino - Royal
  53.             578// Filtered - Cartellino - Palazzetti
  54.             579  // Filtered - Cartellino - Royal
  55.         );
  56.         $_eventDocument $event->getDocument();
  57.         $_eventDocumentID $_eventDocument->getId();
  58.         if (in_array($_eventDocumentID$_listOfDocumentID)) {
  59.             $_fileName $event->getArgument("filename");
  60.             $_tmpFileName sys_get_temp_dir() . '/' uniqid(rand(), true) . '.pdf';
  61.             // -sOutputICCProfile=/opt/bitnami/apps/pimcore/htdocs/icc_profiles/CMYK/WebCoatedSWOP2006Grade5.icc
  62.             $_output shell_exec("gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -dPDFX -sDEVICE=pdfwrite -sColorConversionStrategy=CMYK -sOutputFile={$_tmpFileName} {$_fileName}");
  63.             // var_dump($_output);
  64.             $_output shell_exec("/bin/cp -rf {$_tmpFileName} {$_fileName}");
  65.             $_output shell_exec("/bin/rm {$_tmpFileName}");
  66.             // var_dump($_output);
  67.         }
  68.     }
  69. }