V7.4 : HowTo PdfDefs Loader for Pdf Printing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • item
    Active Community Member
    • Mar 2017
    • 1476

    V7.4 : HowTo PdfDefs Loader for Pdf Printing

    Hi,

    whith this, you can print in pdf what you will
    in this sample, is for contact.

    custom/metadat/pdfDefs/Contact.json

    PHP Code:
    {
        "dataLoaderClassNameList": [
            "__APPEND__",
            "Espo\\Custom\\Classes\\Pdf\\Contact\\PdfDataLoader"
        ]
    }

    custom/Classes/Pdf/Contact/PdfDataLoader.php

    PHP Code:
    <?php
    
    namespace Espo\Custom\Classes\Pdf\Contact;
    
    use Espo\Tools\Pdf\Data\DataLoader;
    use Espo\Tools\Pdf\Params;
    use Espo\ORM\Entity;
    use Espo\Core\ORM\EntityManager;
    use Espo\Core\Utils\Log;
    
    use stdClass;
    
    class PdfDataLoader implements DataLoader
    {
        private $em;
        private $log;
    
        public function __construct( EntityManager $entityManager , Log $log)
        {
            $this->em = $entityManager;
            $this->log = $log;
    
        }
    
        public function load(Entity $entity, Params $params): stdClass
        {
    
            $account = $this->em->getRDBRepository('Account')
                ->where([ // where clause
                    'name' => 'blabla',
                ])
                ->findOne();
    
            if (!$account){
                return (object) [];
            }
    
            return (object) [
                'accountPostalCode' => $account->get('postalCode'),
                'ct' =>  $account->get('ct1') .'/' .$account->get('ct2'),
            ];
        }
    }
    and in PDF template just insert :

    PHP Code:
    {{accountPostalCode}} {{ct}} 
    

    here sample is all fake criteria.. but just 2 files, and you can "load any data from any entity" for print in pdf of the main entity

    doc : https://docs.espocrm.com/development...-defs/#pdfdefs
    Last edited by item; 06-04-2023, 07:29 PM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​
  • esforim
    Active Community Member
    • Jan 2020
    • 2204

    #2
    Noob here! But I use PDF lots of time. What does this code do?

    Is it getting data from a Relationship?

    Comment


    • rabii
      rabii commented
      Editing a comment
      Yes this class allows you to load additional data (relationships etc) for the current entity so that you could use that data in pdf template. this is useful if you want to load more data into an entity.

    • esforim
      esforim commented
      Editing a comment
      Cool, another task to try out. I guess it explain what what is currently available in EspoCRM using the . trick, e.g.

      contact.name
      contact.customfield
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #3
    Let me try explain with my poor english and of course my understanding :
    when relation many2many or field is parent with parentType, you can out-of-box only get parentName, parentId, or relationEntityId, relationEntityName

    in ours case, we use 90% meeting... and print meeting.
    but we need print info about attendee, parent, .. some relation data of contact

    not tested this code but must work for entity print Account
    PHP Code:
    
    $contactCollection = $entityManager
        ->getRDBRepository('Account')
        ->getRelation($entity, 'contacts')
        ->order('createdAt', 'DESC')
        ->find();
    
     return (object) [
                'contactCollection' => $contactCollection,
          
            ];
    
    en in pdf

    PHP Code:
    {{#each contactCollection}}
          {{phoneNumber}} {{name}} {{all field of contact}}
    {{/each}}
    Last edited by item; 06-05-2023, 08:02 AM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    Working...