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

232 lines
5.9 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-10-26 07:52:34 +02:00
use App\Models\Invoice;
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-10-26 07:52:34 +02:00
use App\Utils\PhantomJS\Phantom;
2023-01-08 05:42:44 +01:00
use App\Models\InvoiceInvitation;
2023-02-25 06:11:12 +01:00
use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\Traits\Pdf\PdfMaker;
2023-10-26 07:52:34 +02:00
use App\Jobs\Invoice\CreateEInvoice;
use App\Models\PurchaseOrderInvitation;
use App\Utils\Traits\Pdf\PageNumbering;
use App\Models\RecurringInvoiceInvitation;
use horstoeko\zugferd\ZugferdDocumentPdfBuilder;
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;
2023-10-24 07:26:16 +02:00
private float $start_time;
public float $execution_time;
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;
2023-10-25 10:39:42 +02:00
$this->start_time = microtime(true);
2022-12-23 03:46:52 +01:00
}
2023-02-28 08:05:08 +01:00
public function boot(): self
2023-10-26 04:57:44 +02:00
{
2023-10-24 07:26:16 +02:00
2023-02-28 08:05:08 +01:00
$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;
}
2023-10-26 07:52:34 +02:00
if($this->config->entity_string == "invoice" && $this->config->settings->enable_e_invoice) {
$pdf = $this->checkEInvoice($pdf);
}
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
2023-10-24 07:26:16 +02:00
$this->execution_time = microtime(true) - $this->start_time;
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
{
2023-10-25 10:04:27 +02:00
2022-12-28 15:50:11 +01:00
$html = $this->builder->getCompiledHTML();
2023-02-21 08:39:07 +01:00
if (config('ninja.log_pdf_html')) {
nlog($html);
2022-12-28 15:50:11 +01:00
}
2023-10-24 07:26:16 +02:00
$this->execution_time = microtime(true) - $this->start_time;
2023-11-12 05:09:49 +01:00
nlog($html);
2022-12-28 15:50:11 +01:00
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-10-25 10:04:27 +02:00
$this->start_time = microtime(true);
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-10-26 07:52:34 +02:00
/**
* Switch to determine if we need to embed the xml into the PDF itself
*
* @param string $pdf
* @return string
*/
private function checkEInvoice(string $pdf): string
{
if(!$this->config->entity instanceof Invoice)
return $pdf;
$e_invoice_type = $this->config->settings->e_invoice_type;
switch ($e_invoice_type) {
case "EN16931":
case "XInvoice_2_2":
case "XInvoice_2_1":
case "XInvoice_2_0":
case "XInvoice_1_0":
case "XInvoice-Extended":
case "XInvoice-BasicWL":
case "XInvoice-Basic":
return $this->embedEInvoiceZuGFerD($pdf) ?? $pdf;
//case "Facturae_3.2":
//case "Facturae_3.2.1":
//case "Facturae_3.2.2":
//
default:
return $pdf;
}
}
/**
* Embed the .xml file into the PDF
*
* @param string $pdf
* @return string
*/
private function embedEInvoiceZuGFerD(string $pdf): string
{
try {
$e_rechnung = (new CreateEInvoice($this->config->entity, true))->handle();
$pdfBuilder = new ZugferdDocumentPdfBuilder($e_rechnung, $pdf);
$pdfBuilder->generateDocument();
return $pdfBuilder->downloadString(basename($this->config->entity->getFileName()));
} catch (\Exception $e) {
nlog("E_Invoice Merge failed - " . $e->getMessage());
}
return $pdf;
}
2023-02-21 08:39:07 +01:00
}