From c2f83df205ecad258d178501ee86836621e2ab6f Mon Sep 17 00:00:00 2001 From: Daniel Goerz Date: Thu, 29 Nov 2018 17:07:48 +0100 Subject: [PATCH 1/5] [TASK] Streamline PDF options --- Classes/Pdf/PdfSettings.php | 53 +++++++++---------- Classes/Service/PdfService.php | 1 + Configuration/TypoScript/constants.typoscript | 24 +++++++-- Configuration/TypoScript/setup.typoscript | 2 +- 4 files changed, 48 insertions(+), 32 deletions(-) diff --git a/Classes/Pdf/PdfSettings.php b/Classes/Pdf/PdfSettings.php index e87c779..0746782 100755 --- a/Classes/Pdf/PdfSettings.php +++ b/Classes/Pdf/PdfSettings.php @@ -55,11 +55,6 @@ class PdfSettings */ protected $tempFileName; - /** - * @var integer - */ - protected $cacheSeconds = 3600; - /** * If set the fileName will be appended with X characters of the md5 hash of the content * @@ -142,6 +137,11 @@ class PdfSettings */ protected $persistPDF = false; + /** + * @var string + */ + protected $additionalAttributes = ''; + /** * @var string */ @@ -225,28 +225,6 @@ public function setBinaryFilePath($binaryFilePath) return $this; } - /** - * Returns cache seconds - * - * @return integer - */ - public function getCacheSeconds() - { - return $this->cacheSeconds; - } - - /** - * Sets cache seconds - * - * @param integer $cacheSeconds - * @return $this - */ - public function setCacheSeconds($cacheSeconds) - { - $this->cacheSeconds = $cacheSeconds; - return $this; - } - /** * Returns the public temp file url * @@ -798,6 +776,27 @@ public function getJavaScriptDelayAttribute() return ''; } + /** + * @return string + */ + public function getAdditionalAttributes(): string + { + if (!empty($this->additionalAttributes)) { + return ' ' . trim($this->additionalAttributes); + } + return ''; + } + + /** + * @param string $additionalAttributes + * @return $this + */ + public function setAdditionalAttributes($additionalAttributes) + { + $this->additionalAttributes = $additionalAttributes; + return $this; + } + public function __destruct() { if (file_exists($this->getAbsoluteHtmlTempFilePath())) { diff --git a/Classes/Service/PdfService.php b/Classes/Service/PdfService.php index 37d1bc3..52ed439 100644 --- a/Classes/Service/PdfService.php +++ b/Classes/Service/PdfService.php @@ -112,6 +112,7 @@ public function saveToFile(string $filename = ''): string $this->settings->getOrientationAttribute() . $this->settings->getMarginAttributes() . $this->settings->getPageSizeAttribute() . + $this->settings->getAdditionalAttributes() . ' ' . $contentParameter . ' ' . $this->settings->getAbsoluteTempFilePath(); exec($command, $res, $ret); diff --git a/Configuration/TypoScript/constants.typoscript b/Configuration/TypoScript/constants.typoscript index 461c165..f14ec0d 100755 --- a/Configuration/TypoScript/constants.typoscript +++ b/Configuration/TypoScript/constants.typoscript @@ -1,29 +1,45 @@ plugin.tx_format { settings { - typeNum = 1386239798 pdf { // Path to the wkhtmltopdf binary binaryFilePath = /usr/local/bin/wkhtmltopdf + // Path were the PDFs are stored tempDirectoryPath = /tmp/ + // Default file name of the generated PDF tempFileName = - cacheSeconds = 3600 // If set the fileName will be appended with X characters of the md5 hash of the content md5Length = 0 + // Use print media-type instead of screen printMediaTypeAttribute = 1 + // Generates lower quality pdf/ps. Useful to shrink the result document space lowQualityAttribute = 0 + // Adds a html footer footerHtmlAttribute = + // URL to render (instead of content) url = + // Minimum font size minimumFontSize = 15 + // Set the page left margin (default 10mm) marginLeft = 10 + // Set the page right margin (default 10mm) marginRight = 10 + // Set the page top margin marginTop = 10 + // Set the page bottom margin marginBottom = 10 + // The default page size of the rendered document is A4, but using this + // --page-size optionthis can be changed to almost anything else, such as: A3, + // Letter and Legal. For a full list of supported pages sizes please see + // . pageSize = - // 200 is default for wkhtmltopdf + // Wait some milliseconds for javascript finish (default 200) javaScriptDelay = 200 + // Set orientation to Landscape or Portrait (default Portrait) orientation = Portrait - // Whether a generated PDF should be kept at the end of the process. By default it is deleted + // Whether a generated PDF should be kept at the end of the process. By default it is deleted. persistPDF = 0 + // For all supported attributes refer to https://wkhtmltopdf.org/usage/wkhtmltopdf.txt + additionalAttributes = } } } diff --git a/Configuration/TypoScript/setup.typoscript b/Configuration/TypoScript/setup.typoscript index d03e003..b8adda3 100644 --- a/Configuration/TypoScript/setup.typoscript +++ b/Configuration/TypoScript/setup.typoscript @@ -4,7 +4,6 @@ plugin.tx_format { binaryFilePath = {$plugin.tx_format.settings.pdf.binaryFilePath} tempDirectoryPath = {$plugin.tx_format.settings.pdf.tempDirectoryPath} tempFileName = {$plugin.tx_format.settings.pdf.tempFileName} - cacheSeconds = {$plugin.tx_format.settings.pdf.cacheSeconds} md5Length = {$plugin.tx_format.settings.pdf.md5Length} printMediaTypeAttribute = {$plugin.tx_format.settings.pdf.printMediaTypeAttribute} lowQualityAttribute = {$plugin.tx_format.settings.pdf.lowQualityAttribute} @@ -19,6 +18,7 @@ plugin.tx_format { javaScriptDelay = {$plugin.tx_format.settings.pdf.javaScriptDelay} orientation = {$plugin.tx_format.settings.pdf.orientation} persistPDF = {$plugin.tx_format.settings.pdf.persistPDF} + additionalAttributes = {$plugin.tx_format.settings.pdf.additionalAttributes} } } } From da5d0ca7adb0e778fc80ea48e05335e9c7cbd87a Mon Sep 17 00:00:00 2001 From: Daniel Goerz Date: Thu, 29 Nov 2018 17:08:25 +0100 Subject: [PATCH 2/5] [BUGFIX] Add missing parameter type annotation --- Classes/Service/CsvService.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Classes/Service/CsvService.php b/Classes/Service/CsvService.php index d43a84d..69e33ab 100644 --- a/Classes/Service/CsvService.php +++ b/Classes/Service/CsvService.php @@ -181,6 +181,7 @@ protected function prepareDataAsString($data, $delimiter = ';', $linedelimiter = * formats a value to be compatible with the CSV output * * @param mixed $value + * @param string $delimiter * @return string */ protected function formatValue($value, $delimiter = ';') From 332b5158d0cbf35ded1de49bc5910f12343e8728 Mon Sep 17 00:00:00 2001 From: Daniel Goerz Date: Thu, 29 Nov 2018 17:08:52 +0100 Subject: [PATCH 3/5] [TASK] Pin versions, dependencies and license --- composer.json | 4 +++- ext_emconf.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 87ed90b..94a3533 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "name": "b13/format", "type": "typo3-cms-extension", "homepage": "https://github.com/b13/t3ext-format", + "license": "GPL-2.0-or-later", "autoload": { "psr-4": { "B13\\Format\\": "Classes" @@ -9,7 +10,8 @@ }, "require": { "typo3/cms-core": "^8.7", - "phpoffice/phpspreadsheet": "^1.5" + "phpoffice/phpspreadsheet": "^1.5", + "php": ">=5.5.0 <7.3" }, "suggest": { "h4cc/wkhtmltopdf-amd64": "*" diff --git a/ext_emconf.php b/ext_emconf.php index b467c4b..ae34bf6 100644 --- a/ext_emconf.php +++ b/ext_emconf.php @@ -20,7 +20,7 @@ 'version' => '2.0.0', 'constraints' => [ 'depends' => [ - 'typo3' => '4.5.0-6.2.99' + 'typo3' => '>=7.6.0 <=9.6.0' ], 'conflicts' => [], 'suggests' => [], From d6ba95cb4fd1fe6b15979b55c6346f2af0f101b0 Mon Sep 17 00:00:00 2001 From: Daniel Goerz Date: Thu, 29 Nov 2018 17:09:04 +0100 Subject: [PATCH 4/5] [TASK] Improve Readme.md --- README.md | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ba37710..3416f8a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,100 @@ -t3ext-format -============ +#TYPO3 Extension format -Helper extension to allow easy use of putting data in different formats (CSV, Excel, PDF etc) \ No newline at end of file +Utility Extension for putting data in different formats like + +* CSV +* PDF +* Excel + +## Installation + +Simply install the extension with Composer or the Extension Manager. Include the TypoScript if you want to use the PDF generation. + +## Usage + +### CSV + +Use the public API of the `CsvService` to generate a CSV file or output the CSV string directly. + +### Excel + +There is no wrapper functionality to help with the creation of excel files. However the library +`phpoffice/phpspreadsheet` is required as a composer dependency and can therefore be used out of +the box in composer based installations. + +### PDF + +The PDF functionality relies on wkhtmltopdf which must be available on the server. +There are several ways to provide the binary. Please refer to the wkhtmltopdf documentation. + +The extension provides a PdfService. here is an example usage: + +``` +$pdfService = GeneralUtility::makeInstance(PdfService::class); +$pdfService->setContent($myHtml); +$absolutepathToFile = $pdfService->saveToFile('myPdf.pdf'); +``` + +This will create a file `myPdf.pdf` with the contents of `$myHtml` in a directory that can be configured in TypoScript. +The whole TypoScript configuration (`constants.typoscript`): + +``` +plugin.tx_format { + settings { + pdf { + // Path to the wkhtmltopdf binary + binaryFilePath = /usr/local/bin/wkhtmltopdf + // Path were the PDFs are stored + tempDirectoryPath = /tmp/ + // Default file name of the generated PDF + tempFileName = + // If set the fileName will be appended with X characters of the md5 hash of the content + md5Length = 0 + // Use print media-type instead of screen + printMediaTypeAttribute = 1 + // Generates lower quality pdf/ps. Useful to shrink the result document space + lowQualityAttribute = 0 + // Adds a html footer + footerHtmlAttribute = + // URL to render (instead of content) + url = + // Minimum font size + minimumFontSize = 15 + // Set the page left margin (default 10mm) + marginLeft = 10 + // Set the page right margin (default 10mm) + marginRight = 10 + // Set the page top margin + marginTop = 10 + // Set the page bottom margin + marginBottom = 10 + // The default page size of the rendered document is A4, but using this + // --page-size optionthis can be changed to almost anything else, such as: A3, + // Letter and Legal. For a full list of supported pages sizes please see + // . + pageSize = + // Wait some milliseconds for javascript finish (default 200) + javaScriptDelay = 200 + // Set orientation to Landscape or Portrait (default Portrait) + orientation = Portrait + // Whether a generated PDF should be kept at the end of the process. By default it is deleted. + persistPDF = 0 + // For all supported attributes refer to https://wkhtmltopdf.org/usage/wkhtmltopdf.txt + additionalAttributes = + } + } +} +``` + +Every setting can be overwritten during runtime: + +``` +$pdfService = GeneralUtility::makeInstance(PdfService::class); +$pdfService->setSettings( + [ + 'orientation' => 'Landscape, + 'marginLeft' => 25, + 'tempDirectoryPath' => PATH_site . 'something/public/' + ] +); +``` From e78143130a3f2d0d80fd64b88109cc8b4285a7e3 Mon Sep 17 00:00:00 2001 From: Daniel Goerz Date: Thu, 29 Nov 2018 17:20:37 +0100 Subject: [PATCH 5/5] [TASK] Allow 7LTS and 9LTS in composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 94a3533..0515282 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } }, "require": { - "typo3/cms-core": "^8.7", + "typo3/cms-core": "^7.6.0 || ^8.7.0 || ^9.5.0", "phpoffice/phpspreadsheet": "^1.5", "php": ">=5.5.0 <7.3" },