56 lines
1.1 KiB
PHP
56 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Utils\Exports;
|
|
|
|
use Dompdf\Dompdf;
|
|
use Dompdf\Options;
|
|
|
|
class Pdf
|
|
{
|
|
private Dompdf $dompdf;
|
|
|
|
public function __construct(Options $options = null)
|
|
{
|
|
$this->dompdf = new Dompdf($options ?? $this->getDefaultOptions());
|
|
|
|
$this->setPaper('A4', 'portrait');
|
|
}
|
|
|
|
private function getDefaultOptions() : Options
|
|
{
|
|
$options = new Options;
|
|
|
|
$options->set('isPhpEnabled', true);
|
|
|
|
return $options;
|
|
}
|
|
|
|
public function setPaper(string $type, string $orientation) : void
|
|
{
|
|
$this->dompdf->setPaper($type, $orientation);
|
|
}
|
|
|
|
public function stream(string $html, string $fileName = 'documento')
|
|
{
|
|
$this->dompdf->loadHtml($html);
|
|
$this->dompdf->render();
|
|
|
|
return response()->streamDownload(
|
|
function () {
|
|
|
|
echo $this->dompdf->output();
|
|
},
|
|
"{$fileName}.pdf",
|
|
['Content-Type' => 'Application/pdf']
|
|
);
|
|
}
|
|
|
|
public function output(string $html)
|
|
{
|
|
$this->dompdf->loadHtml($html);
|
|
$this->dompdf->render();
|
|
|
|
return $this->dompdf->output();
|
|
}
|
|
}
|