1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 17:31:35 +02:00
invoiceninja/app/Services/Pdf/PdfService.php

158 lines
3.8 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-27 16:44:12 +01:00
use App\Models\Account;
2022-12-28 15:50:11 +01:00
use App\Models\Company;
2023-02-23 11:32:51 +01:00
use App\Utils\HtmlEngine;
use App\Models\QuoteInvitation;
use App\Utils\VendorHtmlEngine;
2023-01-08 05:42:44 +01:00
use App\Models\CreditInvitation;
2023-02-23 11:32:51 +01:00
use App\Utils\PhantomJS\Phantom;
2023-01-08 05:42:44 +01:00
use App\Models\InvoiceInvitation;
2023-02-23 11:32:51 +01:00
use App\Services\Pdf\PdfDesigner;
2022-12-28 15:50:11 +01:00
use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\Traits\Pdf\PdfMaker;
2023-02-23 11:32:51 +01:00
use App\Models\PurchaseOrderInvitation;
use App\Utils\Traits\Pdf\PageNumbering;
use App\Models\RecurringInvoiceInvitation;
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-27 16:44:12 +01:00
public Account $account;
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->account = $this->company->account;
2022-12-28 09:31:43 +01:00
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
}
2022-12-28 15:50:11 +01:00
/**
* Resolves the PDF generation type and
* attempts to generate a PDF from the HTML
* string.
2023-02-21 08:39:07 +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-23 11:32:51 +01:00
$pdf = $this->resolvePdfEngine();
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;
}
2023-02-23 11:32:51 +01:00
2022-12-28 15:50:11 +01:00
} 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
*/
private function resolvePdfEngine(): mixed
{
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
$pdf = (new Phantom)->convertHtmlToPdf($this->getHtml());
} elseif (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
$pdf = (new NinjaPdf())->build($this->getHtml());
} else {
$pdf = $this->makePdf(null, null, $this->getHtml());
}
return $pdf;
}
2023-02-21 08:39:07 +01:00
}