PHP code example of vianetz / pdf-generator

1. Go to this page and download the library: Download vianetz/pdf-generator 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/ */

    

vianetz / pdf-generator example snippets


// Create a new pdf instance.
$pdf = \Vianetz\Pdf\Model\PdfFactory::general()->create();

// Create the document. You can return any kind of HTML content here.
$document = new \Vianetz\Pdf\Model\HtmlDocument('<strong>Hello</strong> World!');
 
// Add our document to the pdf. You can add as many documents as you like
// as they will all be merged into one PDF file.
$pdf->add($document);

// Save the resulting PDF to file test.pdf - That's it :-)
$pdf->saveToFile('test.pdf');

$config = (new \Vianetz\Pdf\Model\Config())
    ->setPdfSize('a4')
    ->setPdfOrientation(\Vianetz\Pdf\Model\Config::PAPER_ORIENTATION_LANDSCAPE)
    ->setPdfAuthor('vianetz')
    ->setPdfTitle('Invoice 1000001')
    ->setIsDebugMode(true);

$pdf = \Vianetz\Pdf\Model\PdfFactory::general()->create($config);

$document = new \Vianetz\Pdf\Model\HtmlDocument(
    '<strong>Hello</strong> World!',
    'background.pdf',           // used for every page
    'background-first-page.pdf' // optional, used for the first page instead
);

// Load some random PDF contents
$pdfString = file_get_contents('test1.pdf');

// Setup things
$pdfMerge = \Vianetz\Pdf\Model\PdfMerge::create();

// Do the merge - the second argument puts every page on a background template.
$pdfMerge->mergePdfString($pdfString, 'background.pdf');
$pdfMerge->mergePdfString(file_get_contents('test2.pdf'));

// Save the result PDF to file result.pdf.
file_put_contents('result.pdf', $pdfMerge->toPdf());

$config = new \Vianetz\Pdf\Model\Config();

$pdf = new \Vianetz\Pdf\Model\Pdf(
    $config,
    new \Vianetz\Pdf\Model\NoneEventManager(),
    new \Vianetz\Pdf\Model\Generator\Dompdf($config),
    new \Vianetz\Pdf\Model\Merger\ZugferdFpdf($config)
);

$pdf->add(new \Vianetz\Pdf\Model\HtmlDocument('<strong>Invoice</strong> 1000001'));
$pdf->attach('factur-x.xml');
$pdf->saveToFile('invoice.pdf');

try {
    $pdf->saveToFile('test.pdf');
} catch (\Vianetz\Pdf\Exception $e) {
    // NoDataException, FileNotFoundException, InvalidDocumentException, UnsupportedPaperSizeException
}