PHP code example of endroid / pdf

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

    

endroid / pdf example snippets


$this->pdfBuilder
    ->setCover([
        'controller' => CoverController::class,
        'parameters' => ['title' => 'My PDF', 'date' => new DateTime()],
        'cache_key' => 'cover',
        'cache_expires_after' => 3600,
        'cache_clear' => true, // use to purge any previously cached data
    ])
;

<link rel="stylesheet" href="{{ embed(asset('/styles.css')) }}">

<style>
@font-face {
    font-family: 'SCP';
    font-weight: normal;
    src: url('{{ embed('https://fontlibrary.org/scp.ttf') }}');
}
</style>

$pdfBuilder
    ->setCover([
        'controller' => CoverController::class,
        'cache_key' => 'cover',
        'cache_expires_after' => 3600,
    ])
    ->setTableOfContents([
        'path' => '/var/www/html/table_of_contents.xml',
        'cache_key' => 'toc',
    ])
    ->setHeader([
        'template' => 'pdf/header.html.twig',
        'cache_key' => 'header',
    ])
    ->setFooter([
        'template' => 'pdf/footer.html.twig',
        'cache_key' => 'footer',
    ])
    ->setContent([
        'url' => 'http://endroid.nl/',
        'cache_key' => 'content',
    ])
    ->setOptions([
        'margin-top' => 16,
        'margin-bottom' => 16,
        'header-spacing' => 5,
        'footer-spacing' => 5,
    ])
;

$pdf = $pdfBuilder->getPdf();

// Create a response object
$response = InlinePdfResponse::createFromPdf($pdf);

// Or output directly
header('Content-type: application/pdf');
echo $pdf->generate();

$snappy = new Snappy(__DIR__.'/../vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64');

$assetFactory = new AssetFactory();
$assetFactory->add(new DataAssetFactoryAdapter());
$assetFactory->add(new ControllerAssetFactoryAdapter($kernel, $requestStack));
$assetFactory->add(new TemplateAssetFactoryAdapter($twig));
...

$pdfBuilder = new PdfBuilder(new Pdf($snappy), $assetFactory);