| 1 |
<?php |
| 2 |
|
| 3 |
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf; |
| 4 |
|
| 5 |
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 6 |
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
| 7 |
use PhpOffice\PhpSpreadsheet\Writer\Pdf; |
| 8 |
|
| 9 |
class Tcpdf extends Pdf |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Create a new PDF Writer instance. |
| 13 |
* |
| 14 |
* @param Spreadsheet $spreadsheet Spreadsheet object |
| 15 |
*/ |
| 16 |
public function __construct(Spreadsheet $spreadsheet) |
| 17 |
{ |
| 18 |
parent::__construct($spreadsheet); |
| 19 |
$this->setUseInlineCss(true); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Gets the implementation of external PDF library that should be used. |
| 24 |
* |
| 25 |
* @param string $orientation Page orientation |
| 26 |
* @param string $unit Unit measure |
| 27 |
* @param array|string $paperSize Paper size |
| 28 |
* |
| 29 |
* @return \TCPDF implementation |
| 30 |
*/ |
| 31 |
protected function createExternalWriterInstance($orientation, $unit, $paperSize) |
| 32 |
{ |
| 33 |
return new \TCPDF($orientation, $unit, $paperSize); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Save Spreadsheet to file. |
| 38 |
* |
| 39 |
* @param string $filename Name of the file to save as |
| 40 |
*/ |
| 41 |
public function save($filename, int $flags = 0): void |
| 42 |
{ |
| 43 |
$fileHandle = parent::prepareForSave($filename); |
| 44 |
|
| 45 |
// Default PDF paper size |
| 46 |
$paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.) |
| 47 |
|
| 48 |
// Check for paper size and page orientation |
| 49 |
$setup = $this->spreadsheet->getSheet($this->getSheetIndex() ?? 0)->getPageSetup(); |
| 50 |
$orientation = $this->getOrientation() ?? $setup->getOrientation(); |
| 51 |
$orientation = ($orientation === PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P'; |
| 52 |
$printPaperSize = $this->getPaperSize() ?? $setup->getPaperSize(); |
| 53 |
$paperSize = self::$paperSizes[$printPaperSize] ?? PageSetup::getPaperSizeDefault(); |
| 54 |
$printMargins = $this->spreadsheet->getSheet($this->getSheetIndex() ?? 0)->getPageMargins(); |
| 55 |
|
| 56 |
// Create PDF |
| 57 |
$pdf = $this->createExternalWriterInstance($orientation, 'pt', $paperSize); |
| 58 |
$pdf->setFontSubsetting(false); |
| 59 |
// Set margins, converting inches to points (using 72 dpi) |
| 60 |
$pdf->SetMargins($printMargins->getLeft() * 72, $printMargins->getTop() * 72, $printMargins->getRight() * 72); |
| 61 |
$pdf->SetAutoPageBreak(true, $printMargins->getBottom() * 72); |
| 62 |
|
| 63 |
$pdf->setPrintHeader(false); |
| 64 |
$pdf->setPrintFooter(false); |
| 65 |
|
| 66 |
$pdf->AddPage(); |
| 67 |
|
| 68 |
// Set the appropriate font |
| 69 |
$pdf->SetFont($this->getFont()); |
| 70 |
$pdf->writeHTML($this->generateHTMLAll()); |
| 71 |
|
| 72 |
// Document info |
| 73 |
$pdf->SetTitle($this->spreadsheet->getProperties()->getTitle()); |
| 74 |
$pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator()); |
| 75 |
$pdf->SetSubject($this->spreadsheet->getProperties()->getSubject()); |
| 76 |
$pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords()); |
| 77 |
$pdf->SetCreator($this->spreadsheet->getProperties()->getCreator()); |
| 78 |
|
| 79 |
// Write to file |
| 80 |
fwrite($fileHandle, $pdf->output('', 'S')); |
| 81 |
|
| 82 |
parent::restoreStateAfterSave(); |
| 83 |
} |
| 84 |
} |
| 85 |
|