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

Release #2

Merged
merged 5 commits into from
Nov 29, 2018
Merged
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
53 changes: 26 additions & 27 deletions Classes/Pdf/PdfSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -142,6 +137,11 @@ class PdfSettings
*/
protected $persistPDF = false;

/**
* @var string
*/
protected $additionalAttributes = '';

/**
* @var string
*/
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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())) {
Expand Down
1 change: 1 addition & 0 deletions Classes/Service/CsvService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ';')
Expand Down
1 change: 1 addition & 0 deletions Classes/Service/PdfService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 20 additions & 4 deletions Configuration/TypoScript/constants.typoscript
Original file line number Diff line number Diff line change
@@ -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
// <http://qt-project.org/doc/qt-4.8/qprinter.html#PaperSize-enum>.
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 =
}
}
}
2 changes: 1 addition & 1 deletion Configuration/TypoScript/setup.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
}
}
}
Expand Down
102 changes: 99 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
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
// <http://qt-project.org/doc/qt-4.8/qprinter.html#PaperSize-enum>.
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/'
]
);
```
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
"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"
}
},
"require": {
"typo3/cms-core": "^8.7",
"phpoffice/phpspreadsheet": "^1.5"
"typo3/cms-core": "^7.6.0 || ^8.7.0 || ^9.5.0",
"phpoffice/phpspreadsheet": "^1.5",
"php": ">=5.5.0 <7.3"
},
"suggest": {
"h4cc/wkhtmltopdf-amd64": "*"
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
Expand Down