Commit 6cc9d4d2 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Ajout de la génération de pdf dans service

parent 85f219b3
Pipeline #15087 passed with stage
in 16 seconds
......@@ -2,11 +2,11 @@
namespace UnicaenRenderer;
use Zend\Config\Factory as ConfigFactory;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\Glob;
use Zend\Config\Factory as ConfigFactory;
class Module
{
......@@ -16,17 +16,6 @@ class Module
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
/* Active un layout spécial si la requête est de type AJAX. Valable pour TOUS les modules de l'application. */
$eventManager->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch',
function (MvcEvent $e) {
$request = $e->getRequest();
if ($request instanceof HttpRequest && $request->isXmlHttpRequest()) {
$e->getTarget()->layout('layout/ajax.phtml');
}
}
);
}
public function getConfig()
......
......@@ -6,8 +6,10 @@ use DateTime;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\QueryBuilder;
use Mpdf\MpdfException;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\EntityManagerAwareTrait;
use UnicaenPdf\Exporter\PdfExporter;
use UnicaenRenderer\Entity\Db\Rendu;
use UnicaenRenderer\Entity\Db\Template;
use UnicaenRenderer\Service\Template\TemplateServiceAwareTrait;
......@@ -17,6 +19,8 @@ class RenduService {
use EntityManagerAwareTrait;
use TemplateServiceAwareTrait;
public $renderer;
/** Gestion des entités *******************************************************************************************/
/**
......@@ -155,4 +159,24 @@ class RenduService {
$rendu->setCorps($this->getTemplateService()->replaceMacros($rendu->getCorps(), $variables));
return $rendu;
}
/**
* @param string $sujet
* @param string $corps
* @return string
*/
public function generate(string $sujet, string $corps) : string
{
try {
$exporter = new PdfExporter();
$exporter->setRenderer($this->renderer);
$exporter->getMpdf()->SetTitle($sujet);
$exporter->setHeaderScript('/unicaen-renderer/pdf/header');
$exporter->setFooterScript('/unicaen-renderer/pdf/footer');
$exporter->addBodyHtml($corps);
return $exporter->export($sujet);
} catch (MpdfException $e) {
throw new RuntimeException("Un problème est survenue lors de la génértion du PDF", null, $e);
}
}
}
......@@ -5,6 +5,7 @@ namespace UnicaenRenderer\Service\Rendu;
use Doctrine\ORM\EntityManager;
use Interop\Container\ContainerInterface;
use UnicaenRenderer\Service\Template\TemplateService;
use Zend\View\Renderer\PhpRenderer;
class RenduServiceFactory {
......@@ -24,6 +25,11 @@ class RenduServiceFactory {
$service = new RenduService();
$service->setEntityManager($entityManager);
$service->setTemplateService($templateService);
/* @var PhpRenderer $renderer */
$renderer = $container->get('ViewRenderer');
$service->renderer = $renderer;
return $service;
}
}
\ No newline at end of file
<table style="border: none">
<tr>
<td style="border: none">
Généré par EMC2 le <?php echo (new DateTime())->format('d/m/Y à H:i'); ?>
</td>
<td style="border: none">
{PAGENO}/{nbpg}
</td>
<td style="border: none">
<?php echo $denomination; ?>
</td>
</tr>
</table>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment