PHP code example of horstoeko / zugferdvisualizer

1. Go to this page and download the library: Download horstoeko/zugferdvisualizer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

horstoeko / zugferdvisualizer example snippets


use horstoeko\zugferd\ZugferdDocumentReader;
use horstoeko\zugferdvisualizer\ZugferdVisualizer;


$visualizer = new ZugferdVisualizer($document);
$visualizer->setDefaultTemplate();

echo $visualizer->renderMarkup();

use horstoeko\zugferd\ZugferdDocumentReader;
use horstoeko\zugferdvisualizer\ZugferdVisualizer;


$visualizer = new ZugferdVisualizer($document);
$visualizer->setDefaultTemplate();
$visualizer->setPdfFontDefault("courier");
$visualizer->renderPdfFile(dirname(__FILE__) . "/invoice_1.pdf");

use horstoeko\zugferd\ZugferdDocumentReader;
use horstoeko\zugferdvisualizer\ZugferdVisualizer;


$visualizer = new ZugferdVisualizer($document);
$visualizer->setDefaultTemplate();
$visualizer->setPdfFontDefault("courier");

$pdfString = $visualizer->renderPdf();

$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");

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;


$visualizer = new ZugferdVisualizer($document);
$visualizer->setRenderer(new MyOwnRenderer());
$visualizer->setTemplate('/assets/myowntemplate.tmpl');

echo $visualizer->renderMarkup();

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);
    }
}

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;
});

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";
});

use horstoeko\zugferdvisualizer\ZugferdVisualizer;
use Mpdf\Mpdf;

$visualizer = new ZugferdVisualizer(static::$document);
$visualizer->addPdfFontDirectory('/var/fonts1/');
$visualizer->addPdfFontDirectory('/var/fonts2/');

$visualizer->addPdfFontData('comicsans', 'R', 'comic.ttf');
$visualizer->addPdfFontData('comicsans', 'I', 'comici.ttf');

$visualizer->setPdfFontDefault("comicsans");