PHP code example of dreadnip / chrome-pdf-bundle

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

    

dreadnip / chrome-pdf-bundle example snippets


use Dreadnip\ChromePdfBundle\Service\PdfGenerator;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

class TestController extends AbstractController
{
    public function __invoke(PdfGenerator $pdfGenerator): Response
    {
        $html = $this->render('pdf.html.twig');

        $path = $pdfGenerator->generate($html, 'files/test.pdf');
   
        return new BinaryFileResponse($path);
    }
}

use Dreadnip\ChromePdfBundle\Service\PdfGenerator;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

class TestController
{
    public function __invoke(
        Environment $twig,
        PdfGenerator $pdfGenerator
    ): Response {
        $html = $twig->render('pdf.html.twig');

        // Control everything by passing custom options
        $printOptions = [
            'printBackground' => true,
            'displayHeaderFooter' => true,
            'preferCSSPageSize' => true,
            'headerTemplate'=> "<div></div>",
            'footerTemplate' => "<div></div>",
            'scale' => 1.0,
        ];
        
        // Setting headless to false helps you debug issues
        $browserOptions = [
            'headless' => false,
        ];

        $path = $pdfGenerator->generate(
            html: $html,
            path: 'files/test.pdf',
            printOptions: $options,
            browserOptions: $browserOptions,
            timeout: 5000
        );

        return new BinaryFileResponse($path);
    }
}