Jak v Nette vytvořit PDF fakturu

Pokud budete v Nette potřebovat vytvořit fakturu, doporučuji plugin Eciovni.

composer require OndrejBrejla/Eciovni ~1.1

Následně stačí jen vytvořit akci a funkci pro zobrazení faktury:

use OndrejBrejla\Eciovni\Eciovni;
use OndrejBrejla\Eciovni\ParticipantBuilder;
use OndrejBrejla\Eciovni\ItemImpl;
use OndrejBrejla\Eciovni\DataBuilder;
use OndrejBrejla\Eciovni\TaxImpl;

//...


    public function actionDefault() {
        // in case you downloaded mPDF separately
        // include_once(LIBS_DIR . '/MPDF54/mpdf.php');
        $mpdf = new \mPDF('utf-8');

        // Exporting prepared invoice to PDF.
        // To save the invoice into a file just use the second and the third parameter, equally as it's described in the documentation of mPDF->Output().
        $this['eciovni']->exportToPdf( $mpdf );
        //file_put_contents( "faktura.pdf", $this['eciovni']->exportToPdf( $mpdf ) );//uloz pdf na disk
    }

//..


    protected function createComponentEciovni() {
        $dateNow = new \DateTime();
        $dateExp = new \DateTime();
        $dateExp->modify('+14 days');
        $variableSymbol = '1234';

        $supplierBuilder = new ParticipantBuilder('Jan Baťa', 'Vodňany', '11', 'Čurákov', '13000');
        $supplier = $supplierBuilder->setIn('12345678')->setTin('CZ12345678')->setAccountNumber('123456789 / 1111')->build();
        $customerBuilder = new ParticipantBuilder('ěščřžýáíé ŠČŘŽÝÁÍÉ', 'Another street', '3', 'Prague 9', '19000');
        $customer = $customerBuilder->setAccountNumber('123456789 / 1111')->build();

        $items = array(
            new ItemImpl('Tworba webu', 1, 900, TaxImpl::fromPercent(22)),
            new ItemImpl('Správa databáze', 1, 900, TaxImpl::fromLowerDecimal(0.22)),
            new ItemImpl('Píčoviny', 1, 900, TaxImpl::fromUpperDecimal(1.22)),
        );
		/*
		for($i = 0; $i < 100; $i++)
		{
			$items[] = new ItemImpl( "Tworba webu " . $i, 1, 900, TaxImpl::fromPercent(22));
		}*/

        $dataBuilder = new DataBuilder(date('YmdHis'), 'Faktura - faktura číslo', $supplier, $customer, $dateExp, $dateNow, $items);
        $dataBuilder->setVariableSymbol($variableSymbol)->setDateOfVatRevenueRecognition($dateNow);
        $data = $dataBuilder->build();

        return new Eciovni($data);
    }