PHP code example of nadar / pdf-generator

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

    

nadar / pdf-generator example snippets


namespace App\Pdf;

use Nadar\PdfGenerator\AbstractPdfSettings;
use Nadar\PdfGenerator\Font\FontSet;
use Nadar\PdfGenerator\Overflow;
use Nadar\PdfGenerator\Value\Color;

final class BrandPdfSettings extends AbstractPdfSettings
{
    public function fontPath(): string      { return __DIR__ . '/fonts'; }
    public function fontCachePath(): string { return __DIR__ . '/fonts/cache'; }
    public function templatePath(): string  { return __DIR__ . '/templates'; }

    public function fonts(): FontSet
    {
        return FontSet::make()
            ->family('inter', 'Inter-Regular.ttf', 'Inter-Bold.ttf')
            ->role('regular', 'inter')
            ->role('bold', 'inter', 'bold');
    }

    public function textColor(): Color   { return Color::hex('#223764'); }

    // the safe choice for anything data-driven: never throws on a long value
    public function overflow(): Overflow { return Overflow::ShrinkThenClip; }
}

use Nadar\PdfGenerator\{Align, ImageBox, Layout, PdfGenerator, QrBox, TextBox};
use Nadar\PdfGenerator\Value\Color;

$row = Layout::fromArray([
    ['id' => 'title', 'x' => 53.2, 'y' => 58.48, 'w' => 120, 'h' => 11.5, 'font' => 'bold', 'size' => 24, 'maxLines' => 1],
    ['id' => 'meta',  'x' => 53.2, 'y' => 70.11, 'w' => 120, 'h' => 9.5,  'font' => 'bold', 'size' => 19, 'maxLines' => 1],
])->with(
    ImageBox::circle('photo', cx: 30.34, cy: 68.3, diameter: 30.85, placeholder: Color::hex('#ff920c')),
    new QrBox('link', x: 179, y: 58.63, size: 19.5, color: Color::hex('#223764')),
);

$pdf = (new PdfGenerator(new BrandPdfSettings()))
    ->title('June highlights')
    ->page('poster-template.pdf');

$pdf->write(new TextBox('month', x: 20, y: 28.35, w: 170, size: 25, align: Align::Center), 'June');

foreach ($row->repeat(times: 6, dy: 40.3) as $index => $slots) {
    $event = $events[$index] ?? null;
    if ($event === null) {
        break;
    }

    $pdf->writeAll($slots, $event);                        // text slots
    $pdf->image($slots->image('photo'), $event['image']);  // image slot
    $pdf->qr($slots->qr('link'), $event['url']);           // code slot
}

file_put_contents('poster.pdf', $pdf->bytes());

new TextBox('title', x: 53.2, y: 58.48, w: 120, size: 24, anchor: Anchor::Baseline);

$metrics = $pdf->probe($titleBox, $event['title']);
// $metrics->baseline, ->size (after any shrink), ->lines, ->box
bash
php examples/template-overlay-poster.php