entityManager = $entityManager; $this->acl = $acl; $this->serviceContainer = $serviceContainer; $this->dataLoaderManager = $dataLoaderManager; $this->config = $config; $this->builder = $builder; } /** * Generate a PDF. * * @param string $entityType An entity type. * @param string $id A record ID. * @param string $templateId A template ID. * @param ?Params $params Params. If null, a params with the apply-acl will be used. * @params ?Data $data Data. * * @throws Error * @throws NotFound * @throws Forbidden */ public function generate( string $entityType, string $id, string $templateId, ?Params $params = null, ?Data $data = null ): Contents { $params = $params ?? Params::create()->withAcl(true); $applyAcl = $params->applyAcl(); $entity = $this->entityManager->getEntityById($entityType, $id); if (!$entity) { throw new NotFound("Record not found."); } /** @var ?TemplateEntity $template */ $template = $this->entityManager->getEntityById(TemplateEntity::ENTITY_TYPE, $templateId); if (!$template) { throw new NotFound("Template not found."); } if ($applyAcl && !$this->acl->checkEntityRead($entity)) { throw new Forbidden("No access to record."); } if ($applyAcl && !$this->acl->checkEntityRead($template)) { throw new Forbidden("No access to template."); } $service = $this->serviceContainer->get($entityType); $service->loadAdditionalFields($entity); if (method_exists($service, 'loadAdditionalFieldsForPdf')) { // For bc. $service->loadAdditionalFieldsForPdf($entity); } if ($template->getTargetEntityType() !== $entityType) { throw new Error("Not matching entity types."); } $data = $this->dataLoaderManager->load($entity, $params, $data); $engine = $this->config->get('pdfEngine') ?? self::DEFAULT_ENGINE; $printer = $this->builder ->setTemplate(new TemplateWrapper($template)) ->setEngine($engine) ->build(); return $printer->printEntity($entity, $params, $data); } }