horstoeko / zugferdvisualizer
A library
Installs: 31 481
Dependents: 1
Suggesters: 0
Security: 0
Stars: 19
Watchers: 3
Forks: 4
Open Issues: 0
Type:package
Requires
- php: ^7.3|^7.4|^8
- ext-dom: *
- ext-mbstring: *
- dompdf/dompdf: ^2.0
- horstoeko/zugferd: ^1
- league/commonmark: ^1|^2
- mpdf/mpdf: ^8
Requires (Dev)
- ext-gd: *
- ext-json: *
- ext-zip: *
- pdepend/pdepend: ^2
- phploc/phploc: ^7
- phpmd/phpmd: ^2
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9
- sebastian/phpcpd: ^6
- squizlabs/php_codesniffer: ^3
README
Table of Contents
- ZUGFeRD/XRechnung/Factur-X Visualizer
- Table of Contents
- License
- Overview
- Dependencies
- Installation
- Usage
- Create HTML markup from existing invoice document (XML) using built-in template
- Create a PDF file from existing invoice document (XML) using built-in template
- Create a PDF string from existing invoice document (XML) using built-in template
- Create a PDF string from document builder and merge XML with generated PDF
- Create a custom renderer
- Use a custom renderer
- Use the built-in Laravel renderer
- Set PDF-Options
License
The code in this project is provided under the MIT license.
Overview
With horstoeko/zugferdvisualizer
you can visualize ZUGFeRD/XRechnung/Factur-X documents. This package is an addon for horstoeko/zugferd package. The system uses a markup template (HTML) to render the output. On top you can create a PDF from the rendered markup
Dependencies
This package makes use of
Installation
There is one recommended way to install horstoeko/zugferdvisualizer
via Composer:
- adding the dependency to your
composer.json
file:
"require": { .. "horstoeko/zugferdvisualizer":"^1", .. },
Usage
Create HTML markup from existing invoice document (XML) using built-in template
use horstoeko\zugferd\ZugferdDocumentReader; use horstoeko\zugferdvisualizer\ZugferdVisualizer; require dirname(__FILE__) . "/../vendor/autoload.php"; $document = ZugferdDocumentReader::readAndGuessFromFile(dirname(__FILE__) . "/invoice_1.xml"); $visualizer = new ZugferdVisualizer($document); $visualizer->setDefaultTemplate(); echo $visualizer->renderMarkup();
Create a PDF file from existing invoice document (XML) using built-in template
Find there full example here
use horstoeko\zugferd\ZugferdDocumentReader; use horstoeko\zugferdvisualizer\ZugferdVisualizer; require dirname(__FILE__) . "/../vendor/autoload.php"; $document = ZugferdDocumentReader::readAndGuessFromFile(dirname(__FILE__) . "/invoice_1.xml"); $visualizer = new ZugferdVisualizer($document); $visualizer->setDefaultTemplate(); $visualizer->setPdfFontDefault("courier"); $visualizer->renderPdfFile(dirname(__FILE__) . "/invoice_1.pdf");
Create a PDF string from existing invoice document (XML) using built-in template
use horstoeko\zugferd\ZugferdDocumentReader; use horstoeko\zugferdvisualizer\ZugferdVisualizer; require dirname(__FILE__) . "/../vendor/autoload.php"; $document = ZugferdDocumentReader::readAndGuessFromFile(dirname(__FILE__) . "/invoice_1.xml"); $visualizer = new ZugferdVisualizer($document); $visualizer->setDefaultTemplate(); $visualizer->setPdfFontDefault("courier"); $pdfString = $visualizer->renderPdf();
Create a PDF string from document builder and merge XML with generated PDF
Find there full example here
$document = ZugferdDocumentBuilder::CreateNew(ZugferdProfiles::PROFILE_EN16931); $document ->setDocumentInformation("471102", "380", \DateTime::createFromFormat("Ymd", "20180305"), "EUR") ->... $reader = ZugferdDocumentReader::readAndGuessFromContent($document->getContent()); $visualizer = new ZugferdVisualizer($reader); $visualizer->setDefaultTemplate(); $visualizer->setPdfFontDefault("courier"); $visualizer->setPdfPaperSize('A4-P'); $merger = new ZugferdDocumentPdfMerger($document->getContent(), $visualizer->renderPdf()); $merger->generateDocument(); $merger->saveDocument(dirname(__FILE__) . "/invoice_2.pdf");
Create a custom renderer
If you want to implement your own markup renderer, then your class must implement the interface ZugferdVisualizerMarkupRendererContract
. The interface defines two methods:
templateExists
render
use horstoeko\zugferd\ZugferdDocumentReader; use horstoeko\zugferdvisualizer\contracts\ZugferdVisualizerMarkupRendererContract; class MyOwnRenderer implements ZugferdVisualizerMarkupRendererContract { public function templateExists(string $template): bool { // Put your logic here // Method must return a boolean value } public function render(ZugferdDocumentReader $document, string $template): string { // Put your logic here // Method must return a string (rendered HTML markup) } }
Use a custom renderer
use horstoeko\zugferd\ZugferdDocumentReader; use horstoeko\zugferdvisualizer\ZugferdVisualizer; require dirname(__FILE__) . "/../vendor/autoload.php"; $document = ZugferdDocumentReader::readAndGuessFromFile(dirname(__FILE__) . "/invoice_1.xml"); $visualizer = new ZugferdVisualizer($document); $visualizer->setRenderer(new MyOwnRenderer()); $visualizer->setTemplate('/assets/myowntemplate.tmpl'); echo $visualizer->renderMarkup();
Use the built-in Laravel renderer
The ZugferdVisualizerLaravelRenderer
can be used within the Laravel-Framework:
namespace App\Http\Controllers; use Illuminate\Http\Request; use horstoeko\zugferd\ZugferdDocumentReader; use horstoeko\zugferdvisualizer\renderer\ZugferdVisualizerLaravelRenderer; use horstoeko\zugferdvisualizer\ZugferdVisualizer; class ZugferdController extends Controller { public function index(Request $request) { $document = ZugferdDocumentReader::readAndGuessFromFile(storage_path('app/invoice_1.xml')); $visualizer = new ZugferdVisualizer($document); $visualizer->setRenderer(app(ZugferdVisualizerLaravelRenderer::class)); $visualizer->setTemplate('zugferd'); // ~/resources/views/zugferd.blade.php return $visualizer->renderMarkup(); } public function download(Request $request) { $document = ZugferdDocumentReader::readAndGuessFromFile(storage_path('app/invoice_1.xml')); $visualizer = new ZugferdVisualizer($document); $visualizer->setRenderer(app(ZugferdVisualizerLaravelRenderer::class)); $visualizer->setTemplate('zugferd'); $visualizer->setPdfFontDefault("courier"); $visualizer->setPdfPaperSize('A4-P'); $visualizer->renderPdfFile(storage_path('app/invoice_1.pdf')); $headers = [ 'Content-Type: application/pdf', ]; return response()->download(storage_path('app/invoice_1.pdf'), "invoice_1.pdf", $headers); } }
Set PDF-Options
If you want to make further settings to the internal PDF engine, then you can change further settings using a callback. The usage is as follows:
Set options before instanciating the internal PDF-Engine (setPdfPreInitCallback
)
use horstoeko\zugferdvisualizer\ZugferdVisualizer; use Mpdf\Mpdf; $visualizer = new ZugferdVisualizer(static::$document); $visualizer->setDefaultTemplate(); $visualizer->setPdfPreInitCallback(function (array $config, ZugferdVisualizer $visualizer) { $config["orientation"] = "L"; return $config; });
Set options after instanciating the internal PDF-Engine (setPdfRuntimeInitCallback
)
use horstoeko\zugferdvisualizer\ZugferdVisualizer; use Mpdf\Mpdf; $visualizer = new ZugferdVisualizer(static::$document); $visualizer->setDefaultTemplate(); $visualizer->setPdfRuntimeInitCallback(function (Mpdf $mpdf, ZugferdVisualizer $visualizer) { $mpdf->pdf_version = "1.7"; });
Working with custom fonts
If you would like to use your own fonts, that's no problem at all. First you have to specify one or more directories in which your fonts are located:
use horstoeko\zugferdvisualizer\ZugferdVisualizer; use Mpdf\Mpdf; $visualizer = new ZugferdVisualizer(static::$document); $visualizer->addPdfFontDirectory('/var/fonts1/'); $visualizer->addPdfFontDirectory('/var/fonts2/');
Next, you need to define the font properties:
- The first parameter sets the name of the font-family
- Thé second parameter sets the type of the font
- R - Regular
- I - Italic
- B - Bold
- BI - Bold & Italic
- The third parameter sets the filename under which the font can be found in the specified font-directories
$visualizer->addPdfFontData('comicsans', 'R', 'comic.ttf'); $visualizer->addPdfFontData('comicsans', 'I', 'comici.ttf');
If you want to set a custom font as the default font, you can use the following method:
$visualizer->setPdfFontDefault("comicsans");
You can also use the name of the font family in the style attribute of any HTML elements in your template:
<p style="font-family: comicsans">Text in Comic Sans</p>
For more configuration options, please consult the documentation of mPdf