### Install via Composer Source: https://github.com/horstoeko/zugferdvisualizer/blob/master/README.md Add the package dependency to your project's composer.json file to enable the visualizer functionality. ```json "require": { "horstoeko/zugferdvisualizer": "^1" } ``` -------------------------------- ### Install ZUGFeRD Visualizer via Composer Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt This command installs the ZUGFeRD Visualizer library using Composer, adding invoice visualization capabilities to your PHP project. Ensure Composer is installed and accessible in your environment. ```bash composer require horstoeko/zugferdvisualizer:^1 ``` -------------------------------- ### Use Pre-Init Callback to Configure mPDF with ZugferdVisualizer Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt This example illustrates using the `setPdfPreInitCallback` method to modify the mPDF configuration array before the mPDF engine is instantiated. This allows for customization of orientation, margins, temporary directory, and PDF/A compliance. ```php setDefaultTemplate(); // Modify mPDF config before instantiation $visualizer->setPdfPreInitCallback(function (array $config, ZugferdVisualizer $visualizer) { // Set landscape orientation $config["orientation"] = "L"; // Set custom margins (in mm) $config["margin_left"] = 15; $config["margin_right"] = 15; $config["margin_top"] = 20; $config["margin_bottom"] = 20; // Set custom temp directory $config["tempDir"] = '/custom/temp/mpdf'; // Enable/disable PDF/A compliance $config["PDFA"] = true; $config["PDFAauto"] = true; // Must return the modified config return $config; }); $visualizer->renderPdfFile('/output/invoice.pdf'); ?> ``` -------------------------------- ### Use Custom Renderer with ZUGFeRD Visualizer (PHP) Source: https://github.com/horstoeko/zugferdvisualizer/blob/master/README.md Applies a custom renderer to the Zugferd Visualizer. This example shows how to instantiate a custom renderer (e.g., `MyOwnRenderer`), set it on the `ZugferdVisualizer` instance, specify a custom template, and then render the markup. This provides a way to use custom logic for visualizing ZUGFeRD data. ```php 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(); ``` -------------------------------- ### Render Markup as HTML (PHP) Source: https://github.com/horstoeko/zugferdvisualizer/wiki/Class-ZugferdVisualizer Renders the internal markup, typically HTML, and returns it as a string. This method is used to get the HTML representation of the content. ```php public function renderMarkup(): string { } ``` -------------------------------- ### Implement Custom Markup Renderer with Twig Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt Demonstrates how to implement the ZugferdVisualizerMarkupRendererContract to render ZUGFeRD invoices using the Twig templating engine. It includes the class implementation and a usage example for initializing the visualizer with the custom renderer. ```php twig = $twig; } public function templateExists(string $template): bool { return $this->twig->getLoader()->exists($template); } public function render(ZugferdDocumentReader $document, string $template): string { $document->getDocumentInformation( $documentNo, $documentTypeCode, $documentDate, $invoiceCurrency, $taxCurrency, $documentName, $documentLanguage, $effectiveSpecifiedPeriod ); $document->getDocumentSeller($sellerName, $sellerId, $sellerDescription); $document->getDocumentBuyer($buyerName, $buyerId, $buyerDescription); return $this->twig->render($template, [ 'document' => $document, 'documentNo' => $documentNo, 'documentDate' => $documentDate, 'currency' => $invoiceCurrency, 'sellerName' => $sellerName, 'buyerName' => $buyerName, ]); } } // Usage $twig = new \Twig\Environment(new \Twig\Loader\FilesystemLoader('/templates')); $document = ZugferdDocumentReader::readAndGuessFromFile('/path/to/invoice.xml'); $visualizer = new ZugferdVisualizer($document); $visualizer->setRenderer(new TwigRenderer($twig)); $visualizer->setTemplate('invoice.html.twig'); echo $visualizer->renderMarkup(); ``` -------------------------------- ### Use Built-in Laravel Renderer for ZUGFeRD (PHP) Source: https://github.com/horstoeko/zugferdvisualizer/blob/master/README.md Integrates the Zugferd Visualizer with the Laravel framework using `ZugferdVisualizerLaravelRenderer`. This example shows how to set up the renderer within a Laravel controller to read an invoice XML, render its markup using a Blade template (e.g., `zugferd.blade.php`), and return it as an HTTP response. It also includes a method to download the generated PDF. ```php 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); } } ``` -------------------------------- ### Configuration Methods Source: https://github.com/horstoeko/zugferdvisualizer/wiki/Class-ZugferdVisualizer Methods for configuring the PDF engine initialization callbacks. ```APIDOC ## POST setPdfPreInitCallback ### Description Sets the callback function to be executed before the PDF engine instance is initialized. ### Method POST ### Parameters #### Request Body - **callback** (callable) - Required - The function to execute before initialization. ## POST setPdfRuntimeInitCallback ### Description Sets the callback function to be executed after the internal instance of the PDF-Engine is instantiated. ### Method POST ### Parameters #### Request Body - **callback** (callable) - Required - The function to execute after initialization. ``` -------------------------------- ### Initialize PDF Engine Runtime Source: https://github.com/horstoeko/zugferdvisualizer/blob/master/README.md Configures the internal PDF engine settings after instantiation using a callback function. This allows for fine-grained control over the Mpdf instance properties. ```php 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"; }); ``` -------------------------------- ### Set PDF Initialization Options with Callback (PHP) Source: https://github.com/horstoeko/zugferdvisualizer/blob/master/README.md Allows customization of the internal PDF engine's settings before it's initialized using `setPdfPreInitCallback`. This method accepts a callback function that receives the configuration array and the visualizer instance, enabling modification of options like page orientation. This provides fine-grained control over PDF generation parameters. ```php 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; }); ``` -------------------------------- ### Create PDF from Document Builder and Merge XML (PHP) Source: https://github.com/horstoeko/zugferdvisualizer/blob/master/README.md This snippet demonstrates creating a PDF by first building a document using ZugferdDocumentBuilder, then reading its content, visualizing it with ZugferdVisualizer, and finally merging the original XML content with the generated PDF. The result is saved as a new PDF file. It requires ZugferdDocumentBuilder, ZugferdDocumentReader, ZugferdVisualizer, and ZugferdDocumentPdfMerger. ```php $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"); ``` -------------------------------- ### Initialize ZugferdVisualizer from Document Reader Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt Demonstrates how to create an instance of the ZugferdVisualizer class by loading an existing ZUGFeRD XML invoice file using ZugferdDocumentReader. It shows both direct instantiation and the recommended factory method. ```php setDefaultTemplate(); $visualizer->setPdfFontDefault("courier"); $visualizer->renderPdfFile(dirname(__FILE__) . "/invoice_1.pdf"); ``` -------------------------------- ### Set PDF Pre-Initialization Callback (PHP) Source: https://github.com/horstoeko/zugferdvisualizer/wiki/Class-ZugferdVisualizer Sets a callback function to be executed before the internal PDF engine instance is created. This allows for pre-initialization configurations. The callback must be a callable type. ```php public function setPdfPreInitCallback(callable $callback): void { } ``` -------------------------------- ### Render HTML from XML Invoice Source: https://github.com/horstoeko/zugferdvisualizer/blob/master/README.md Demonstrates how to read an existing XML invoice document and render it as HTML markup using the built-in template. ```php 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(); ``` -------------------------------- ### Use Runtime Init Callback to Configure mPDF Instance with ZugferdVisualizer Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt This snippet demonstrates how to use the `setPdfRuntimeInitCallback` method to configure the mPDF object instance after it has been created. This allows direct manipulation of the Mpdf object for setting PDF version, metadata, watermarks, and headers/footers. ```php setDefaultTemplate(); // Configure mPDF instance after creation $visualizer->setPdfRuntimeInitCallback(function (Mpdf $mpdf, ZugferdVisualizer $visualizer) { // Set PDF version $mpdf->pdf_version = "1.7"; // Set document metadata $mpdf->SetTitle('Invoice Document'); $mpdf->SetAuthor('My Company'); $mpdf->SetCreator('ZugferdVisualizer'); $mpdf->SetSubject('Electronic Invoice'); // Add watermark $mpdf->SetWatermarkText('DRAFT'); $mpdf->showWatermarkText = true; // Set header/footer $mpdf->SetHTMLHeader('
Text in Comic Sans
``` -------------------------------- ### Render Invoice to PDF File (PHP) Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt Renders an invoice document as a PDF and saves it directly to a specified file path on disk. This method is convenient for generating and storing PDF invoices without needing to handle the binary content directly. ```php setDefaultTemplate(); $visualizer->setPdfFontDefault("courier"); $visualizer->setPdfPaperSize('A4-P'); // Render and save PDF directly to file $visualizer->renderPdfFile('/output/path/invoice.pdf'); echo "PDF saved successfully!"; ``` -------------------------------- ### Build and Merge ZUGFeRD Invoice with PDF (PHP) Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt This PHP code demonstrates the complete process of creating a ZUGFeRD-compliant invoice. It involves building the invoice data using ZugferdDocumentBuilder, reading it back, visualizing it into a PDF using ZugferdVisualizer, and merging the XML with the PDF for final compliance. Dependencies include the 'horstoeko/zugferd' and 'horstoeko/zugferdvisualizer' libraries. ```php setDocumentInformation("471102", "380", DateTime::createFromFormat("Ymd", "20180305"), "EUR") ->addDocumentNote('Invoice as per order dated 01.03.2018.') ->addDocumentNote( "Supplier GmbH\nSupplier Street 20\n80333 Munich\nGermany\nManaging Director: John Doe\nCommercial Register: H A 123", null, 'REG' ) ->setDocumentSupplyChainEvent(DateTime::createFromFormat('Ymd', '20180305')) ->addDocumentPaymentMean( ZugferdPaymentMeans::UNTDID_4461_58, null, null, null, null, null, "DE12500105170648489890", null, null, null ) ->setDocumentSeller("Supplier GmbH", "549910") ->addDocumentSellerGlobalId("4000001123452", "0088") ->addDocumentSellerTaxRegistration("FC", "201/113/40209") ->addDocumentSellerTaxRegistration("VA", "DE123456789") ->setDocumentSellerAddress("Supplier Street 20", "", "", "80333", "Munich", "DE") ->setDocumentSellerContact("John Doe", "Accounting", "+49-111-2222222", "+49-111-3333333", "info@supplier.de") ->setDocumentBuyer("Customer AG", "GE2020211") ->setDocumentBuyerReference("34676-342323") ->setDocumentBuyerAddress("Customer Street 15", "", "", "69876", "Frankfurt", "DE") ->addDocumentTax("S", "VAT", 275.0, 19.25, 7.0) ->addDocumentTax("S", "VAT", 198.0, 37.62, 19.0) ->setDocumentSummation(529.87, 529.87, 473.00, 0.0, 0.0, 473.00, 56.87, null, 0.0) ->addDocumentPaymentTerm("Payable within 30 days net until 04.04.2018") // First line item ->addNewPosition("1") ->setDocumentPositionNote("Note for line 1") ->setDocumentPositionProductDetails("Dividers A4", "", "TB100A4", null, "0160", "4012345001235") ->setDocumentPositionProductOriginTradeCountry("CN") ->setDocumentPositionGrossPrice(9.9000) ->setDocumentPositionNetPrice(9.9000) ->setDocumentPositionQuantity(20, "H87") ->addDocumentPositionTax('S', 'VAT', 19) ->setDocumentPositionLineSummation(198.0) // Second line item ->addNewPosition("2") ->setDocumentPositionNote("Note for line 2") ->setDocumentPositionProductDetails("Yogurt Banana", "", "ARNR2", null, "0160", "4000050986428") ->setDocumentPositionGrossPrice(5.5000) ->setDocumentPositionNetPrice(5.5000) ->setDocumentPositionQuantity(50, "H87") ->addDocumentPositionTax('S', 'VAT', 7) ->setDocumentPositionLineSummation(275.0); // Create reader from builder content $reader = ZugferdDocumentReader::readAndGuessFromContent($document->getContent()); // Configure visualizer $visualizer = new ZugferdVisualizer($reader); $visualizer->setDefaultTemplate(); $visualizer->setPdfFontDefault("courier"); $visualizer->setPdfPaperSize('A4-P'); // Optional: Add custom fonts $visualizer->addPdfFontDirectory(__DIR__ . '/fonts/'); $visualizer->addPdfFontData('comicsans', 'R', 'comic.ttf'); $visualizer->addPdfFontData('comicsans', 'I', 'comici.ttf'); // Merge XML content with rendered PDF for ZUGFeRD/Factur-X compliance $merger = new ZugferdDocumentPdfMerger($document->getContent(), $visualizer->renderPdf()); $merger->generateDocument(); $merger->saveDocument(__DIR__ . "/invoice_complete.pdf"); echo "ZUGFeRD-compliant PDF created successfully!"; ``` -------------------------------- ### Implement Custom ZUGFeRD Renderer (PHP) Source: https://github.com/horstoeko/zugferdvisualizer/blob/master/README.md Defines a custom markup renderer for Zugferd documents by implementing the `ZugferdVisualizerMarkupRendererContract` interface. This involves creating a class with `templateExists` and `render` methods to handle custom template logic and HTML generation. This allows for flexible customization of how ZUGFeRD data is presented. ```php 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) } } ``` -------------------------------- ### Render Invoice as HTML Markup (PHP) Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt Renders an invoice document into an HTML markup string. This method requires a template to be defined and accessible. It can throw exceptions if the template is not defined or does not exist. ```php setDefaultTemplate(); try { $htmlMarkup = $visualizer->renderMarkup(); echo $htmlMarkup; // Outputs HTML representation of the invoice } catch (ZugferdVisualizerNoTemplateDefinedException $e) { echo "Error: No template has been defined"; } catch (ZugferdVisualizerNoTemplateNotExistsException $e) { echo "Error: Template file does not exist"; } ``` -------------------------------- ### Create PDF String from XML Invoice (PHP) Source: https://github.com/horstoeko/zugferdvisualizer/blob/master/README.md Generates a PDF string from an existing invoice XML file using the library's built-in template. It reads the XML, visualizes it with a default template and font, and outputs the PDF as a string. Dependencies include the Zugferd library and its autoloader. ```php 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(); ``` -------------------------------- ### Set Default Template for Visualization Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt Configures the ZugferdVisualizer to use its built-in default template and the default rendering engine. This is the most straightforward method to begin visualizing invoice documents. ```php setDefaultTemplate(); // Uses built-in template and default renderer // Now ready to render echo $visualizer->renderMarkup(); ``` -------------------------------- ### Set Invoice Template Path (PHP) Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt Sets the path to the template file that the configured renderer will use. The expected format of the template path depends on the specific renderer being used (e.g., a file path for the default renderer or a view name for a Laravel-based renderer). ```php setRenderer(new ZugferdVisualizerDefaultRenderer()); $visualizer->setTemplate('/path/to/custom/invoice-template.tmpl'); echo $visualizer->renderMarkup(); ``` -------------------------------- ### Configure Custom PDF Fonts with ZugferdVisualizer Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt This snippet demonstrates how to configure custom fonts for PDF rendering using the ZugferdVisualizer. It involves adding font directories and defining font data (family, style, filename). The default font for the entire PDF can also be set. ```php setDefaultTemplate(); // Add directories where custom fonts are located $visualizer->addPdfFontDirectory('/var/fonts/'); $visualizer->addPdfFontDirectory('/app/resources/fonts/'); // Define font data: (family name, style, filename) // Styles: R=Regular, I=Italic, B=Bold, BI=Bold+Italic $visualizer->addPdfFontData('comicsans', 'R', 'comic.ttf'); $visualizer->addPdfFontData('comicsans', 'I', 'comici.ttf'); $visualizer->addPdfFontData('comicsans', 'B', 'comicbd.ttf'); $visualizer->addPdfFontData('comicsans', 'BI', 'comicbi.ttf'); $visualizer->addPdfFontData('frutiger', 'R', 'Frutiger-Normal.ttf'); $visualizer->addPdfFontData('frutiger', 'I', 'FrutigerObl-Normal.ttf'); // Set default font for the entire PDF $visualizer->setPdfFontDefault("comicsans"); $visualizer->renderPdfFile('/output/invoice.pdf'); ?> ``` -------------------------------- ### Render and Export ZUGFeRD Invoices in Laravel Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt This snippet demonstrates how to use the ZugferdVisualizerLaravelRenderer within a Laravel controller. It covers reading an invoice file, setting the Blade template, and performing both HTML markup rendering and PDF file generation. ```php setRenderer(app(ZugferdVisualizerLaravelRenderer::class)); $visualizer->setTemplate('invoices.zugferd'); // resources/views/invoices/zugferd.blade.php return $visualizer->renderMarkup(); } /** * Download invoice as PDF */ public function download(Request $request): Response { $document = ZugferdDocumentReader::readAndGuessFromFile( storage_path('app/invoices/invoice.xml') ); $visualizer = new ZugferdVisualizer($document); $visualizer->setRenderer(app(ZugferdVisualizerLaravelRenderer::class)); $visualizer->setTemplate('invoices.zugferd'); $visualizer->setPdfFontDefault("dejavusans"); $visualizer->setPdfPaperSize('A4-P'); $outputPath = storage_path('app/temp/invoice.pdf'); $visualizer->renderPdfFile($outputPath); return response()->download($outputPath, 'invoice.pdf', [ 'Content-Type' => 'application/pdf', ])->deleteFileAfterSend(); } } ``` -------------------------------- ### Set Custom Markup Renderer (PHP) Source: https://context7.com/horstoeko/zugferdvisualizer/llms.txt Allows setting a custom markup renderer that implements the ZugferdVisualizerMarkupRendererContract interface. This enables the use of custom rendering logic instead of the default one. The custom renderer must define `templateExists` and `render` methods. ```php getDocumentInformation($documentno, $documenttypecode, $documentdate, $invoiceCurrency); // Return custom HTML return "