Announcement

Collapse
No announcement yet.

Dompdf and imageTag helper

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dompdf and imageTag helper

    Hi,
    situation :
    i create qrCode with Endroid\QrCode library (github)
    the result 'image/png' is saved as Attachment.

    when try to print-to-pdf :
    if engine is :
    - dompdf : no result
    - tcpdf : qrcode is printed.

    so my solution was to create a new template helper "imageTagInline":
    see doc how make a custome pdf helper.

    certainly need more "control/throw error" but the base is here.
    Just, we can create attachment with : 'contents' => $contents,
    but we can't get the contents.. so it's why i use FileManager


    PHP Code:

    <?php

    namespace Espo\Custom\TemplateHelpers;

    use 
    Espo\Core\ORM\EntityManager;
    use 
    Espo\Core\Utils\Log;
    use 
    Espo\Core\Utils\File\Manager as FileManager;

    use 
    Espo\Core\Htmlizer\Helper;
    use 
    Espo\Core\Htmlizer\Helper\Data;
    use 
    Espo\Core\Htmlizer\Helper\Result;

    class 
    ImageTagInline implements Helper
    {
        public function 
    __construct(
            private 
    EntityManager $em,
            private 
    Log $log,
            private 
    FileManager $fileManager,
        ){}

        public function 
    render(Data $data): Result
        
    {        
            
    $idAttachment $data->getArgumentList()[0] ?? '';
            
    $attachment $this->em->getEntityById('Attachment'$idAttachment);

            
    $width $color $data->getOption('width');
            
    $height $data->getOption('height');

            
    $attributesPart "";

            if (
    $width) {
                
    $attributesPart .= " width="" .strval($width) . """;
            }

            if (
    $height) {
                
    $attributesPart .= " height="" .strval($height) . """;
            }

            
    //$contents = $attachment->get('contents'); DON'T WORK AS CREATE ATTACHMENT
            
    $contents =  $this->fileManager->getContents('data/upload/' .$idAttachment);
            
    $type $attachment->getType();
            
    $image base64_encode$contents );

            
    $html "<img src="data:{$type};base64,{$image}"{$attributesPart}>";

            return 
    Result::createSafeString($html);
        }
    }

  • #2
    Hey item

    You could use the attachment service to get the file content like below:

    PHP Code:
    <?php

    namespace Espo\Custom\TemplateHelpers;

    use 
    Espo\Core\ORM\EntityManager;
    use 
    Espo\Core\Utils\Log;
    use 
    Espo\Core\Utils\File\Manager as FileManager;

    use 
    Espo\Core\Htmlizer\Helper;
    use 
    Espo\Core\Htmlizer\Helper\Data;
    use 
    Espo\Core\Htmlizer\Helper\Result;
    use 
    Espo\Core\InjectableFactory;

    // Use This service to get the file content
    use Espo\Tools\Attachment\Service;

    class 
    ImageTagInline implements Helper
    {
        public function 
    __construct(
            private 
    EntityManager $em,
            private 
    Log $log,
            private 
    FileManager $fileManager,
            private 
    InjectableFactory $injectableFactory,
        ){}

        public function 
    render(Data $data): Result
        
    {        
            
    $attachmentId $data->getArgumentList()[0] ?? '';
            
    $attachment $this->em->getEntityById('Attachment'$attachmentId);

            
    $width $color $data->getOption('width');
            
    $height $data->getOption('height');

            
    $attributesPart "";

            if (
    $width) {
                
    $attributesPart .= " width="" .strval($width) . """;
            }

            if (
    $height) {
                
    $attributesPart .= " height="" .strval($height) . """;
            }

            
    $service $this->injectableFactory->create(Service::class);

            try {

                
    $fileData $service->getFileData($attachment->getId());
            }
            catch (
    Exception $e) {
                
    $message $e->getMessage();
                
    $this->log("Error while getting. Content: {$message}." 'error');

                return 
    false;
            }

            
    $image base64_encode($fileData->getStream());
            
    $type $attachment->getType();

            
    $html "<img src="data:{$type};base64,{$image}"{$attributesPart}>";

            return 
    Result::createSafeString($html);
        }
    }
    Rabii
    Web Dev

    Comment


    • #3
      No need to use InjectableFactory when a class can be injected via constructor. The Attachment/Service is not a right choice here.

      Comment


      • #4
        To read an attachment content, use Espo\Core\FileStorage\Manager.

        Comment


        • #5
          Thanks Yuri, thanks Rabii.

          PHP Code:
          <?php

          namespace Espo\Custom\TemplateHelpers;

          use 
          Espo\Core\ORM\EntityManager;
          use 
          Espo\Core\Utils\Log;
          use 
          Espo\Core\FileStorage\Manager as FileStorageManager;

          use 
          Espo\Core\Htmlizer\Helper;
          use 
          Espo\Core\Htmlizer\Helper\Data;
          use 
          Espo\Core\Htmlizer\Helper\Result;

          class 
          ImageTagInline implements Helper
          {
              public function 
          __construct(
                  private 
          EntityManager $em,
                  private 
          Log $log,
                  private 
          FileStorageManager $fileStorageManager,
              ){}

              public function 
          render(Data $data): Result
              
          {        
                  
          $attachmentId $data->getArgumentList()[0] ?? '';
                  
          $attachment $this->em->getEntityById('Attachment'$attachmentId);

                  if (!
          $attachment) {
                      return 
          Result::createSafeString('Attachment Not Found.');
                  }

                  
          $width $data->getOption('width');
                  
          $height $data->getOption('height');

                  
          $attributesPart "";

                  if (
          $width) {
                      
          $attributesPart .= " width="" .strval($width) . """;
                  }

                  if (
          $height) {
                      
          $attributesPart .= " height="" .strval($height) . """;
                  }

                  
          $contents $this->fileStorageManager->getContents($attachment);
                  
          $type $attachment->getType();
                  
          $image base64_encode$contents );

                  
          $html "<img src="data:{$type};base64,{$image}"{$attributesPart}>";

                  return 
          Result::createSafeString($html);
              }
          }
          Last edited by item; 10-16-2023, 10:49 AM.

          Comment

          Working...
          X