mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 15:06:06 +00:00
Pdf service
This commit is contained in:
@@ -527,7 +527,10 @@ class Util
|
||||
return uniqid() . substr(md5(rand()), 0, 4);
|
||||
}
|
||||
|
||||
public static function sanitizeFileName($fileName)
|
||||
{
|
||||
return preg_replace("/([^\w\s\d\-_~,;:\[\]\(\).])/u", '_', $fileName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
|
||||
@@ -32,8 +32,6 @@ namespace Espo\EntryPoints;
|
||||
use \Espo\Core\Exceptions\NotFound;
|
||||
use \Espo\Core\Exceptions\Forbidden;
|
||||
use \Espo\Core\Exceptions\BadRequest;
|
||||
use \Espo\Core\Htmlizer\Htmlizer;
|
||||
|
||||
|
||||
class Pdf extends \Espo\Core\EntryPoints\Base
|
||||
{
|
||||
@@ -52,49 +50,11 @@ class Pdf extends \Espo\Core\EntryPoints\Base
|
||||
$entity = $this->getEntityManager()->getEntity($entityType, $entityId);
|
||||
$template = $this->getEntityManager()->getEntity('Template', $templateId);
|
||||
|
||||
$this->getContainer()->get('serviceFactory')->create($entityType)->loadAdditionalFields($entity);
|
||||
|
||||
if (!$entity || !$template) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
if ($template->get('entityType') !== $entityType) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
if (!$this->getAcl()->check($entity, 'read') || !$this->getAcl()->check($template, 'read')) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
$fileName = $entity->get('name') . '.pdf';
|
||||
|
||||
$htmlizer = new Htmlizer($this->getFileManager(), $this->getDateTime(), $this->getNumber());
|
||||
|
||||
$pdf = new \Espo\Core\Pdf\Tcpdf();
|
||||
$pdf->setFont('freesans', '', 12, '', true);
|
||||
$pdf->setPrintHeader(false);
|
||||
|
||||
$pdf->setAutoPageBreak(true, $template->get('bottomMargin'));
|
||||
$pdf->setMargins($template->get('leftMargin'), $template->get('topMargin'), $template->get('rightMargin'));
|
||||
|
||||
|
||||
if ($template->get('printFooter')) {
|
||||
$htmlFooter = $htmlizer->render($entity, $template->get('footer'));
|
||||
$pdf->setFooterPosition($template->get('footerPosition'));
|
||||
$pdf->setFooterHtml($htmlFooter);
|
||||
} else {
|
||||
$pdf->setPrintFooter(false);
|
||||
}
|
||||
|
||||
$pdf->addPage();
|
||||
|
||||
$htmlHeader = $htmlizer->render($entity, $template->get('header'));
|
||||
$pdf->writeHTML($htmlHeader, true, false, true, false, '');
|
||||
|
||||
$htmlBody = $htmlizer->render($entity, $template->get('body'));
|
||||
$pdf->writeHTML($htmlBody, true, false, true, false, '');
|
||||
|
||||
$pdf->output($fileName);
|
||||
$this->getContainer()->get('serviceFactory')->create('Pdf')->buildFromTemplate($entity, $template, true);
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
123
application/Espo/Services/Pdf.php
Normal file
123
application/Espo/Services/Pdf.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
|
||||
* Website: http://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Services;
|
||||
|
||||
use \Espo\Core\Exceptions\Forbidden;
|
||||
use \Espo\Core\Exceptions\NotFound;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
use \Espo\Core\Htmlizer\Htmlizer;
|
||||
|
||||
class Pdf extends \Espo\Core\Services\Base
|
||||
{
|
||||
|
||||
protected $fontFace = 'freesans';
|
||||
|
||||
protected $fontSize = 12;
|
||||
|
||||
|
||||
protected function init()
|
||||
{
|
||||
$this->addDependency('fileManager');
|
||||
$this->addDependency('acl');
|
||||
$this->addDependency('metadata');
|
||||
$this->addDependency('serviceFactory');
|
||||
}
|
||||
|
||||
protected function getAcl()
|
||||
{
|
||||
return $this->getInjection('acl');
|
||||
}
|
||||
|
||||
protected function getMetadata()
|
||||
{
|
||||
return $this->getInjection('metadata');
|
||||
}
|
||||
|
||||
protected function getServiceFactory()
|
||||
{
|
||||
return $this->getInjection('serviceFactory');
|
||||
}
|
||||
|
||||
public function buildFromTemplate(Entity $entity, Entity $template, $displayInline = false)
|
||||
{
|
||||
$entityType = $entity->getEntityType();
|
||||
|
||||
$this->getServiceFactory()->create($entityType)->loadAdditionalFields($entity);
|
||||
|
||||
if ($template->get('entityType') !== $entityType) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
if (!$this->getAcl()->check($entity, 'read') || !$this->getAcl()->check($template, 'read')) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
$htmlizer = new Htmlizer($this->getFileManager(), $this->getDateTime(), $this->getNumber());
|
||||
|
||||
$pdf = new \Espo\Core\Pdf\Tcpdf();
|
||||
|
||||
$pdf->setFont($this->fontFace, '', $this->fontSize, '', true);
|
||||
$pdf->setPrintHeader(false);
|
||||
|
||||
$pdf->setAutoPageBreak(true, $template->get('bottomMargin'));
|
||||
$pdf->setMargins($template->get('leftMargin'), $template->get('topMargin'), $template->get('rightMargin'));
|
||||
|
||||
|
||||
if ($template->get('printFooter')) {
|
||||
$htmlFooter = $htmlizer->render($entity, $template->get('footer'));
|
||||
$pdf->setFooterPosition($template->get('footerPosition'));
|
||||
$pdf->setFooterHtml($htmlFooter);
|
||||
} else {
|
||||
$pdf->setPrintFooter(false);
|
||||
}
|
||||
|
||||
$pdf->addPage();
|
||||
|
||||
$htmlHeader = $htmlizer->render($entity, $template->get('header'));
|
||||
$pdf->writeHTML($htmlHeader, true, false, true, false, '');
|
||||
|
||||
$htmlBody = $htmlizer->render($entity, $template->get('body'));
|
||||
$pdf->writeHTML($htmlBody, true, false, true, false, '');
|
||||
|
||||
if ($displayInline) {
|
||||
$name = $entity->get('name');
|
||||
$name = \Espo\Core\Utils::sanitizeFileName($name);
|
||||
$fileName = $name . '.pdf';
|
||||
|
||||
$pdf->output($fileName, 'F');
|
||||
return;
|
||||
}
|
||||
|
||||
return $pdf->output('', 'S');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user