diff --git a/Classes/Service/ConversionService.php b/Classes/Service/ConversionService.php index 5b4b164..5c2f1c7 100755 --- a/Classes/Service/ConversionService.php +++ b/Classes/Service/ConversionService.php @@ -35,6 +35,7 @@ use TYPO3\CMS\Core\Resource\FileInterface; use TYPO3\CMS\Core\Resource\ResourceFactory; use TYPO3\CMS\Core\SingletonInterface; +use TYPO3\CMS\Core\Utility\PathUtility; use TYPO3\CMS\Extbase\Domain\Model\FileReference; /** @@ -212,10 +213,10 @@ public function convertImageExtensionToRenderMode(string $extension): string /** * @param string|FileInterface|FileReference $src - * + * @return FileInterface|null return null if the file is not within FAL * @throws Exception */ - public function convertFileSrcToFileObject($src): FileInterface + public function convertFileSrcToFileObject($src): ?FileInterface { $file = null; $previousException = null; @@ -226,6 +227,9 @@ public function convertFileSrcToFileObject($src): FileInterface } elseif ($src instanceof FileReference) { $file = $src->getOriginalResource(); } else { + if (PathUtility::isExtensionPath($src)) { + return null; + } try { $file = $this->resourceFactory->retrieveFileOrFolderObject($src); } catch (\Exception $e) { diff --git a/Classes/ViewHelpers/HtmlViewHelper.php b/Classes/ViewHelpers/HtmlViewHelper.php index 155f23b..f535fb2 100755 --- a/Classes/ViewHelpers/HtmlViewHelper.php +++ b/Classes/ViewHelpers/HtmlViewHelper.php @@ -31,6 +31,8 @@ * * */ use Bithost\Pdfviewhelpers\Exception\Exception; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use UnexpectedValueException; /** * HtmlViewHelper @@ -96,8 +98,17 @@ public function render(): void if (!empty($this->arguments['styleSheet'])) { $styleSheetFile = $this->conversionService->convertFileSrcToFileObject($this->arguments['styleSheet']); - - $htmlStyle = ''; + if ($styleSheetFile) { + $styleSheetContent = $styleSheetFile->getContents(); + } else { + $styleSheetFileName = GeneralUtility::getFileAbsFileName($this->arguments['styleSheet']); + if ($styleSheetFileName) { + $styleSheetContent = file_get_contents($styleSheetFileName); + } else { + throw new UnexpectedValueException('Provided CSS file can\'t be loaded: ' . $this->arguments['styleSheet'], 1729853324); + } + } + $htmlStyle = ''; } if ($this->arguments['autoHyphenation']) { diff --git a/Classes/ViewHelpers/ImageViewHelper.php b/Classes/ViewHelpers/ImageViewHelper.php index d494466..b92410f 100755 --- a/Classes/ViewHelpers/ImageViewHelper.php +++ b/Classes/ViewHelpers/ImageViewHelper.php @@ -33,7 +33,9 @@ use Bithost\Pdfviewhelpers\Exception\Exception; use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection; use TYPO3\CMS\Core\Resource\FileInterface; +use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Service\ImageService; +use UnexpectedValueException; /** * ImageViewHelper @@ -86,10 +88,22 @@ public function render(): void $this->initializeMultiColumnSupport(); $imageFile = $this->conversionService->convertFileSrcToFileObject($this->arguments['src']); - $processedImage = $this->processImage($imageFile, $this->arguments['processingInstructions']); + if ($imageFile) { + $processedImage = $this->processImage($imageFile, $this->arguments['processingInstructions']); - $src = '@' . $processedImage->getContents(); - $extension = $processedImage->getExtension(); + $src = $processedImage->getContents(); + $extension = $processedImage->getExtension(); + } else { + $imageFileName = GeneralUtility::getFileAbsFileName($this->arguments['src']); + if ($imageFileName) { + $src = file_get_contents($imageFileName); + $extension = pathinfo($imageFileName, PATHINFO_EXTENSION); + } else { + throw new UnexpectedValueException('Provided image source can\'t be loaded: ' . $this->arguments['src'], 1729853325); + } + } + // prepend @ symbol to tell PDF renderer this is inline content + $src = '@' . $src; $multiColumnContext = $this->getCurrentMultiColumnContext(); $isInAColumn = is_array($multiColumnContext) && ($multiColumnContext['isInAColumn'] ?? false); diff --git a/Classes/ViewHelpers/ListViewHelper.php b/Classes/ViewHelpers/ListViewHelper.php index d5ae5f4..c78e11b 100755 --- a/Classes/ViewHelpers/ListViewHelper.php +++ b/Classes/ViewHelpers/ListViewHelper.php @@ -32,6 +32,8 @@ use Bithost\Pdfviewhelpers\Exception\Exception; use Bithost\Pdfviewhelpers\Exception\ValidationException; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use UnexpectedValueException; /** * ListViewHelper @@ -66,8 +68,8 @@ public function initialize() if (!empty($this->arguments['bulletImageSrc'])) { $bulletImageFile = $this->conversionService->convertFileSrcToFileObject($this->arguments['bulletImageSrc']); - - if (!($this->conversionService->convertImageExtensionToRenderMode($bulletImageFile->getExtension()) === 'image')) { + $fileExtension = $bulletImageFile ? $bulletImageFile->getExtension() : pathinfo($this->arguments['bulletImageSrc'], PATHINFO_EXTENSION); + if ($this->conversionService->convertImageExtensionToRenderMode($fileExtension) !== 'image') { throw new ValidationException('Image type not supported for list. ERROR: 1363771014', 1363771014); } } @@ -102,14 +104,26 @@ public function render(): void //Update y respecting padding $this->getPDF()->setY($this->arguments['posY'] + $this->arguments['padding']['top']); + $bulletImageFileContent = ''; if (!empty($this->arguments['bulletImageSrc'])) { $bulletImageFile = $this->conversionService->convertFileSrcToFileObject($this->arguments['bulletImageSrc']); - $bulletImageFileContent = '@' . $bulletImageFile->getContents(); + if ($bulletImageFile) { + $bulletImageFileContent = $bulletImageFile->getContents(); + } else { + $bulletImageFileName = GeneralUtility::getFileAbsFileName($this->arguments['bulletImageSrc']); + if ($bulletImageFileName) { + $bulletImageFileContent = file_get_contents($bulletImageFileName); + } else { + throw new UnexpectedValueException('Provided bullet image source can\'t be loaded: ' . $this->arguments['bulletImageSrc'], 1729853326); + } + } + // prepend @ symbol to tell PDF renderer this is inline content + $bulletImageFileContent = '@' . $bulletImageFileContent; } //The height of a single text line $oneLineTextHeight = $this->getPDF()->getStringHeight($textWidth, '.'); - $elementEndY = $this->getPDF()->getY(); + $elementEndY = $this->getPDF()->GetY(); foreach ($this->arguments['listElements'] as $listElement) { if ($this->arguments['autoHyphenation']) { $listElement = $this->hyphenationService->hyphenateText( @@ -119,7 +133,7 @@ public function render(): void } $elementStartPage = $this->getPDF()->getPage(); - $elementStartY = $this->getPDF()->getY(); + $elementStartY = $this->getPDF()->GetY(); if ($this->arguments['paragraphLineFeed']) { $listElement .= "\n"; @@ -128,7 +142,7 @@ public function render(): void $this->getPDF()->MultiCell($textWidth, $this->arguments['height'], $listElement, 0, $this->conversionService->convertSpeakingAlignmentToTcpdfAlignment($this->arguments['alignment']), false, 1, $textPosX, null, true, 0, false, true, 0, 'T', false); $elementEndPage = $this->getPDF()->getPage(); - $elementEndY = $this->getPDF()->getY(); + $elementEndY = $this->getPDF()->GetY(); $scaledPageHeight = $this->getPDF()->getScaledPageHeight(); $breakMargin = $this->getPDF()->getBreakMargin($elementStartPage); @@ -141,7 +155,7 @@ public function render(): void $this->getPDF()->setPage($elementStartPage); } - if (empty($this->arguments['bulletImageSrc'])) { + if (!$bulletImageFileContent) { $this->getPDF()->Rect($bulletPosX, $elementStartY + $relativBulletPosY, $this->arguments['bulletSize'], $this->arguments['bulletSize'], 'F', null, [$this->arguments['bulletColor']['R'], $this->arguments['bulletColor']['G'], $this->arguments['bulletColor']['B']]); } else { $this->getPDF()->Image($bulletImageFileContent, $bulletPosX, $elementStartY + $relativBulletPosY, $this->arguments['bulletSize'], null, '', '', '', false, 300, '', false, false, 0, false, false, true, false); diff --git a/composer.json b/composer.json index dbfc0bf..5c8b5f9 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ } ], "require": { + "ext-json": "*", "typo3/cms-core": "^11.5 || ^12.4", "php": ">=7.4.0", "tecnickcom/tcpdf": "^6.2",