1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
<?php
namespace Dompdf\Tests\OutputTest;
use Dompdf\Options;
use Dompdf\Dompdf;
use SplFileInfo;
final class Dataset
{
/**
* @var string
*/
public $name;
/**
* @var SplFileInfo
*/
public $file;
/**
* @param string $name The name of the data set.
* @param SplFileInfo $file The HTML source file.
*/
public function __construct(string $name, SplFileInfo $file)
{
$this->name = $name;
$this->file = $file;
}
public function referenceFile(): SplFileInfo
{
$path = $this->file->getPath();
$name = $this->file->getBasename("." . $this->file->getExtension());
return new SplFileInfo("$path/$name.pdf");
}
protected function getDompdf(Options $options = null): Dompdf
{
return new Dompdf($this->getOptions($options));
}
protected function getOptions(Options $options = null): Options
{
$options = $options === null ? new Options() : $options;
if (is_dir('/usr/share/php/dompdf/lib/fonts')) {
$rootDir = realpath(__DIR__ . '/../../');
$options = $options
->setChroot(array($rootDir))
->setRootDir($rootDir);
return $options;
}
$rootDir = realpath(__DIR__ . '/../../');
$options = $options
->setChroot(array($rootDir))
->setRootDir($rootDir)
->setTempDir($rootDir . '/tmp')
->setFontDir($rootDir . '/lib/fonts')
->setFontCache($rootDir . '/lib/fonts');
return $options;
}
public function render(string $backend = "cpdf"): Dompdf
{
$options = new Options();
$options->setPdfBackend($backend);
$pdf = $this->getDompdf($options);
$pdf->loadHtmlFile($this->file->getPathname());
$pdf->setBasePath($this->file->getPath());
$pdf->render();
return $pdf;
}
public function updateReferenceFile(): void
{
$pdf = $this->render();
$file = $this->referenceFile();
file_put_contents($file->getPathname(), $pdf->output());
}
}
|