echo '</ul>'; exit; } $engine = isset($_GET['engine']) ? $_GET['engine'] : 'pdf'; // set different way of configuration //$facade = PHPPdf\Core\FacadeBuilder::create(new PHPPdf\Core\Configuration\DependencyInjection\LoaderImpl())//->setCache('File', array('cache_dir' => __DIR__.'/cache/')) $facade = PHPPdf\Core\FacadeBuilder::create()->setEngineType($engine)->setEngineOptions(array('format' => 'jpg', 'quality' => 70, 'engine' => 'imagick'))->build(); $name = basename($_GET['name']); $documentFilename = __DIR__ . '/' . $name . '.xml'; $stylesheetFilename = __DIR__ . '/' . $name . '-style.xml'; if (!is_readable($documentFilename)) { die(sprintf('Example "%s" dosn\'t exist.', $name)); } $xml = str_replace('dir:', __DIR__ . '/', file_get_contents($documentFilename)); $stylesheetXml = is_readable($stylesheetFilename) ? str_replace('dir:', __DIR__ . '/', file_get_contents($stylesheetFilename)) : null; $stylesheet = $stylesheetXml ? PHPPdf\DataSource\DataSource::fromString($stylesheetXml) : null; $start = microtime(true); $content = $facade->render($xml, $stylesheet); if (isset($_GET['t'])) { echo microtime(true) - $start . '<br />'; echo memory_get_peak_usage(true) / 1024 / 1024 . 'MB'; } else { if ($engine == 'pdf') { header('Content-Type: application/pdf'); echo $content; } else { foreach ($content as $data) { $data = base64_encode($data); echo '<img src="data:image/jpeg;base64,' . $data . '" />'; } }
set_time_limit(240); require_once __DIR__ . '/get_examples.php'; require_once __DIR__ . '/../lib/PHPPdf/Autoloader.php'; PHPPdf\Autoloader::register(); PHPPdf\Autoloader::register(__DIR__ . '/../lib/vendor/Zend/library'); // set different way of configuration //$facade = PHPPdf\Core\FacadeBuilder::create(new PHPPdf\Core\Configuration\DependencyInjection\LoaderImpl())->setCache('File', array('cache_dir' => __DIR__.'/cache/')) $facade = PHPPdf\Core\FacadeBuilder::create()->build(); if ($_SERVER['argc'] < 3) { echo 'Pass example name and destination file path, for example `cli.php example-name /some/destination/file.pdf`' . PHP_EOL; echo 'Available examples:' . PHP_EOL; $examples = get_examples(); die(implode(PHP_EOL, $examples)); } $name = basename($_SERVER['argv'][1]); $destinationPath = $_SERVER['argv'][2]; $documentFilename = __DIR__ . '/' . $name . '.xml'; $stylesheetFilename = __DIR__ . '/' . $name . '-style.xml'; if (!is_readable($documentFilename) || !is_readable($stylesheetFilename)) { die(sprintf('Example "%s" dosn\'t exist.', $name)); } if (!is_writable(dirname($destinationPath))) { die(sprintf('"%s" isn\'t writable.', $destinationPath)); } $xml = str_replace('dir:', __DIR__ . '/', file_get_contents($documentFilename)); $stylesheetXml = str_replace('dir:', __DIR__ . '/', file_get_contents($stylesheetFilename)); $stylesheet = PHPPdf\DataSource\DataSource::fromString($stylesheetXml); $start = microtime(true); $content = $facade->render($xml, $stylesheet); echo 'time: ' . (microtime(true) - $start) . 's'; file_put_contents($destinationPath, $content);