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

150 lines
3.1 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;
2022-12-23 03:22:01 +01:00
use App\Services\Pdf\PdfConfiguration;
2022-12-28 15:50:11 +01:00
use App\Utils\HostedPDF\NinjaPdf;
2022-12-23 03:46:52 +01:00
use App\Utils\HtmlEngine;
2022-12-28 15:50:11 +01:00
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\Pdf\PageNumbering;
use App\Utils\Traits\Pdf\PdfMaker;
2022-12-23 03:22:01 +01:00
class PdfService
{
2022-12-28 15:50:11 +01:00
use PdfMaker, PageNumbering;
2022-12-23 03:22:01 +01:00
public $invitation;
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-23 03:22:01 +01:00
$this->config = (new PdfConfiguration($this))->init();
2022-12-23 03:46:52 +01:00
$this->html_variables = (new HtmlEngine($invitation))->generateLabelsAndValues();
$this->designer = (new PdfDesigner($this))->build();
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-28 15:50:11 +01:00
$this->builder = (new PdfBuilder($this))->build();
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.
*
* @return mixed
*
*/
public function getPdf()
2022-12-23 03:46:52 +01:00
{
2022-12-28 15:50:11 +01:00
try {
2022-12-28 11:28:58 +01:00
2022-12-28 15:50:11 +01:00
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());
}
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
2022-12-28 15:50:11 +01:00
if ($numbered_pdf)
{
$pdf = $numbered_pdf;
}
2022-12-23 03:22:01 +01:00
2022-12-28 15:50:11 +01:00
} catch (\Exception $e) {
2022-12-23 03:46:52 +01:00
2022-12-28 15:50:11 +01:00
nlog(print_r($e->getMessage(), 1));
2022-12-23 03:46:52 +01:00
2022-12-28 15:50:11 +01:00
throw new \Exception($e->getMessage(), $e->getCode());
2022-12-23 03:46:52 +01:00
2022-12-28 15:50:11 +01:00
}
2022-12-23 03:46:52 +01:00
2022-12-28 15:50:11 +01:00
return $pdf;
2022-12-23 03:46:52 +01:00
2022-12-28 15:50:11 +01:00
}
/**
* Renders the dom document to HTML
*
* @return string
*
*/
public function getHtml(): string
{
2022-12-23 03:46:52 +01:00
2022-12-28 15:50:11 +01:00
$html = $this->builder->getCompiledHTML();
if (config('ninja.log_pdf_html'))
{
info($html);
}
return $html;
}
2022-12-23 03:46:52 +01:00
2022-12-23 03:22:01 +01:00
}