2020-02-19 21:44:12 +01:00
|
|
|
<?php
|
2020-12-21 16:32:04 +01:00
|
|
|
|
2020-03-18 10:40:15 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-18 10:40:15 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-03-18 10:40:15 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-03-18 10:40:15 +01:00
|
|
|
*/
|
2020-02-19 21:44:12 +01:00
|
|
|
|
|
|
|
namespace App\Utils\Traits\Pdf;
|
|
|
|
|
2021-04-12 06:36:51 +02:00
|
|
|
use App\Exceptions\InternalPDFFailure;
|
2020-12-21 16:32:04 +01:00
|
|
|
use Beganovich\Snappdf\Snappdf;
|
2020-02-19 21:44:12 +01:00
|
|
|
|
|
|
|
trait PdfMaker
|
|
|
|
{
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns a PDF stream.
|
2020-02-19 21:44:12 +01:00
|
|
|
*
|
|
|
|
* @param string $header Header to be included in PDF
|
|
|
|
* @param string $footer Footer to be included in PDF
|
|
|
|
* @param string $html The HTML object to be converted into PDF
|
|
|
|
*
|
|
|
|
* @return string The PDF string
|
|
|
|
*/
|
2020-03-21 06:37:30 +01:00
|
|
|
public function makePdf($header, $footer, $html)
|
|
|
|
{
|
2020-12-21 16:32:04 +01:00
|
|
|
$pdf = new Snappdf();
|
2020-07-01 00:29:05 +02:00
|
|
|
|
2020-12-25 14:15:57 +01:00
|
|
|
if (config('ninja.snappdf_chromium_path')) {
|
|
|
|
$pdf->setChromiumPath(config('ninja.snappdf_chromium_path'));
|
|
|
|
}
|
|
|
|
|
2021-07-07 02:47:50 +02:00
|
|
|
if (config('ninja.snappdf_chromium_arguments')) {
|
|
|
|
$pdf->clearChromiumArguments();
|
|
|
|
$pdf->addChromiumArguments(config('ninja.snappdf_chromium_arguments'));
|
|
|
|
}
|
|
|
|
|
2023-09-07 12:57:04 +02:00
|
|
|
$html = str_replace(['file:/', 'iframe', '<object', '<object', '127.0.0.1', 'localhost'], ['','','','','',''], $html);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$generated = $pdf
|
2021-04-12 06:36:51 +02:00
|
|
|
->setHtml($html)
|
|
|
|
->generate();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($generated) {
|
2021-04-12 06:36:51 +02:00
|
|
|
return $generated;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-04-12 06:36:51 +02:00
|
|
|
|
|
|
|
throw new InternalPDFFailure('There was an issue generating the PDF locally');
|
2020-07-01 00:29:05 +02:00
|
|
|
}
|
|
|
|
}
|