Announcement

Collapse
No announcement yet.

Backend Example of getting PDF file path from template call

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

  • Backend Example of getting PDF file path from template call

    Hello friends,

    We can make url calls such as

    /?entryPoint=pdf&entityType={MyEntityType}&entityId ={MyEntityId}&templateId={MyTemplateId}

    which will render pdf in browser.

    Does anyone have an example of php code on backend to make a call to this endpoint and/or use framework to Generate the PDF from the template and return the temporary pdf local file path ?

    I'm trying to figure out how I might use the framework on the backend (in php code) to generate a temporary local pdf file from the pdf template.


    For example,

    would something like this work?

    $url = /?entryPoint=pdf&entityType={MyEntityType}&entityId ={MyEntityId}&templateId={MyTemplateId};

    file_put_contents('file.pdf', fopen($url, 'r'));
    Last edited by czcpf; 10-12-2023, 06:02 PM.

  • #2
    You can call this service https://github.com/espocrm/espocrm/b...df/Service.php. It will return a content. You can then save it to an attachment if you need it as a file.

    Comment


    • czcpf
      czcpf commented
      Editing a comment
      thanks. I found that service about an hour ago. Very helpful. I will post a complete example shortly. Quick question: do files in data/tmp get cleaned out by Espo?

  • #3
    For anyone else needing this, here is a working example. This is for an ESPO generated service for a custom entity but the important parts are in myFunction()


    PHP Code:
    use Espo\Entities\Template;
    use 
    Espo\Tools\Pdf\Service as PdfService;
    use Espo\Core\{
        
    Di,
        
    Exceptions\Error,
        
    Exceptions\Forbidden,
        
    Exceptions\NotFound,
        
    Acl\Table,
        
    Templates\Services\Base as BaseService,
        
    Utils\DateTime as DateTimeUtil,
        
    Utils\File\Manager
    };

    /**
    * @extends BaseService<\Espo\Custom\Entities\MyCustomEntity>
    */


    class MyCustomEntity extends BaseService implements

        
    Di\FileManagerAware,
        
    Di\DataManagerAware,
        
    Di\LanguageAware,
        
    Di\ServiceFactoryAware,
        
    Di\InjectableFactoryAware
    {
        use 
    Di\FileManagerSetter;
        use 
    Di\DataManagerSetter;
        use 
    Di\LanguageSetter;
        use 
    Di\ServiceFactorySetter;
        use 
    Di\InjectableFactorySetter;

        
    public function myFunction(): void
        
    {

            
    $pdfTemplateId '12345';

           
    /** @var ?Template $template */
            
    $template $this->entityManager->getEntityById(Template::ENTITY_TYPE$pdfTemplateId) ;

            
    if($pdfTemplateId && $template) {

                
    /** @var PdfService $pdfService */
                
    $pdfService $this->injectableFactory->create(PdfService::class);
                
    $tempPath tempnam('data/tmp''my-custom-entity-pdf-item');

                
    try {

                     
    $pdfContents $pdfService->generate($entityType$id$pdfTemplateId) ;
                     
    $this->fileManager->putContents($tempPath$pdfContents->getString());

                    
    // do something with $tempPath
                    // which is a pdf file on the server in
                    // the /data/tmp directory

                    //Optional, delete the temporary file after you no longer need it
                    //$this->fileManager->unlink($tempPath);

                
    } catch (Error $e) {
                      
    $GLOBALS['log']->error('MyCustomEntity.php Service - myFunction',$e->getTrace());
                } catch (
    Forbidden $e) {
                      
    $GLOBALS['log']->error('MyCustomEntity.php Service - myFunction',$e->getTrace());
                } catch (
    NotFound $e) {
                      
    $GLOBALS['log']->error('MyCustomEntity.php Service - myFunction',$e->getTrace());
            }

        
    }





    Comment

    Working...
    X