Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Allow CSS styles to be loaded from private folders #248

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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
15 changes: 13 additions & 2 deletions Classes/ViewHelpers/HtmlViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* * */

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

/**
* HtmlViewHelper
Expand Down Expand Up @@ -96,8 +98,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 && file_exists($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 && file_exists($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 && file_exists($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
5 changes: 3 additions & 2 deletions Tests/Functional/ViewHelpers/HtmlViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
* This copyright notice MUST APPEAR in all copies of the script!
* * */

use Bithost\Pdfviewhelpers\Exception\ValidationException;
use Bithost\Pdfviewhelpers\Tests\Functional\AbstractFunctionalTestCase;
use UnexpectedValueException;

/**
* HtmlViewHelperTest
Expand Down Expand Up @@ -65,7 +65,8 @@ public function testRenderRichText(): void
*/
public function testInvalidStylesheet(): void
{
$this->expectException(ValidationException::class);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionCode(1729853324);

$this->renderFluidTemplate(
$this->getFixtureExtPath('HtmlViewHelper/Html.html'),
Expand Down
5 changes: 3 additions & 2 deletions Tests/Functional/ViewHelpers/ImageViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
* This copyright notice MUST APPEAR in all copies of the script!
* * */

use Bithost\Pdfviewhelpers\Exception\ValidationException;
use Bithost\Pdfviewhelpers\Tests\Functional\AbstractFunctionalTestCase;
use TYPO3\CMS\Core\Resource\FileInterface;
use UnexpectedValueException;

/**
* ImageViewHelperTest
Expand Down Expand Up @@ -82,7 +82,8 @@ public function testFileSource(): void
*/
public function testInvalidStringSource(): void
{
$this->expectException(ValidationException::class);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionCode(1729853325);

$output = $this->renderFluidTemplate(
$this->getFixtureExtPath('ImageViewHelper/Image.html'),
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