1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Services/Pdf/PdfService.php

152 lines
3.7 KiB
PHP
Raw Normal View History

2022-12-23 03:22:01 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Pdf;
2022-12-28 15:50:11 +01:00
use App\Models\Company;
2023-01-08 05:42:44 +01:00
use App\Models\CreditInvitation;
use App\Models\InvoiceInvitation;
2023-02-23 11:32:51 +01:00
use App\Models\PurchaseOrderInvitation;
2023-02-25 06:11:12 +01:00
use App\Models\QuoteInvitation;
2023-02-23 11:32:51 +01:00
use App\Models\RecurringInvoiceInvitation;
2023-02-25 06:11:12 +01:00
use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\Pdf\PageNumbering;
use App\Utils\Traits\Pdf\PdfMaker;
use App\Utils\VendorHtmlEngine;
2022-12-23 03:22:01 +01:00
class PdfService
{
2022-12-28 15:50:11 +01:00
use PdfMaker, PageNumbering;
public InvoiceInvitation | QuoteInvitation | CreditInvitation | RecurringInvoiceInvitation | PurchaseOrderInvitation $invitation;
2022-12-23 03:22:01 +01:00
2022-12-23 10:51:24 +01:00
public Company $company;
2022-12-23 03:22:01 +01:00
public PdfConfiguration $config;
2022-12-23 03:46:52 +01:00
public PdfBuilder $builder;
public PdfDesigner $designer;
public array $html_variables;
2022-12-27 16:44:12 +01:00
public string $document_type;
2022-12-28 09:31:43 +01:00
public array $options;
2022-12-27 16:44:12 +01:00
const DELIVERY_NOTE = 'delivery_note';
const STATEMENT = 'statement';
const PURCHASE_ORDER = 'purchase_order';
const PRODUCT = 'product';
2022-12-28 09:31:43 +01:00
public function __construct($invitation, $document_type = 'product', $options = [])
2022-12-23 03:22:01 +01:00
{
$this->invitation = $invitation;
2022-12-23 10:51:24 +01:00
$this->company = $invitation->company;
2022-12-27 16:44:12 +01:00
$this->document_type = $document_type;
2022-12-28 09:31:43 +01:00
$this->options = $options;
2022-12-23 03:46:52 +01:00
}
2023-02-28 08:05:08 +01:00
public function boot(): self
{
$this->init();
return $this;
}
2022-12-28 15:50:11 +01:00
/**
* Resolves the PDF generation type and
* attempts to generate a PDF from the HTML
* string.
2023-03-18 08:24:56 +01:00
*
2023-01-08 05:42:44 +01:00
* @return mixed | Exception
2023-02-21 08:39:07 +01:00
*
2022-12-28 15:50:11 +01:00
*/
public function getPdf()
2022-12-23 03:46:52 +01:00
{
2022-12-28 15:50:11 +01:00
try {
2023-02-28 08:05:08 +01:00
$pdf = $this->resolvePdfEngine($this->getHtml());
2022-12-23 03:22:01 +01:00
2022-12-28 15:50:11 +01:00
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
2022-12-23 03:22:01 +01:00
2023-02-21 08:39:07 +01:00
if ($numbered_pdf) {
2022-12-28 15:50:11 +01:00
$pdf = $numbered_pdf;
}
} catch (\Exception $e) {
nlog(print_r($e->getMessage(), 1));
throw new \Exception($e->getMessage(), $e->getCode());
}
2022-12-23 03:46:52 +01:00
2022-12-28 15:50:11 +01:00
return $pdf;
}
/**
* Renders the dom document to HTML
2023-02-21 08:39:07 +01:00
*
2022-12-28 15:50:11 +01:00
* @return string
2023-02-21 08:39:07 +01:00
*
2022-12-28 15:50:11 +01:00
*/
public function getHtml(): string
{
$html = $this->builder->getCompiledHTML();
2023-02-21 08:39:07 +01:00
if (config('ninja.log_pdf_html')) {
2022-12-28 15:50:11 +01:00
info($html);
}
return $html;
}
2023-02-23 11:58:52 +01:00
/**
* Initialize all the services to build the PDF
*
* @return self
*/
public function init(): self
{
2023-02-23 12:03:21 +01:00
$this->config = (new PdfConfiguration($this))->init();
2023-02-23 11:58:52 +01:00
$this->html_variables = $this->config->client ?
(new HtmlEngine($this->invitation))->generateLabelsAndValues() :
(new VendorHtmlEngine($this->invitation))->generateLabelsAndValues();
$this->designer = (new PdfDesigner($this))->build();
$this->builder = (new PdfBuilder($this))->build();
return $this;
}
2023-02-23 11:32:51 +01:00
/**
* resolvePdfEngine
*
* @return mixed
*/
2023-02-25 05:01:52 +01:00
public function resolvePdfEngine(string $html): mixed
2023-02-23 11:32:51 +01:00
{
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2023-02-25 05:01:52 +01:00
$pdf = (new Phantom)->convertHtmlToPdf($html);
2023-02-23 11:32:51 +01:00
} elseif (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
2023-02-25 05:01:52 +01:00
$pdf = (new NinjaPdf())->build($html);
2023-02-23 11:32:51 +01:00
} else {
2023-02-25 05:01:52 +01:00
$pdf = $this->makePdf(null, null, $html);
2023-02-23 11:32:51 +01:00
}
return $pdf;
}
2023-02-21 08:39:07 +01:00
}