The code in this project is provided under the MIT license.
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
This package makes use of
There is one recommended way to install horstoeko/zugferdvisualizer
via Composer:
composer.json
file: "require": {
..
"horstoeko/zugferdvisualizer":"^1",
..
},
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();
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");
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();
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");
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 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();
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);
}
}
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:
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;
});
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";
});
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:
$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