This commit is contained in:
55
api/app/Utils/Exports/Pdf.php
Normal file
55
api/app/Utils/Exports/Pdf.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user