config = $config; $this->templateRendererFactory = $templateRendererFactory; } public function process(Tcpdf $pdf, Template $template, Entity $entity, Params $params, Data $data): void { $renderer = $this->templateRendererFactory ->create() ->setApplyAcl($params->applyAcl()) ->setEntity($entity) ->setData($data->getAdditionalTemplateData()); $fontFace = $this->config->get('pdfFontFace', $this->fontFace); $fontSize = $this->config->get('pdfFontSize', $this->fontSize); if ($template->getFontFace()) { $fontFace = $template->getFontFace(); } if ($template->hasTitle()) { $title = $this->replacePlaceholders($template->getTitle(), $entity); $pdf->SetTitle($title); } $pdf->setFont($fontFace, '', $fontSize, '', true); $pdf->setAutoPageBreak(true, $template->getBottomMargin()); $pdf->setMargins( $template->getLeftMargin(), $template->getTopMargin(), $template->getRightMargin() ); $pageOrientation = $template->getPageOrientation(); $pageFormat = $template->getPageFormat(); if ($pageFormat === 'Custom') { $pageFormat = [ $template->getPageWidth(), $template->getPageHeight(), ]; } $pageOrientationCode = 'P'; if ($pageOrientation === 'Landscape') { $pageOrientationCode = 'L'; } if ($template->hasFooter()) { $htmlFooter = $this->render($renderer, $template->getFooter()); $pdf->setFooterFont([$fontFace, '', $this->fontSize]); $pdf->setFooterPosition($template->getFooterPosition()); $pdf->setFooterHtml($htmlFooter); } else { $pdf->setPrintFooter(false); } if ($template->hasHeader()) { $htmlHeader = $this->render($renderer, $template->getHeader()); $pdf->setHeaderFont([$fontFace, '', $this->fontSize]); $pdf->setHeaderPosition($template->getHeaderPosition()); $pdf->setHeaderHtml($htmlHeader); } else { $pdf->setPrintHeader(false); } $pdf->addPage($pageOrientationCode, $pageFormat); $htmlBody = $this->render($renderer, $template->getBody()); $pdf->writeHTML($htmlBody, true, false, true, false, ''); } private function render(TemplateRenderer $renderer, string $template): string { $html = $renderer->renderTemplate($template); /** @var string */ return preg_replace_callback( '//', function ($matches) { $dataString = $matches[1]; $data = json_decode(urldecode($dataString), true); return $this->composeBarcodeTag($data); }, $html ); } /** * * @param array $data * @return string */ private function composeBarcodeTag(array $data): string { $value = $data['value'] ?? null; $codeType = $data['type'] ?? 'CODE128'; $typeMap = [ "CODE128" => 'C128', "CODE128A" => 'C128A', "CODE128B" => 'C128B', "CODE128C" => 'C128C', "EAN13" => 'EAN13', "EAN8" => 'EAN8', "EAN5" => 'EAN5', "EAN2" => 'EAN2', "UPC" => 'UPCA', "UPCE" => 'UPCE', "ITF14" => 'I25', "pharmacode" => 'PHARMA', "QRcode" => 'QRCODE,H', ]; if ($codeType === 'QRcode') { $function = 'write2DBarcode'; $params = [ $value, $typeMap[$codeType] ?? null, /** @phpstan-ignore-line */ '', '', $data['width'] ?? 40, $data['height'] ?? 40, [ 'border' => false, 'vpadding' => $data['padding'] ?? 2, 'hpadding' => $data['padding'] ?? 2, 'fgcolor' => $data['color'] ?? [0, 0, 0], 'bgcolor' => $data['bgcolor'] ?? false, 'module_width' => 1, 'module_height' => 1, ], 'N', ]; } else { $function = 'write1DBarcode'; $params = [ $value, $typeMap[$codeType] ?? null, '', '', $data['width'] ?? 60, $data['height'] ?? 30, 0.4, [ 'position' => 'S', 'border' => false, 'padding' => $data['padding'] ?? 0, 'fgcolor' => $data['color'] ?? [0, 0, 0], 'bgcolor' => $data['bgcolor'] ?? [255, 255, 255], 'text' => $data['text'] ?? true, 'font' => 'helvetica', 'fontsize' => $data['fontsize'] ?? 14, 'stretchtext' => 4, ], 'N', ]; } $paramsString = urlencode(Json::encode($params)); return ""; } private function replacePlaceholders(string $string, Entity $entity): string { $newString = $string; $attributeList = ['name']; foreach ($attributeList as $attribute) { $value = (string) ($entity->get($attribute) ?? ''); $newString = str_replace('{$' . $attribute . '}', $value, $newString); } return $newString; } }