<?php
namespace AppBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PDFConfigListener implements EventSubscriberInterface
{
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
\Pimcore\Event\DocumentEvents::PRINT_MODIFY_PROCESSING_OPTIONS => 'modifyProcessingOptions',
\Pimcore\Event\DocumentEvents::PRINT_MODIFY_PROCESSING_CONFIG => 'modifyConfig',
\Pimcore\Event\DocumentEvents::PRINT_PRE_PDF_GENERATION => 'prePdfGeneration',
\Pimcore\Event\DocumentEvents::PRINT_POST_PDF_GENERATION => 'postPdfGeneration'
];
}
public function modifyProcessingOptions(\Pimcore\Event\Model\PrintConfigEvent $event) {
// https://github.com/pimcore/pimcore/blob/6f8b0925f60cfa64a4f64689617cbb6bf65f79ac/lib/Event/DocumentEvents.php#L141
}
// https://pimcore.com/docs/6.x/Development_Documentation/Best_Practice/Web2Print_Extending_Config_for_PDFX_conformance.html
// https://github.com/pimcore/pimcore/pull/1833
public function modifyConfig(\Pimcore\Event\Model\PrintConfigEvent $event) {
// https://github.com/pimcore/pimcore/blob/6f8b0925f60cfa64a4f64689617cbb6bf65f79ac/lib/Event/DocumentEvents.php#L163
// Utilizzare per impostare determinati parametri al pdf
// $event->setArgument("options", "{$event->getArgument("options")} --page-width 200mm --page-height 200mm");
}
public function prePdfGeneration(\Pimcore\Event\Model\DocumentEvent $event) {
// https://github.com/pimcore/pimcore/blob/6f8b0925f60cfa64a4f64689617cbb6bf65f79ac/lib/Event/DocumentEvents.php#L116
$_listOfDocumentID = array(
574, // Single - Cartellino - Palazzetti
575, // Single - Cartellino - Royal
576, // Multiple - Cartellino - Palazzetti
577, // Multiple - Cartellino - Royal
578, // Filtered - Cartellino - Palazzetti
579 // Filtered - Cartellino - Royal
);
$_eventDocument = $event->getDocument();
$_eventDocumentID = $_eventDocument->getId();
// var_dump($event->getArgument("processor")->getOptions());
if (in_array($_eventDocumentID, $_listOfDocumentID)) {
$_processor = $event->getArgument("processor");
$_processor->setOptions("{$_processor->getOptions()} --page-width 200mm --page-height 200mm");
}
}
public function postPdfGeneration(\Pimcore\Event\Model\DocumentEvent $event) {
// https://github.com/pimcore/pimcore/blob/6f8b0925f60cfa64a4f64689617cbb6bf65f79ac/lib/Event/DocumentEvents.php#L129
$_listOfDocumentID = array(
574, // Single - Cartellino - Palazzetti
575, // Single - Cartellino - Royal
576, // Multiple - Cartellino - Palazzetti
577, // Multiple - Cartellino - Royal
578, // Filtered - Cartellino - Palazzetti
579 // Filtered - Cartellino - Royal
);
$_eventDocument = $event->getDocument();
$_eventDocumentID = $_eventDocument->getId();
if (in_array($_eventDocumentID, $_listOfDocumentID)) {
$_fileName = $event->getArgument("filename");
$_tmpFileName = sys_get_temp_dir() . '/' . uniqid(rand(), true) . '.pdf';
// -sOutputICCProfile=/opt/bitnami/apps/pimcore/htdocs/icc_profiles/CMYK/WebCoatedSWOP2006Grade5.icc
$_output = shell_exec("gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -dPDFX -sDEVICE=pdfwrite -sColorConversionStrategy=CMYK -sOutputFile={$_tmpFileName} {$_fileName}");
// var_dump($_output);
$_output = shell_exec("/bin/cp -rf {$_tmpFileName} {$_fileName}");
$_output = shell_exec("/bin/rm {$_tmpFileName}");
// var_dump($_output);
}
}
}