Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 111 additions & 23 deletions Classes/Service/PdfService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@
use Extcode\Cart\Domain\Model\Order\Item as OrderItem;
use Extcode\CartPdf\Domain\Model\Dto\PdfDemand;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\View\ViewFactoryData;
use TYPO3\CMS\Core\View\ViewFactoryInterface;
use TYPO3\CMS\Core\View\ViewInterface;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;

readonly class PdfService implements DocumentRenderServiceInterface
{
private array $configuration;
private array $viewSettings;

public function __construct(
private readonly ConfigurationManager $configurationManager,
private ViewFactoryInterface $viewFactory,
) {
if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend()) {
$pageId = (int)($GLOBALS['TYPO3_REQUEST']->getQueryParams()['id'] ?? 1);
Expand All @@ -40,27 +46,106 @@ public function __construct(
ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
'CartPdf'
);

$viewSettings = $this->configuration['view'] ?? [];
$this->viewSettings = is_array($viewSettings) ? $viewSettings : [];
}

public function getStandaloneView(string $format = 'html'): ViewInterface
{
return $this->viewFactory->create(new ViewFactoryData(
$this->viewSettings['templateRootPaths'] ?? [],
$this->viewSettings['partialRootPaths'] ?? [],
$this->viewSettings['layoutRootPaths'] ?? [],
null,
$GLOBALS['TYPO3_REQUEST'] ?? null,
$format,
));
}

public function renderStandaloneView(
string $templatePath,
string $type,
array $config,
array $assignToView = [],
): string {
$view = $this->getStandaloneView();

if (!empty($config['file'])) {
$file = GeneralUtility::getFileAbsFileName($config['file']);
$view->assign('file', $file);
$view->assign('file_base64', base64_encode(file_get_contents($file)));
$view->assign('file_mimetype', mime_content_type($file));

if (!empty($config['width'])) {
$view->assign('width', $config['width']);
}

if (!empty($config['height'])) {
$view->assign('height', $config['height']);
}
}

$view->assignMultiple($assignToView);

$content = (string)$view->render($templatePath . ucfirst($type));
return trim(preg_replace('~[\\n]+~', '', $content));
}

public function renderDocument(OrderItem $orderItem, string $pdfType): string
{
$pdfType .= 'Pdf';
$pdfSettings = $this->configuration[$pdfType];
$pdfSettings = $this->configuration[$pdfType] ?? [];
$pdfSettings = is_array($pdfSettings) ? $pdfSettings : [];
$pdfDemand = PdfDemand::createFromSettings($pdfSettings);

$pdf = TcpdfWrapper::createWithTypeAndSettings(
$pdfType,
$this->configuration,
);

foreach ($pdfSettings['header']['html'] ?? [] as $partName => $partConfig) {
$assignToView = [];
if ($partName === 'page') {
$assignToView['numPage'] = $pdf->getAliasNumPage();
$assignToView['numPages'] = $pdf->getAliasNbPages();
}

$content = $this->renderStandaloneView(
$pdfSettings['header']['html'][$partName]['templatePath'],
$partName,
$partConfig,
$assignToView,
);

$pdf->addHeaderPart($content, $partConfig);
}

foreach ($pdfSettings['footer']['html'] ?? [] as $partName => $partConfig) {
$assignToView = [];
if ($partName === 'page') {
$assignToView['numPage'] = $pdf->getAliasNumPage();
$assignToView['numPages'] = $pdf->getAliasNbPages();
}

$content = $this->renderStandaloneView(
$pdfSettings['footer']['html'][$partName]['templatePath'],
$partName,
$partConfig,
$assignToView,
);

$pdf->addFooterPart($content, $partConfig);
}

if (empty($pdfSettings['header'])) {
$pdf->setPrintHeader(false);
} else {
if (!empty($pdfSettings['header']['margin'])) {
$pdf->setHeaderMargin((int)$pdfSettings['header']['margin']);
$pdf->SetMargins(PDF_MARGIN_LEFT, (float)$pdfSettings['header']['margin'], PDF_MARGIN_RIGHT);
$pdf->setMargins(PDF_MARGIN_LEFT, (float)$pdfSettings['header']['margin'], PDF_MARGIN_RIGHT);
} else {
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->setMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
}
}

Expand Down Expand Up @@ -95,7 +180,7 @@ public function renderDocument(OrderItem $orderItem, string $pdfType): string
$fontSize = $pdfSettings['fontSize'];
}

$pdf->SetFont($font, $fontStyle, $fontSize);
$pdf->setFont($font, $fontStyle, $fontSize);

$colorArray = [0, 0, 0];

Expand All @@ -110,15 +195,17 @@ public function renderDocument(OrderItem $orderItem, string $pdfType): string
foreach ($pdfSettings['letterhead']['html'] as $partName => $partConfig) {
$templatePath = '/' . ucfirst($pdfType) . '/Letterhead/';
$assignToView = ['orderItem' => $orderItem];
$pdf->renderStandaloneView($templatePath, $partName, $partConfig, $assignToView);
$content = $this->renderStandaloneView($templatePath, $partName, $partConfig, $assignToView);
$pdf->writeHtmlCellWithConfig($content, $partConfig);
}
}

