71 lines
1.8 KiB
PHP
Raw Normal View History

2014-04-13 23:17:39 +07:00
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PhpWord
* @copyright 2014 PHPWord
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
2014-04-13 23:17:39 +07:00
*/
namespace PhpOffice\PhpWord\Writer\PDF;
2014-05-04 21:03:28 +04:00
use PhpOffice\PhpWord\Exception\Exception;
2014-04-13 23:17:39 +07:00
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
/**
* DomPDF writer
*/
2014-04-14 10:07:35 +07:00
class DomPDF extends AbstractRenderer implements \PhpOffice\PhpWord\Writer\WriterInterface
2014-04-13 23:17:39 +07:00
{
/**
* Create new instance
*
* @param PhpWord $phpWord PhpWord object
*/
public function __construct(PhpWord $phpWord)
{
parent::__construct($phpWord);
$configFile = Settings::getPdfRendererPath() . '/dompdf_config.inc.php';
if (file_exists($configFile)) {
require_once $configFile;
} else {
throw new Exception('Unable to load PDF Rendering library');
}
2014-04-13 23:17:39 +07:00
}
/**
* Save PhpWord to file
*
* @param string $pFilename Name of the file to save as
* @throws Exception
*/
public function save($pFilename = null)
{
$fileHandle = parent::prepareForSave($pFilename);
// Default PDF paper size
$paperSize = 'A4';
$orientation = 'P';
$printPaperSize = 9;
if (isset(self::$paperSizes[$printPaperSize])) {
$paperSize = self::$paperSizes[$printPaperSize];
}
$orientation = ($orientation == 'L') ? 'landscape' : 'portrait';
// Create PDF
$pdf = new \DOMPDF();
$pdf->set_paper(strtolower($paperSize), $orientation);
$pdf->load_html($this->writeDocument());
$pdf->render();
// Write to file
fwrite($fileHandle, $pdf->output());
parent::restoreStateAfterSave($fileHandle);
}
}