PHP code example of zentiq-labs / fast-pdf

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

    

zentiq-labs / fast-pdf example snippets


use ZentiqLabs\FastPdf\FastPdf;

$pdf = new FastPdf();

// From a raw HTML string
$pdf->fromHtml('<html><body><h1>Hello World</h1></body></html>')
    ->save('/var/www/output.pdf');

// From a PHP template file
$pdf->fromFile(__DIR__ . '/templates/invoice.php', ['invoice' => $invoice])
    ->landscape()
    ->download('invoice.pdf');   // streams + exits

$pdf = new FastPdf([
    'binary'       => '/usr/bin/chromium-browser',
    'timeout'      => 15,
    'paper_size'   => 'a4',
    'orientation'  => 'portrait',
    'emulate_media' => 'print',
    'margins'      => ['top' => 15, 'right' => 15, 'bottom' => 15, 'left' => 15, 'unit' => 'mm'],
    'chromium_flags' => [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
    ],
    'tailwind_cdn_url' => 'https://cdn.tailwindcss.com',
    'temp_dir'     => '/tmp',
]);

$pdf = new FastPdf(

// templates/invoice.php

$pdf->fromFile(__DIR__ . '/templates/invoice.php', [
    'invoice' => ['number' => 'INV-001', 'total' => 499.00],
])->save('/var/invoices/INV-001.pdf');

$pdf->fromHtml($html)
    ->withTailwind()
    ->paperSize('a4')
    ->save('/tmp/report.pdf');

$bytes = $pdf->fromHtml($html)->output();
file_put_contents('/var/cache/report.pdf', $bytes);

$pdf->fromFile('/templates/receipt.php', $data)->save('/receipts/001.pdf');

// Sends Content-Disposition: attachment and calls exit()
$pdf->fromFile('/templates/receipt.php', $data)->download('receipt-001.pdf');

// Sends Content-Disposition: inline and calls exit()
$pdf->fromFile('/templates/receipt.php', $data)->stream('receipt-001.pdf');

use ZentiqLabs\FastPdf\Enums\PaperSize;

$pdf->fromHtml($html)->paperSize(PaperSize::Letter)->output();
$pdf->fromHtml($html)->paperSize('legal')->landscape()->output();



use ZentiqLabs\FastPdf\FastPdf;

$pdf = new FastPdf(['binary' => '/usr/bin/chromium']);
$pdf->fromHtml('<h1>Core PHP</h1>')->save('/tmp/output.pdf');

use ZentiqLabs\FastPdf\FastPdf;

public static function fastPdf(bool $getShared = true): FastPdf
{
    if ($getShared) {
        return static::getSharedInstance('fastPdf');
    }
    return new FastPdf(['binary' => env('FAST_PDF_BINARY')]);
}

service('fastPdf')
    ->fromFile(APPPATH . 'Views/pdf/invoice.php', compact('invoice'))
    ->download('invoice.pdf');

public function __construct(private readonly FastPdf $pdf) {}

public function export(): Response
{
    $bytes = $this->pdf->fromHtml($html)->output();
    return new Response($bytes, 200, ['Content-Type' => 'application/pdf']);
}

// app/Providers/AppServiceProvider.php
use ZentiqLabs\FastPdf\FastPdf;

$this->app->singleton(FastPdf::class, fn () => new FastPdf([
    'binary' => config('services.chromium.binary'),
]));

// In a plugin bootstrap file
df;

function zentiq_pdf(): FastPdf
{
    static $instance;
    return $instance ??= new FastPdf(['binary' => defined('CHROMIUM_BIN') ? CHROMIUM_BIN : null]);
}

$pdf->fromHtml($html)->paper('a4', 'portrait')->output();
$pdf->fromHtml($html)->paper('letter', 'landscape')->save('/tmp/report.pdf');

// Framework-agnostic — inspect headers and body yourself
$response = $pdf->fromHtml($html)->toInlineResponse('report.pdf');

foreach ($response->headers as $name => $value) {
    header("{$name}: {$value}");
}
echo $response->body;

// Or let PdfResponse emit directly
$pdf->fromHtml($html)->toDownloadResponse('invoice.pdf')->send();

$r = $pdf->fromHtml($html)->toDownloadResponse('invoice.pdf');

return new Response($r->body, 200, $r->headers);  // Symfony
return new \Laminas\Diactoros\Response($r->body, 200, $r->headers);  // PSR-7

$pdf = FastPdf::fake();

$pdf->fromHtml('<h1>Invoice #42</h1>')->output();
$pdf->fromView('invoices.pdf', ['id' => 42])->save('/tmp/out.pdf'); // hypothetical

// Assert at least one render occurred
$pdf->assertRendered();

// Assert an exact number of renders
$pdf->assertRenderedCount(1);

// Assert a specific HTML fragment was rendered
$pdf->assertRenderedHtmlContains('Invoice #42');

// Inspect all recorded calls
$calls = $pdf->renderedCalls(); // array of ['html' => ..., 'options' => ...]

// Not needed in most tests, but available if you must assert on the bytes
use ZentiqLabs\FastPdf\Testing\FakePdfEngine;

$engine = new FakePdfEngine();
$engine->returns('%PDF-1.5 custom');
$pdf = new FastPdf([], $engine);
bash
# Auto-discover Chromium
php bin/test-render.php

# Explicit binary path
php bin/test-render.php /usr/bin/chromium-browser