Expose PDF writer to be used directly

We used to have some kind of wrapper that didn't do much except
forward methods to the real instance. That unnecessary complexity
made it harder to work with the real writer instance.
This commit is contained in:
Adrien Crivelli 2017-10-14 14:57:44 +09:00
parent 98cd5e07bf
commit 79ab852bf5
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
17 changed files with 319 additions and 376 deletions

View File

@ -27,6 +27,9 @@
"php-cs-fixer fix --ansi --dry-run --diff",
"phpcs --report-width=200 --report-summary --report-full samples/ src/ tests/ --ignore=samples/Header.php --standard=PSR2 -n",
"phpunit --color=always"
],
"fix": [
"php-cs-fixer fix --ansi"
]
},
"require": {

View File

@ -124,13 +124,15 @@ autoloading mechanism.
## Writing PDF
`PHPExcel_Settings::setPdfRenderer()` and `PHPExcel_Settings::setPdfRenderer()`
were removed and PDF libraries must be installed via composer. So the only thing
to do is to specify a renderer like so:
`PHPExcel_Settings::getPdfRenderer()` and `PHPExcel_Settings::setPdfRenderer()`
were removed. `PHPExcel_Settings::getPdfRendererName()` and
`PHPExcel_Settings::setPdfRendererName()` were renamed as `setDefaultPdfWriter()`
and `setDefaultPdfWriter()` respectively. And PDF libraries must be installed via
composer. So the only thing to do is to specify a default writer class like so:
```php
$rendererName = \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_MPDF;
\PhpOffice\PhpSpreadsheet\Settings::setPdfRendererName($rendererName);
$rendererName = \PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF::class;
\PhpOffice\PhpSpreadsheet\Settings::setDefaultPdfWriter($rendererName);
```
## PclZip and ZipArchive

View File

@ -745,11 +745,11 @@ of different libraries.
Currently, the following libraries are supported:
Library | Downloadable from | PhpSpreadsheet Internal Constant
--------|-------------------------------------|---------------------------------
tcPDF | https://github.com/tecnickcom/tcpdf | PDF_RENDERER_TCPDF
mPDF | https://github.com/mpdf/mpdf | PDF_RENDERER_MPDF
domPDF | https://github.com/dompdf/dompdf | PDF_RENDERER_DOMPDF
Library | Downloadable from | PhpSpreadsheet writer
--------|-------------------------------------|----------------------
tcPDF | https://github.com/tecnickcom/tcpdf | TcPdf
mPDF | https://github.com/mpdf/mpdf | MPDF
domPDF | https://github.com/dompdf/dompdf | DomPDF
The different libraries have different strengths and weaknesses. Some
generate better formatted output than others, some are faster or use
@ -757,12 +757,19 @@ less memory than others, while some generate smaller .pdf files. It is
the developers choice which one they wish to use, appropriate to their
own circumstances.
Before instantiating a Writer to generate PDF output, you will need to
indicate which Rendering library you are using.
Before instantiating a Writer via `IOFactory` to generate PDF output,
you will need to indicate which writer you are using:
``` php
$rendererName = \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_MPDF;
\PhpOffice\PhpSpreadsheet\Settings::setPdfRendererName($rendererName);
$rendererName = \PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF::class;
\PhpOffice\PhpSpreadsheet\Settings::setDefaultPdfWriter($rendererName);
$writer = \PhpOffice\PhpSpreadsheet\IOFactory\IOFactory::createWriter($spreadsheet, 'Pdf');
```
Or you can instantiate directly the writer of your choice like so:
``` php
$writer = \PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF($spreadsheet);
```
#### Writing a spreadsheet
@ -771,11 +778,11 @@ Once you have identified the Renderer that you wish to use for PDF
generation, you can write a .pdf file using the following code:
``` php
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf($spreadsheet);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF($spreadsheet);
$writer->save("05featuredemo.pdf");
```
Please note that \PhpOffice\PhpSpreadsheet\Writer\Pdf only outputs the
Please note that `\PhpOffice\PhpSpreadsheet\Writer\Pdf` only outputs the
first worksheet by default.
#### Write all worksheets
@ -803,7 +810,7 @@ This can be slow on large spreadsheets, and maybe even unwanted. You can
however disable formula pre-calculation:
``` php
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf($spreadsheet);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF($spreadsheet);
$writer->setPreCalculateFormulas(false);
$writer->save("05featuredemo.pdf");

View File

@ -16,9 +16,7 @@ if ($helper->isCli()) {
// Change these values to select the Rendering library that you wish to use
// and its directory location on your server
//$rendererName = \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_TCPDF;
$rendererName = Settings::PDF_RENDERER_MPDF;
//$rendererName = \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_DOMPDF;
$rendererName = \PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF::class;
// Create new Spreadsheet object
$spreadsheet = new Spreadsheet();
@ -51,7 +49,7 @@ $spreadsheet->getActiveSheet()->setShowGridLines(false);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$spreadsheet->setActiveSheetIndex(0);
Settings::setPdfRendererName($rendererName);
Settings::setDefaultPdfWriter($rendererName);
// Redirect output to a clients web browser (PDF)
header('Content-Type: application/pdf');

View File

@ -7,9 +7,7 @@ require __DIR__ . '/../Header.php';
// Change these values to select the PDF Rendering library that you wish to use
// and its directory location on your server
//$rendererName = \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_TCPDF;
//$rendererName = \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_MPDF;
$rendererName = Settings::PDF_RENDERER_DOMPDF;
$rendererName = \PhpOffice\PhpSpreadsheet\Writer\Pdf\DomPDF::class;
// Read from Xlsx (.xlsx) template
$helper->log('Load Xlsx template file');
@ -21,7 +19,7 @@ $helper->write($spreadsheet, __FILE__, ['Xlsx', 'Xls', 'Html']);
// Export to PDF (.pdf)
$helper->log('Write to PDF format');
Settings::setPdfRendererName($rendererName);
Settings::setDefaultPdfWriter($rendererName);
$helper->write($spreadsheet, __FILE__, ['Pdf']);
// Remove first two rows with field headers before exporting to CSV

View File

@ -7,11 +7,9 @@ require __DIR__ . '/../Header.php';
// Change these values to select the Rendering library that you wish to use
// for PDF files, and its directory location on your server
//$rendererName = \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_TCPDF;
$rendererName = Settings::PDF_RENDERER_MPDF;
//$rendererName = \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_DOMPDF;
$rendererName = \PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF::class;
Settings::setPdfRendererName($rendererName);
Settings::setDefaultPdfWriter($rendererName);
// Change these values to select the Rendering library that you wish to use
// for Chart images, and its directory location on your server

View File

@ -12,9 +12,9 @@ $spreadsheet->getActiveSheet()->setShowGridLines(false);
$helper->log('Set orientation to landscape');
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
$rendererName = Settings::PDF_RENDERER_DOMPDF;
$rendererName = \PhpOffice\PhpSpreadsheet\Writer\Pdf\DomPDF::class;
$helper->log("Write to PDF format using {$rendererName}");
Settings::setPdfRendererName($rendererName);
Settings::setDefaultPdfWriter($rendererName);
// Save
$helper->write($spreadsheet, __FILE__, ['Pdf']);

View File

@ -12,9 +12,9 @@ $spreadsheet->getActiveSheet()->setShowGridLines(false);
$helper->log('Set orientation to landscape');
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
$rendererName = Settings::PDF_RENDERER_TCPDF;
$rendererName = \PhpOffice\PhpSpreadsheet\Writer\Pdf\TcPDF::class;
$helper->log("Write to PDF format using {$rendererName}");
Settings::setPdfRendererName($rendererName);
Settings::setDefaultPdfWriter($rendererName);
// Save
$helper->write($spreadsheet, __FILE__, ['Pdf']);

View File

@ -12,9 +12,9 @@ $spreadsheet->getActiveSheet()->setShowGridLines(false);
$helper->log('Set orientation to landscape');
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
$rendererName = Settings::PDF_RENDERER_MPDF;
$rendererName = \PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF::class;
$helper->log("Write to PDF format using {$rendererName}");
Settings::setPdfRendererName($rendererName);
Settings::setDefaultPdfWriter($rendererName);
// Save
$helper->write($spreadsheet, __FILE__, ['Pdf']);

View File

@ -59,7 +59,7 @@ class Migrator
'PHPExcel_Writer_OpenDocument_Styles' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Styles::class,
'PHPExcel_Writer_OpenDocument_Thumbnails' => \PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails::class,
'PHPExcel_Writer_OpenDocument_WriterPart' => \PhpOffice\PhpSpreadsheet\Writer\Ods\WriterPart::class,
'PHPExcel_Writer_PDF_Core' => \PhpOffice\PhpSpreadsheet\Writer\Pdf\Core::class,
'PHPExcel_Writer_PDF_Core' => \PhpOffice\PhpSpreadsheet\Writer\Pdf::class,
'PHPExcel_Writer_PDF_DomPDF' => \PhpOffice\PhpSpreadsheet\Writer\Pdf\DomPDF::class,
'PHPExcel_Writer_PDF_mPDF' => \PhpOffice\PhpSpreadsheet\Writer\Pdf\MPDF::class,
'PHPExcel_Writer_PDF_tcPDF' => \PhpOffice\PhpSpreadsheet\Writer\Pdf\TcPDF::class,

View File

@ -98,6 +98,15 @@ class IOFactory
// Search type
$searchType = 'IWriter';
if ($writerType === 'Pdf') {
$pdfWriter = Settings::getDefaultPdfWriter();
if ($pdfWriter === null) {
throw new Exception('PDF default writer has not been defined.');
}
return new $pdfWriter($spreadsheet);
}
// Include class
foreach (self::$searchLocations as $searchLocation) {
if ($searchLocation['type'] == $searchType) {

View File

@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheet;
use PhpOffice\PhpSpreadsheet\Collection\Memory;
use PhpOffice\PhpSpreadsheet\Writer\Pdf;
use Psr\SimpleCache\CacheInterface;
class Settings
@ -10,19 +11,9 @@ class Settings
/** Optional Chart Rendering libraries */
const CHART_RENDERER_JPGRAPH = 'JpGraph';
/** Optional PDF Rendering libraries */
const PDF_RENDERER_TCPDF = 'TcPDF';
const PDF_RENDERER_DOMPDF = 'DomPDF';
const PDF_RENDERER_MPDF = 'MPDF';
private static $chartRenderers = [
self::CHART_RENDERER_JPGRAPH,
];
private static $pdfRenderers = [
self::PDF_RENDERER_TCPDF,
self::PDF_RENDERER_DOMPDF,
self::PDF_RENDERER_MPDF,
];
/**
* Name of the external Library used for rendering charts
@ -47,7 +38,7 @@ class Settings
*
* @var string
*/
private static $pdfRendererName;
private static $defaultPdfWriter;
/**
* Default options for libxml loader.
@ -152,33 +143,27 @@ class Settings
}
/**
* Identify to PhpSpreadsheet the external library to use for rendering PDF files.
* Identify to PhpSpreadsheet the default writer to use for PDF.
*
* @param string $libraryName Internal reference name of the library
* e.g. \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_TCPDF,
* \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_DOMPDF
* or \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_MPDF
* @param string $writerClassName Internal reference name of the library
*/
public static function setPdfRendererName($libraryName)
public static function setDefaultPdfWriter($writerClassName)
{
if (!in_array($libraryName, self::$pdfRenderers)) {
throw new Exception('"' . $libraryName . '" is not a valid PDF library name');
if (!is_a($writerClassName, Pdf::class, true)) {
throw new Exception('"' . $writerClassName . '" is not a valid PDF writer class name');
}
self::$pdfRendererName = $libraryName;
self::$defaultPdfWriter = $writerClassName;
}
/**
* Return the PDF Rendering Library that PhpSpreadsheet is currently configured to use (e.g. dompdf).
* Return the default PDF writer that PhpSpreadsheet is currently configured to use (e.g. dompdf).
*
* @return null|string Internal reference name of the PDF Rendering Library that PhpSpreadsheet is
* currently configured to use
* e.g. \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_TCPDF,
* \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_DOMPDF
* or \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_MPDF
*/
public static function getPdfRendererName()
public static function getDefaultPdfWriter()
{
return self::$pdfRendererName;
return self::$defaultPdfWriter;
}
/**

View File

@ -2,58 +2,280 @@
namespace PhpOffice\PhpSpreadsheet\Writer;
use PhpOffice\PhpSpreadsheet\Settings;
use PhpOffice\PhpSpreadsheet\Calculation;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
class Pdf implements IWriter
abstract class Pdf extends Html implements IWriter
{
/**
* The wrapper for the requested PDF rendering engine.
* Temporary storage directory.
*
* @var PDF\Core
* @var string
*/
private $renderer;
protected $tempDir = '';
/**
* Instantiate a new renderer of the configured type within this container class.
* Font.
*
* @param Spreadsheet $spreadsheet PhpSpreadsheet object
* @var string
*/
protected $font = 'freesans';
/**
* Orientation (Over-ride).
*
* @throws Exception when PDF library is not configured
* @var string
*/
protected $orientation;
/**
* Paper size (Over-ride).
*
* @var int
*/
protected $paperSize;
/**
* Temporary storage for Save Array Return type.
*
* @var string
*/
private $saveArrayReturnType;
/**
* Paper Sizes xRef List.
*
* @var array
*/
protected static $paperSizes = [
PageSetup::PAPERSIZE_LETTER => 'LETTER', // (8.5 in. by 11 in.)
PageSetup::PAPERSIZE_LETTER_SMALL => 'LETTER', // (8.5 in. by 11 in.)
PageSetup::PAPERSIZE_TABLOID => [792.00, 1224.00], // (11 in. by 17 in.)
PageSetup::PAPERSIZE_LEDGER => [1224.00, 792.00], // (17 in. by 11 in.)
PageSetup::PAPERSIZE_LEGAL => 'LEGAL', // (8.5 in. by 14 in.)
PageSetup::PAPERSIZE_STATEMENT => [396.00, 612.00], // (5.5 in. by 8.5 in.)
PageSetup::PAPERSIZE_EXECUTIVE => 'EXECUTIVE', // (7.25 in. by 10.5 in.)
PageSetup::PAPERSIZE_A3 => 'A3', // (297 mm by 420 mm)
PageSetup::PAPERSIZE_A4 => 'A4', // (210 mm by 297 mm)
PageSetup::PAPERSIZE_A4_SMALL => 'A4', // (210 mm by 297 mm)
PageSetup::PAPERSIZE_A5 => 'A5', // (148 mm by 210 mm)
PageSetup::PAPERSIZE_B4 => 'B4', // (250 mm by 353 mm)
PageSetup::PAPERSIZE_B5 => 'B5', // (176 mm by 250 mm)
PageSetup::PAPERSIZE_FOLIO => 'FOLIO', // (8.5 in. by 13 in.)
PageSetup::PAPERSIZE_QUARTO => [609.45, 779.53], // (215 mm by 275 mm)
PageSetup::PAPERSIZE_STANDARD_1 => [720.00, 1008.00], // (10 in. by 14 in.)
PageSetup::PAPERSIZE_STANDARD_2 => [792.00, 1224.00], // (11 in. by 17 in.)
PageSetup::PAPERSIZE_NOTE => 'LETTER', // (8.5 in. by 11 in.)
PageSetup::PAPERSIZE_NO9_ENVELOPE => [279.00, 639.00], // (3.875 in. by 8.875 in.)
PageSetup::PAPERSIZE_NO10_ENVELOPE => [297.00, 684.00], // (4.125 in. by 9.5 in.)
PageSetup::PAPERSIZE_NO11_ENVELOPE => [324.00, 747.00], // (4.5 in. by 10.375 in.)
PageSetup::PAPERSIZE_NO12_ENVELOPE => [342.00, 792.00], // (4.75 in. by 11 in.)
PageSetup::PAPERSIZE_NO14_ENVELOPE => [360.00, 828.00], // (5 in. by 11.5 in.)
PageSetup::PAPERSIZE_C => [1224.00, 1584.00], // (17 in. by 22 in.)
PageSetup::PAPERSIZE_D => [1584.00, 2448.00], // (22 in. by 34 in.)
PageSetup::PAPERSIZE_E => [2448.00, 3168.00], // (34 in. by 44 in.)
PageSetup::PAPERSIZE_DL_ENVELOPE => [311.81, 623.62], // (110 mm by 220 mm)
PageSetup::PAPERSIZE_C5_ENVELOPE => 'C5', // (162 mm by 229 mm)
PageSetup::PAPERSIZE_C3_ENVELOPE => 'C3', // (324 mm by 458 mm)
PageSetup::PAPERSIZE_C4_ENVELOPE => 'C4', // (229 mm by 324 mm)
PageSetup::PAPERSIZE_C6_ENVELOPE => 'C6', // (114 mm by 162 mm)
PageSetup::PAPERSIZE_C65_ENVELOPE => [323.15, 649.13], // (114 mm by 229 mm)
PageSetup::PAPERSIZE_B4_ENVELOPE => 'B4', // (250 mm by 353 mm)
PageSetup::PAPERSIZE_B5_ENVELOPE => 'B5', // (176 mm by 250 mm)
PageSetup::PAPERSIZE_B6_ENVELOPE => [498.90, 354.33], // (176 mm by 125 mm)
PageSetup::PAPERSIZE_ITALY_ENVELOPE => [311.81, 651.97], // (110 mm by 230 mm)
PageSetup::PAPERSIZE_MONARCH_ENVELOPE => [279.00, 540.00], // (3.875 in. by 7.5 in.)
PageSetup::PAPERSIZE_6_3_4_ENVELOPE => [261.00, 468.00], // (3.625 in. by 6.5 in.)
PageSetup::PAPERSIZE_US_STANDARD_FANFOLD => [1071.00, 792.00], // (14.875 in. by 11 in.)
PageSetup::PAPERSIZE_GERMAN_STANDARD_FANFOLD => [612.00, 864.00], // (8.5 in. by 12 in.)
PageSetup::PAPERSIZE_GERMAN_LEGAL_FANFOLD => 'FOLIO', // (8.5 in. by 13 in.)
PageSetup::PAPERSIZE_ISO_B4 => 'B4', // (250 mm by 353 mm)
PageSetup::PAPERSIZE_JAPANESE_DOUBLE_POSTCARD => [566.93, 419.53], // (200 mm by 148 mm)
PageSetup::PAPERSIZE_STANDARD_PAPER_1 => [648.00, 792.00], // (9 in. by 11 in.)
PageSetup::PAPERSIZE_STANDARD_PAPER_2 => [720.00, 792.00], // (10 in. by 11 in.)
PageSetup::PAPERSIZE_STANDARD_PAPER_3 => [1080.00, 792.00], // (15 in. by 11 in.)
PageSetup::PAPERSIZE_INVITE_ENVELOPE => [623.62, 623.62], // (220 mm by 220 mm)
PageSetup::PAPERSIZE_LETTER_EXTRA_PAPER => [667.80, 864.00], // (9.275 in. by 12 in.)
PageSetup::PAPERSIZE_LEGAL_EXTRA_PAPER => [667.80, 1080.00], // (9.275 in. by 15 in.)
PageSetup::PAPERSIZE_TABLOID_EXTRA_PAPER => [841.68, 1296.00], // (11.69 in. by 18 in.)
PageSetup::PAPERSIZE_A4_EXTRA_PAPER => [668.98, 912.76], // (236 mm by 322 mm)
PageSetup::PAPERSIZE_LETTER_TRANSVERSE_PAPER => [595.80, 792.00], // (8.275 in. by 11 in.)
PageSetup::PAPERSIZE_A4_TRANSVERSE_PAPER => 'A4', // (210 mm by 297 mm)
PageSetup::PAPERSIZE_LETTER_EXTRA_TRANSVERSE_PAPER => [667.80, 864.00], // (9.275 in. by 12 in.)
PageSetup::PAPERSIZE_SUPERA_SUPERA_A4_PAPER => [643.46, 1009.13], // (227 mm by 356 mm)
PageSetup::PAPERSIZE_SUPERB_SUPERB_A3_PAPER => [864.57, 1380.47], // (305 mm by 487 mm)
PageSetup::PAPERSIZE_LETTER_PLUS_PAPER => [612.00, 913.68], // (8.5 in. by 12.69 in.)
PageSetup::PAPERSIZE_A4_PLUS_PAPER => [595.28, 935.43], // (210 mm by 330 mm)
PageSetup::PAPERSIZE_A5_TRANSVERSE_PAPER => 'A5', // (148 mm by 210 mm)
PageSetup::PAPERSIZE_JIS_B5_TRANSVERSE_PAPER => [515.91, 728.50], // (182 mm by 257 mm)
PageSetup::PAPERSIZE_A3_EXTRA_PAPER => [912.76, 1261.42], // (322 mm by 445 mm)
PageSetup::PAPERSIZE_A5_EXTRA_PAPER => [493.23, 666.14], // (174 mm by 235 mm)
PageSetup::PAPERSIZE_ISO_B5_EXTRA_PAPER => [569.76, 782.36], // (201 mm by 276 mm)
PageSetup::PAPERSIZE_A2_PAPER => 'A2', // (420 mm by 594 mm)
PageSetup::PAPERSIZE_A3_TRANSVERSE_PAPER => 'A3', // (297 mm by 420 mm)
PageSetup::PAPERSIZE_A3_EXTRA_TRANSVERSE_PAPER => [912.76, 1261.42], // (322 mm by 445 mm)
];
/**
* Create a new PDF Writer instance.
*
* @param Spreadsheet $spreadsheet Spreadsheet object
*/
public function __construct(Spreadsheet $spreadsheet)
{
$pdfLibraryName = Settings::getPdfRendererName();
if ($pdfLibraryName === null) {
throw new Exception('PDF Rendering library has not been defined.');
}
$rendererName = '\\PhpOffice\\PhpSpreadsheet\\Writer\\Pdf\\' . $pdfLibraryName;
$this->renderer = new $rendererName($spreadsheet);
parent::__construct($spreadsheet);
$this->setUseInlineCss(true);
$this->tempDir = File::sysGetTempDir();
}
/**
* Magic method to handle direct calls to the configured PDF renderer wrapper class.
* Get Font.
*
* @param string $name Renderer library method name
* @param mixed[] $arguments Array of arguments to pass to the renderer method
*
* @return mixed Returned data from the PDF renderer wrapper method
* @return string
*/
public function __call($name, $arguments)
public function getFont()
{
if ($this->renderer === null) {
throw new Exception('PDF Rendering library has not been defined.');
}
return call_user_func_array([$this->renderer, $name], $arguments);
return $this->font;
}
/**
* {@inheritdoc}
* Set font. Examples:
* 'arialunicid0-chinese-simplified'
* 'arialunicid0-chinese-traditional'
* 'arialunicid0-korean'
* 'arialunicid0-japanese'.
*
* @param string $fontName
*/
public function save($pFilename)
public function setFont($fontName)
{
$this->renderer->save($pFilename);
$this->font = $fontName;
return $this;
}
/**
* Get Paper Size.
*
* @return int
*/
public function getPaperSize()
{
return $this->paperSize;
}
/**
* Set Paper Size.
*
* @param string $pValue Paper size see PageSetup::PAPERSIZE_*
*
* @return self
*/
public function setPaperSize($pValue)
{
$this->paperSize = $pValue;
return $this;
}
/**
* Get Orientation.
*
* @return string
*/
public function getOrientation()
{
return $this->orientation;
}
/**
* Set Orientation.
*
* @param string $pValue Page orientation see PageSetup::ORIENTATION_*
*
* @return self
*/
public function setOrientation($pValue)
{
$this->orientation = $pValue;
return $this;
}
/**
* Get temporary storage directory.
*
* @return string
*/
public function getTempDir()
{
return $this->tempDir;
}
/**
* Set temporary storage directory.
*
* @param string $pValue Temporary storage directory
*
* @throws WriterException when directory does not exist
*
* @return self
*/
public function setTempDir($pValue)
{
if (is_dir($pValue)) {
$this->tempDir = $pValue;
} else {
throw new WriterException("Directory does not exist: $pValue");
}
return $this;
}
/**
* Save Spreadsheet to PDF file, pre-save.
*
* @param string $pFilename Name of the file to save as
*
* @throws WriterException
*/
protected function prepareForSave($pFilename)
{
// garbage collect
$this->spreadsheet->garbageCollect();
$this->saveArrayReturnType = Calculation::getArrayReturnType();
Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE);
// Open file
$fileHandle = fopen($pFilename, 'w');
if ($fileHandle === false) {
throw new WriterException("Could not open file $pFilename for writing.");
}
// Set PDF
$this->isPdf = true;
// Build CSS
$this->buildCSS(true);
return $fileHandle;
}
/**
* Save PhpSpreadsheet to PDF file, post-save.
*
* @param resource $fileHandle
*
* @throws WriterException
*/
protected function restoreStateAfterSave($fileHandle)
{
// Close file
fclose($fileHandle);
Calculation::setArrayReturnType($this->saveArrayReturnType);
}
}

View File

@ -1,282 +0,0 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
use PhpOffice\PhpSpreadsheet\Calculation;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
use PhpOffice\PhpSpreadsheet\Writer\Html;
abstract class Core extends Html
{
/**
* Temporary storage directory.
*
* @var string
*/
protected $tempDir = '';
/**
* Font.
*
* @var string
*/
protected $font = 'freesans';
/**
* Orientation (Over-ride).
*
* @var string
*/
protected $orientation;
/**
* Paper size (Over-ride).
*
* @var int
*/
protected $paperSize;
/**
* Temporary storage for Save Array Return type.
*
* @var string
*/
private $saveArrayReturnType;
/**
* Paper Sizes xRef List.
*
* @var array
*/
protected static $paperSizes = [
PageSetup::PAPERSIZE_LETTER => 'LETTER', // (8.5 in. by 11 in.)
PageSetup::PAPERSIZE_LETTER_SMALL => 'LETTER', // (8.5 in. by 11 in.)
PageSetup::PAPERSIZE_TABLOID => [792.00, 1224.00], // (11 in. by 17 in.)
PageSetup::PAPERSIZE_LEDGER => [1224.00, 792.00], // (17 in. by 11 in.)
PageSetup::PAPERSIZE_LEGAL => 'LEGAL', // (8.5 in. by 14 in.)
PageSetup::PAPERSIZE_STATEMENT => [396.00, 612.00], // (5.5 in. by 8.5 in.)
PageSetup::PAPERSIZE_EXECUTIVE => 'EXECUTIVE', // (7.25 in. by 10.5 in.)
PageSetup::PAPERSIZE_A3 => 'A3', // (297 mm by 420 mm)
PageSetup::PAPERSIZE_A4 => 'A4', // (210 mm by 297 mm)
PageSetup::PAPERSIZE_A4_SMALL => 'A4', // (210 mm by 297 mm)
PageSetup::PAPERSIZE_A5 => 'A5', // (148 mm by 210 mm)
PageSetup::PAPERSIZE_B4 => 'B4', // (250 mm by 353 mm)
PageSetup::PAPERSIZE_B5 => 'B5', // (176 mm by 250 mm)
PageSetup::PAPERSIZE_FOLIO => 'FOLIO', // (8.5 in. by 13 in.)
PageSetup::PAPERSIZE_QUARTO => [609.45, 779.53], // (215 mm by 275 mm)
PageSetup::PAPERSIZE_STANDARD_1 => [720.00, 1008.00], // (10 in. by 14 in.)
PageSetup::PAPERSIZE_STANDARD_2 => [792.00, 1224.00], // (11 in. by 17 in.)
PageSetup::PAPERSIZE_NOTE => 'LETTER', // (8.5 in. by 11 in.)
PageSetup::PAPERSIZE_NO9_ENVELOPE => [279.00, 639.00], // (3.875 in. by 8.875 in.)
PageSetup::PAPERSIZE_NO10_ENVELOPE => [297.00, 684.00], // (4.125 in. by 9.5 in.)
PageSetup::PAPERSIZE_NO11_ENVELOPE => [324.00, 747.00], // (4.5 in. by 10.375 in.)
PageSetup::PAPERSIZE_NO12_ENVELOPE => [342.00, 792.00], // (4.75 in. by 11 in.)
PageSetup::PAPERSIZE_NO14_ENVELOPE => [360.00, 828.00], // (5 in. by 11.5 in.)
PageSetup::PAPERSIZE_C => [1224.00, 1584.00], // (17 in. by 22 in.)
PageSetup::PAPERSIZE_D => [1584.00, 2448.00], // (22 in. by 34 in.)
PageSetup::PAPERSIZE_E => [2448.00, 3168.00], // (34 in. by 44 in.)
PageSetup::PAPERSIZE_DL_ENVELOPE => [311.81, 623.62], // (110 mm by 220 mm)
PageSetup::PAPERSIZE_C5_ENVELOPE => 'C5', // (162 mm by 229 mm)
PageSetup::PAPERSIZE_C3_ENVELOPE => 'C3', // (324 mm by 458 mm)
PageSetup::PAPERSIZE_C4_ENVELOPE => 'C4', // (229 mm by 324 mm)
PageSetup::PAPERSIZE_C6_ENVELOPE => 'C6', // (114 mm by 162 mm)
PageSetup::PAPERSIZE_C65_ENVELOPE => [323.15, 649.13], // (114 mm by 229 mm)
PageSetup::PAPERSIZE_B4_ENVELOPE => 'B4', // (250 mm by 353 mm)
PageSetup::PAPERSIZE_B5_ENVELOPE => 'B5', // (176 mm by 250 mm)
PageSetup::PAPERSIZE_B6_ENVELOPE => [498.90, 354.33], // (176 mm by 125 mm)
PageSetup::PAPERSIZE_ITALY_ENVELOPE => [311.81, 651.97], // (110 mm by 230 mm)
PageSetup::PAPERSIZE_MONARCH_ENVELOPE => [279.00, 540.00], // (3.875 in. by 7.5 in.)
PageSetup::PAPERSIZE_6_3_4_ENVELOPE => [261.00, 468.00], // (3.625 in. by 6.5 in.)
PageSetup::PAPERSIZE_US_STANDARD_FANFOLD => [1071.00, 792.00], // (14.875 in. by 11 in.)
PageSetup::PAPERSIZE_GERMAN_STANDARD_FANFOLD => [612.00, 864.00], // (8.5 in. by 12 in.)
PageSetup::PAPERSIZE_GERMAN_LEGAL_FANFOLD => 'FOLIO', // (8.5 in. by 13 in.)
PageSetup::PAPERSIZE_ISO_B4 => 'B4', // (250 mm by 353 mm)
PageSetup::PAPERSIZE_JAPANESE_DOUBLE_POSTCARD => [566.93, 419.53], // (200 mm by 148 mm)
PageSetup::PAPERSIZE_STANDARD_PAPER_1 => [648.00, 792.00], // (9 in. by 11 in.)
PageSetup::PAPERSIZE_STANDARD_PAPER_2 => [720.00, 792.00], // (10 in. by 11 in.)
PageSetup::PAPERSIZE_STANDARD_PAPER_3 => [1080.00, 792.00], // (15 in. by 11 in.)
PageSetup::PAPERSIZE_INVITE_ENVELOPE => [623.62, 623.62], // (220 mm by 220 mm)
PageSetup::PAPERSIZE_LETTER_EXTRA_PAPER => [667.80, 864.00], // (9.275 in. by 12 in.)
PageSetup::PAPERSIZE_LEGAL_EXTRA_PAPER => [667.80, 1080.00], // (9.275 in. by 15 in.)
PageSetup::PAPERSIZE_TABLOID_EXTRA_PAPER => [841.68, 1296.00], // (11.69 in. by 18 in.)
PageSetup::PAPERSIZE_A4_EXTRA_PAPER => [668.98, 912.76], // (236 mm by 322 mm)
PageSetup::PAPERSIZE_LETTER_TRANSVERSE_PAPER => [595.80, 792.00], // (8.275 in. by 11 in.)
PageSetup::PAPERSIZE_A4_TRANSVERSE_PAPER => 'A4', // (210 mm by 297 mm)
PageSetup::PAPERSIZE_LETTER_EXTRA_TRANSVERSE_PAPER => [667.80, 864.00], // (9.275 in. by 12 in.)
PageSetup::PAPERSIZE_SUPERA_SUPERA_A4_PAPER => [643.46, 1009.13], // (227 mm by 356 mm)
PageSetup::PAPERSIZE_SUPERB_SUPERB_A3_PAPER => [864.57, 1380.47], // (305 mm by 487 mm)
PageSetup::PAPERSIZE_LETTER_PLUS_PAPER => [612.00, 913.68], // (8.5 in. by 12.69 in.)
PageSetup::PAPERSIZE_A4_PLUS_PAPER => [595.28, 935.43], // (210 mm by 330 mm)
PageSetup::PAPERSIZE_A5_TRANSVERSE_PAPER => 'A5', // (148 mm by 210 mm)
PageSetup::PAPERSIZE_JIS_B5_TRANSVERSE_PAPER => [515.91, 728.50], // (182 mm by 257 mm)
PageSetup::PAPERSIZE_A3_EXTRA_PAPER => [912.76, 1261.42], // (322 mm by 445 mm)
PageSetup::PAPERSIZE_A5_EXTRA_PAPER => [493.23, 666.14], // (174 mm by 235 mm)
PageSetup::PAPERSIZE_ISO_B5_EXTRA_PAPER => [569.76, 782.36], // (201 mm by 276 mm)
PageSetup::PAPERSIZE_A2_PAPER => 'A2', // (420 mm by 594 mm)
PageSetup::PAPERSIZE_A3_TRANSVERSE_PAPER => 'A3', // (297 mm by 420 mm)
PageSetup::PAPERSIZE_A3_EXTRA_TRANSVERSE_PAPER => [912.76, 1261.42], // (322 mm by 445 mm)
];
/**
* Create a new PDF Writer instance.
*
* @param Spreadsheet $spreadsheet Spreadsheet object
*/
public function __construct(Spreadsheet $spreadsheet)
{
parent::__construct($spreadsheet);
$this->setUseInlineCss(true);
$this->tempDir = File::sysGetTempDir();
}
/**
* Get Font.
*
* @return string
*/
public function getFont()
{
return $this->font;
}
/**
* Set font. Examples:
* 'arialunicid0-chinese-simplified'
* 'arialunicid0-chinese-traditional'
* 'arialunicid0-korean'
* 'arialunicid0-japanese'.
*
* @param string $fontName
*/
public function setFont($fontName)
{
$this->font = $fontName;
return $this;
}
/**
* Get Paper Size.
*
* @return int
*/
public function getPaperSize()
{
return $this->paperSize;
}
/**
* Set Paper Size.
*
* @param string $pValue Paper size see PageSetup::PAPERSIZE_*
*
* @return self
*/
public function setPaperSize($pValue)
{
$this->paperSize = $pValue;
return $this;
}
/**
* Get Orientation.
*
* @return string
*/
public function getOrientation()
{
return $this->orientation;
}
/**
* Set Orientation.
*
* @param string $pValue Page orientation see PageSetup::ORIENTATION_*
*
* @return self
*/
public function setOrientation($pValue)
{
$this->orientation = $pValue;
return $this;
}
/**
* Get temporary storage directory.
*
* @return string
*/
public function getTempDir()
{
return $this->tempDir;
}
/**
* Set temporary storage directory.
*
* @param string $pValue Temporary storage directory
*
* @throws WriterException when directory does not exist
*
* @return self
*/
public function setTempDir($pValue)
{
if (is_dir($pValue)) {
$this->tempDir = $pValue;
} else {
throw new WriterException("Directory does not exist: $pValue");
}
return $this;
}
/**
* Save Spreadsheet to PDF file, pre-save.
*
* @param string $pFilename Name of the file to save as
*
* @throws WriterException
*/
protected function prepareForSave($pFilename)
{
// garbage collect
$this->spreadsheet->garbageCollect();
$this->saveArrayReturnType = Calculation::getArrayReturnType();
Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE);
// Open file
$fileHandle = fopen($pFilename, 'w');
if ($fileHandle === false) {
throw new WriterException("Could not open file $pFilename for writing.");
}
// Set PDF
$this->isPdf = true;
// Build CSS
$this->buildCSS(true);
return $fileHandle;
}
/**
* Save PhpSpreadsheet to PDF file, post-save.
*
* @param resource $fileHandle
*
* @throws WriterException
*/
protected function restoreStateAfterSave($fileHandle)
{
// Close file
fclose($fileHandle);
Calculation::setArrayReturnType($this->saveArrayReturnType);
}
}

View File

@ -4,8 +4,9 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
use PhpOffice\PhpSpreadsheet\Writer\Pdf;
class DomPDF extends Core implements IWriter
class DomPDF extends Pdf implements IWriter
{
/**
* Save Spreadsheet to file.

View File

@ -5,8 +5,9 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
use PhpOffice\PhpSpreadsheet\Writer\Pdf;
class MPDF extends Core implements IWriter
class MPDF extends Pdf implements IWriter
{
/**
* Save Spreadsheet to file.

View File

@ -4,8 +4,9 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
use PhpOffice\PhpSpreadsheet\Writer\Pdf;
class TcPDF extends Core implements IWriter
class TcPDF extends Pdf implements IWriter
{
/**
* Save Spreadsheet to file.