PHP code example of tesla-software / chrome2pdf

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

    

tesla-software / chrome2pdf example snippets



use Tesla\Chrome2Pdf\Chrome2Pdf;

$c2p = new Chrome2Pdf();
$c2p->setChromeExecutablePath('/opt/google/chrome/chrome')
    ->appendChromeArgs(['--disable-gpu']);

$pdf = $c2p
    ->portrait()
    ->setPaperFormat('A4')
    ->setMargins(10, 10, 10, 10, 'mm')
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->setHeader('<div style="font-size: 11px">This is a header</div>')
    ->setFooter('<div style="font-size: 11px">This is a footer <span class="pageNumber"></span>/<span class="totalPages"></span></div>')
    ->pdf();

file_put_contents('test.pdf', $pdf);


use Tesla\Chrome2Pdf\Chrome2Pdf;

class ExampleController extends Controller
{
    public function showPdf()
    {
        $pdf = (new Chrome2Pdf())
            ->portrait()
            ->setPaperFormat('A4')
            ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
            ->pdf();

        return response()->make($pdf, 200, ['Content-Type' => 'application/pdf']);
    }
}

$pdfContent = (new \Tesla\Chrome2Pdf\Chrome2Pdf())
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

// Available options: A0, A1, A2, A3, A4, A5, A6, Letter, Legal, Tabloid, Ledger
$chrome2pdf->setPaperFormat('A4');

// Custom margins ($top, $right, $bottom, $left, $measurementUnit)
// Available units ion
$chrome2pdf->portrait();
$chrome2pdf->landscape();

// Change webpage rendering scale
$chrome2pdf->setScale(1);

// Set header and footer HTML
$chrome2pdf->setHeader('<p>Header text</p>');
$chrome2pdf->setFooter('<p>Footer text</p>');

// Disable/enable header and footer
$chrome2pdf->setDisplayHeaderFooter(true);

// Set pdf body content
$chrome2pdf->setContent('<p>Demo content</p>');

// Set custom page print range, e.g., '1-5, 8, 11-13'
$chrome2pdf->setPageRanges('2-3');

// Give any CSS @page size declared in the page priority over what is declared
// in width and height or format options
$chrome2pdf->setPreferCSSPageSize(true);

// Print background graphics
$chrome2pdf->setPrintBackground(true);

$chrome2pdf
    ->setChromeExecutablePath('/custom/path/to/chrome')
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

$chrome2pdf
    ->setTempFolder('/storage/temp/pdf')
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

$chrome2pdf
    ->setWaitForLifecycleEvent('networkIdle')
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

$chrome2pdf
    ->setDisableScriptExecution(true)
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

$chrome2pdf
    ->appendChromeArgs(['--disable-gpu', '--user-data-dir=/tmp/session-123'])
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

$chrome2pdf
    ->setTimeout(10)
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();

$chrome2pdf
    ->setEmulateMedia('screen')
    ->setContent('<h1>Hello world</h1><p>This is a paragraph</p>')
    ->pdf();