Skip to content

Commit

Permalink
[BUGFIX] Allow CSS styles to be loaded from private folders
Browse files Browse the repository at this point in the history
Resolves: #247
  • Loading branch information
liayn committed Oct 25, 2024
1 parent 1c25c1c commit 4c8d02c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
8 changes: 6 additions & 2 deletions Classes/Service/ConversionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
16 changes: 14 additions & 2 deletions Classes/ViewHelpers/HtmlViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
* * */

use Bithost\Pdfviewhelpers\Exception\Exception;
use Bithost\Pdfviewhelpers\Exception\ValidationException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use UnexpectedValueException;

/**
* HtmlViewHelper
Expand Down Expand Up @@ -96,8 +99,17 @@ public function render(): void

if (!empty($this->arguments['styleSheet'])) {
$styleSheetFile = $this->conversionService->convertFileSrcToFileObject($this->arguments['styleSheet']);

$htmlStyle = '<style>' . $styleSheetFile->getContents() . '</style>';
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 = '<style>' . $styleSheetContent . '</style>';
}

if ($this->arguments['autoHyphenation']) {
Expand Down
20 changes: 17 additions & 3 deletions Classes/ViewHelpers/ImageViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 21 additions & 7 deletions Classes/ViewHelpers/ListViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

use Bithost\Pdfviewhelpers\Exception\Exception;
use Bithost\Pdfviewhelpers\Exception\ValidationException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use UnexpectedValueException;

/**
* ListViewHelper
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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(
Expand All @@ -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";
Expand All @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
}
],
"require": {
"ext-json": "*",
"typo3/cms-core": "^11.5 || ^12.4",
"php": ">=7.4.0",
"tecnickcom/tcpdf": "^6.2",
Expand Down

0 comments on commit 4c8d02c

Please sign in to comment.