if (is_array($pdfSettings['body']['before']['html'] ?? null)) {
foreach ($pdfSettings['body']['before']['html'] as $partName => $partConfig) {
$templatePath = '/' . ucfirst($pdfType) . '/Body/Before/';
$assignToView = ['orderItem' => $orderItem];
$pdf->renderStandaloneView($templatePath, $partName, $partConfig, $assignToView);
$content = $this->renderStandaloneView($templatePath, $partName, $partConfig, $assignToView);
$pdf->writeHtmlCellWithConfig($content, $partConfig);
}
}

Expand All @@ -128,7 +215,8 @@ public function renderDocument(OrderItem $orderItem, string $pdfType): string
foreach ($pdfSettings['body']['after']['html'] as $partName => $partConfig) {
$templatePath = '/' . ucfirst($pdfType) . '/Body/After/';
$assignToView = ['orderItem' => $orderItem];
$pdf->renderStandaloneView($templatePath, $partName, $partConfig, $assignToView);
$content = $this->renderStandaloneView($templatePath, $partName, $partConfig, $assignToView);
$pdf->writeHtmlCellWithConfig($content, $partConfig);
}
}

Expand All @@ -138,15 +226,15 @@ public function renderDocument(OrderItem $orderItem, string $pdfType): string
private function renderMarker(TcpdfWrapper $pdf, PdfDemand $pdfDemand): void
{
if ($pdfDemand->getFoldMarksEnabled()) {
$pdf->SetLineWidth(0.1);
$pdf->setLineWidth(0.1);
$pdf->Line(6.0, 105.0, 8.0, 105.0);
$pdf->Line(6.0, 148.5, 10.0, 148.5);
$pdf->Line(6.0, 210.0, 8.0, 210.0);
$pdf->SetLineWidth(0.2);
$pdf->setLineWidth(0.2);
}

if ($pdfDemand->getAddressFieldMarksEnabled()) {
$pdf->SetLineWidth(0.1);
$pdf->setLineWidth(0.1);

$pdf->Line(20.0, 45.0, 21.0, 45.0);
$pdf->Line(20.0, 45.0, 20.0, 46.0);
Expand All @@ -159,7 +247,7 @@ private function renderMarker(TcpdfWrapper $pdf, PdfDemand $pdfDemand): void
$pdf->Line(105.0, 90.0, 104.0, 90.0);
$pdf->Line(105.0, 90.0, 105.0, 89.0);

$pdf->SetLineWidth(0.2);
$pdf->setLineWidth(0.2);
}
}

Expand All @@ -172,47 +260,47 @@ private function renderCart(TcpdfWrapper $pdf, OrderItem $orderItem, string $pdf
$config['spacingY'] = 5;
}

$headerOut = $this->renderCartHeader($pdf, $orderItem, $pdfType);
$bodyOut = $this->renderCartBody($pdf, $orderItem, $pdfType);
$footerOut = $this->renderCartFooter($pdf, $orderItem, $pdfType);
$headerOut = $this->renderCartHeader($orderItem, $pdfType);
$bodyOut = $this->renderCartBody($orderItem, $pdfType);
$footerOut = $this->renderCartFooter($orderItem, $pdfType);

$content = '<table cellpadding="3">' . $headerOut . $bodyOut . $footerOut . '</table>';

$pdf->writeHtmlCellWithConfig($content, $config);
}

private function renderCartHeader(TcpdfWrapper $pdf, OrderItem $orderItem, string $pdfType): string
private function renderCartHeader(OrderItem $orderItem, string $pdfType): string
{
$view = $pdf->getStandaloneView('/' . ucfirst($pdfType) . '/Order/', 'Header');
$view = $this->getStandaloneView();
$view->assign('orderItem', $orderItem);
$header = $view->render();
$header = $view->render('/' . ucfirst($pdfType) . '/Order/Header');

return trim(preg_replace('~[\\n]+~', '', $header));
}

private function renderCartBody(TcpdfWrapper $pdf, OrderItem $orderItem, string $pdfType): string
private function renderCartBody(OrderItem $orderItem, string $pdfType): string
{
$view = $pdf->getStandaloneView('/' . ucfirst($pdfType) . '/Order/', 'Product');
$view = $this->getStandaloneView();
$view->assign('orderItem', $orderItem);

$bodyOut = '';

foreach ($orderItem->getProducts() as $product) {
$view->assign('product', $product);
$product = $view->render();
$product = $view->render('/' . ucfirst($pdfType) . '/Order/Product');

$bodyOut .= trim(preg_replace('~[\\n]+~', '', $product));
}

return $bodyOut;
}

private function renderCartFooter(TcpdfWrapper $pdf, OrderItem $orderItem, string $pdfType): string
private function renderCartFooter(OrderItem $orderItem, string $pdfType): string
{
$view = $pdf->getStandaloneView('/' . ucfirst($pdfType) . '/Order/', 'Footer');
$view = $this->getStandaloneView();
$view->assign('orderSettings', $this->configuration[$pdfType]['body']['order']);
$view->assign('orderItem', $orderItem);
$footer = $view->render();
$footer = $view->render('/' . ucfirst($pdfType) . '/Order/Footer');

return trim(preg_replace('~[\\n]+~', '', $footer));
}
Expand Down
Loading