I don't know if this is of interest to anyone but I thought I'd share it.
As we are implementing more and more business functions within EspoCRM, we are producing more and more Templates which are ultimately being turned into PDF files (orders, job sheets, inventory, etc, etc). Once of the needs we have is to be able to generate a PDF from a template and save it to our company network with a given file name format (depending on the type of information within the file).
Natively, EspoCRM doesn't seem to allow you to be able to generate a file name format for each individual entity so I've managed to bake my own system. Its not upgrade-proof but for us, that does not matter. Someone may want to take this forward to make it so.
The following implementation is what I did to allow us to generate file names specific to an entity:
1. Edit the application\Espo\Services\Pdf.php file, replacing the buildFromTemplate function with below
The main change above is that a service function called `getFileNameForPdf` will get called if it exists.
Next, we need to go into the relevant `Services\EntityName.php` file and add the function as below:
And voila, we now have customisable file names on an Entity-by-Entity basis.
As we are implementing more and more business functions within EspoCRM, we are producing more and more Templates which are ultimately being turned into PDF files (orders, job sheets, inventory, etc, etc). Once of the needs we have is to be able to generate a PDF from a template and save it to our company network with a given file name format (depending on the type of information within the file).
Natively, EspoCRM doesn't seem to allow you to be able to generate a file name format for each individual entity so I've managed to bake my own system. Its not upgrade-proof but for us, that does not matter. Someone may want to take this forward to make it so.
The following implementation is what I did to allow us to generate file names specific to an entity:
1. Edit the application\Espo\Services\Pdf.php file, replacing the buildFromTemplate function with below
Code:
public function buildFromTemplate(Entity $entity, Entity $template, $displayInline = false) { $entityType = $entity->getEntityType(); // Get the name field from the entity for a default file name $name = $entity->get('name'); if ($this->getServiceFactory()->checkExists($entityType)) { $service = $this->getServiceFactory()->create($entityType); } else { $service = $this->getServiceFactory()->create('Record'); } $service->loadAdditionalFields($entity); if (method_exists($service, 'loadAdditionalFieldsForPdf')) { $service->loadAdditionalFieldsForPdf($entity); } // Pull the file name from the service, if the function exists if (method_exists($service, 'getFileNameForPdf')) { $name = $service->getFileNameForPdf($entity); } 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(); $pdf = new \Espo\Core\Pdf\Tcpdf(); $this->printEntity($entity, $template, $htmlizer, $pdf); if ($displayInline) { $name = \Espo\Core\Utils\Util::sanitizeFileName($name); $fileName = $name . '.pdf'; $pdf->output($fileName, 'I'); return; } return $pdf->output('', 'S'); }
Next, we need to go into the relevant `Services\EntityName.php` file and add the function as below:
Code:
public function getFileNameForPdf(Entity $entity) { return $entity->get('name') . ' - ' . $entity->get('accountName'); }
Comment