PHP code example of heimrichhannot / pdf-creator

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

    

heimrichhannot / pdf-creator example snippets


use HeimrichHannot\PdfCreator\Concrete\MpdfCreator;
use HeimrichHannot\PdfCreator\PdfCreatorFactory;

$pdf = PdfCreatorFactory::createInstance(MpdfCreator::getType());
$pdf->setHtmlContent($this->compile())
    ->setFilename($this->getFileName())
    ->setFormat('A4')
    ->setOrientation($pdf::ORIENTATION_PORTRAIT)
    ->addFont(
        "/path_to_project/assets/fonts/my_great_font.tff", 
        "myGreatFont", 
        $pdf::FONT_STYLE_REGUALAR,
        "normal"
    )
    ->setMargins(15, 10, 15,10)
    ->setTemplateFilePath("/path_to_project/assets/pdf/mastertemplate.pdf")
    ->setOutputMode($pdf::OUTPUT_MODE_DOWNLOAD)
    ->render()
;

use HeimrichHannot\PdfCreator\BeforeCreateLibraryInstanceCallback;
use HeimrichHannot\PdfCreator\BeforeOutputPdfCallback;
use HeimrichHannot\PdfCreator\Concrete\MpdfCreator;
use HeimrichHannot\PdfCreator\PdfCreatorFactory;

$pdf = PdfCreatorFactory::createInstance(MpdfCreator::getType());

$pdf->setBeforeCreateInstanceCallback(function (BeforeCreateLibraryInstanceCallback $callbackData) {
    $parameter = $callbackData->getConstructorParameters();
    $parameter['config']['fonttrans'] = [
        'rotis-sans-serif-w01-bold' => 'rotis-sans-serif',
        'rotissansserifw01-bold' => 'rotis-sans-serif',
    ];
    $callbackData->setConstructorParameters($parameter);
    return $callbackData;
});

$pdf->setBeforeOutputPdfCallback(function (BeforeOutputPdfCallback $callbackData) use ($pdf) {
    $mpdf = $callbackData->getLibraryInstance();
    $mpdf->AddPage();
    $parameters = $callbackData->getOutputParameters();
    $parameters['name'] = 'custom_'.$pdf->getFilename();
    $callbackData->setOutputParameters($parameters);
});

use HeimrichHannot\PdfCreator\Concrete\DompdfCreator;
use HeimrichHannot\PdfCreator\PdfCreatorFactory;

$pdf = PdfCreatorFactory::createInstance(DompdfCreator::getType());
$result = $pdf->setOutputMode($pdf::OUTPUT_MODE_FILE)
    // ...
    ->render()
;
$filepath = $result->getFilePath();

$pdf = PdfCreatorFactory::createInstance(DompdfCreator::getType());
$result = $pdf->setOutputMode($pdf::OUTPUT_MODE_STRING)
    // ...
    ->render()
;
$filepath = $result->getFileContent();