No announcement yet.

Workaround to TCPDF css limitations

  • Filter
  • Time
  • Show
Clear All
new posts

  • Workaround to TCPDF css limitations

    I am using entry point "pdf" to generate a a "lease" document based on a record view of my custom "Tenancy" entity but I would like to be able to use the full css capabilities for positioning, fonts, background images, etc instead of being very limited by TCPDF css capabilities.

    One possible workaround I believe would be to output the raw HTML to a new window and use the built in "save as PDF" browser capability to create a PDF document. Is this possible and if so how would I go to implement in espoCRM ?

    Any help will be highly appreciated, I am not an experienced developer.

  • #2
    You can develop own action button, where you can build document body and open new window with this body


    • #3
      Thank you so much for your reply Tanya.

      I understand how to create and action button in detail view and I also understand (thanks to the information in this forum) how to call for the pdf entry point and display the result in a new window.

      What I don't understand is where would a custom entry point functionality be defined (in which folder) and also if possible could you provide an example of a "build document body and open new window with this body" code ?


      • #4
        I got it, here's what I did in case other newbie members like myself that might want to understand how the process of integrating a custom action button with a custom entry point works:

        Goal: Create a button at an entity's detail view that when pressed prints the entity fields using a template as html output to a new window, so the browser's "Print to PDF" capabilities render a PDF without having to be limited by TDPDF css limits.
        1. Create button inside the entity's record detail view file ("espocrm\client\custom\src\views\my-entity\record\detail.js")

          setupActionItems: function () {
          	       label: 'Print Lease (Window)',
          	       name: 'printLeaseWindow'
          actionPrintLeaseWindow: function () {
          	   var templateId = this.model.get("templateId");
    '?entryPoint=printToWindow&entityType=''&entityId=''&templateId=' + templateId, '_blank');
        2. Create class ("Entry Point") containing the entry point logic in file ("espocrm\custom\Espo\Custom\EntryPoints\PrintToWindow.php")

          namespace Espo\Custom\EntryPoints;
          	use \Espo\Core\Exceptions\NotFound;
          	use \Espo\Core\Exceptions\BadRequest;
          	class PrintToWindow extends \Espo\Core\EntryPoints\Base
          	   public static $authRequired = true;
          	   public function run()
          	       if (empty($_GET['entityId']) || empty($_GET['entityType']) || empty($_GET['templateId'])) {
          	           throw new BadRequest();
          	       $entityId = $_GET['entityId'];
          	       $entityType = $_GET['entityType'];
          	       $templateId = $_GET['templateId'];
          	       $entity = $this->getEntityManager()->getEntity($entityType, $entityId);
          	       $template = $this->getEntityManager()->getEntity('Template', $templateId);
          	       if (!$entity || !$template) {
          	           throw new NotFound();
          	       $this->getContainer()->get('serviceFactory')->create('PrintToWindowService')->buildFromTemplate($entity, $template, true);
        3. Create class ("Service") providing the logic to print an entity's fields using a given template as HTML output to a new window, file ("espocrm\custom\Espo\Custom\Services\PrintToWindowService.php")

          namespace Espo\Custom\Services;
          	use \Espo\Core\Exceptions\Forbidden;
          	use \Espo\Core\Exceptions\NotFound;
          	use \Espo\Core\Exceptions\Error;
          	use \Espo\ORM\Entity;
          	use \Espo\Core\Htmlizer\Htmlizer;
          	class PrintToWindowService extends \Espo\Core\Services\Base
          	   protected $fontFace = 'freesans';
          	   protected $fontSize = 10;
          	   protected $removeMassFilePeriod = '1 hour';
          	   protected function init()
          	   protected function getAcl()
          	       return $this->getInjection('acl');
          	   protected function getMetadata()
          	       return $this->getInjection('metadata');
          	   protected function getServiceFactory()
          	       return $this->getInjection('serviceFactory');
          	   protected function getFileManager()
          	       return $this->getInjection('fileManager');
          	   protected function createHtmlizer()
          	       return new Htmlizer(
          	   protected function printEntity(Entity $entity, Entity $template, Htmlizer $htmlizer)
          	       $pageOrientation = 'Portrait';
          	       $pageFormat = 'A4';
          	       if ($template->get('fontFace')) {
          	           $fontFace = $template->get('fontFace');
          	       if ($template->get('printFooter')) {
          	           $htmlFooter = $htmlizer->render($entity, $template->get('footer'));
          	       if ($template->get('pageOrientation')) {
          	           $pageOrientation = $template->get('pageOrientation');
          	       if ($template->get('pageFormat')) {
          	           $pageFormat = $template->get('pageFormat');
          	       $htmlHeader = $htmlizer->render($entity, $template->get('header'));
          	       $htmlBody = $htmlizer->render($entity, $template->get('body'));
          	       return $htmlHeader.$htmlBody.$htmlFooter;
          	   public function buildFromTemplate(Entity $entity, Entity $template)
          	       $entityType = $entity->getEntityType();
          	       $service = $this->getServiceFactory()->create($entityType);
          	       if ($template->get('entityType') !== $entityType) {
          	           throw new Forbidden();
          	       if (!$this->getAcl()->check($entity, 'read') || !$this->getAcl()->check($template, 'read')) {
          	           throw new Forbidden();
          	       $htmlizer = $this->createHtmlizer();
          	       $output = $this->printEntity($entity, $template, $htmlizer);
          	       echo $output;
        Last edited by telecastg; 11-14-2018, 08:21 AM.