PHP code example of bogdankharchenko / laravel-pdfmill

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

    

bogdankharchenko / laravel-pdfmill example snippets


use BogdanKharchenko\PdfMill\Enums\Position;
use BogdanKharchenko\PdfMill\Facades\PdfMill;
use BogdanKharchenko\PdfMill\Source;

$pdf = PdfMill::edit('templates/contract.pdf')
    ->fillForm(['client' => 'Acme Inc.', 'agree' => true], flatten: true)
    ->watermark(image: Source::disk('branding/logo.png'), position: Position::BottomRight, scale: 0.15, opacity: 1)
    ->pageNumbers(format: 'Page {page} of {total}')
    ->setMetadata(title: 'Contract 42', copyright: '© 2026 Acme Inc.', custom: ['ContractId' => '42'])
    ->store();

$pdf->url; // signed download link, valid for an hour by default
$pdf->key; // R2 key: the source of your next request

Route::get('/w9', fn () => PdfMill::edit('templates/w9.pdf')
    ->fillForm(['name' => 'Ada Lovelace', 'agree' => true], flatten: true));

$path = "reports/{$report->id}.pdf";

PdfMill::merge($report->pdfUrls())
    ->put(...Storage::disk('s3')->temporaryUploadUrl($path, now()->addMinutes(10)));

Storage::disk('s3')->exists($path); // true

use BogdanKharchenko\PdfMill\MergeSource;

$pdf = PdfMill::merge([
    MergeSource::file($request->file('contract')),
    MergeSource::file($request->file('id_scan')), // images become pages
    'templates/terms.pdf',                        // an R2 key or a URL
])->store();

use BogdanKharchenko\PdfMill\Enums\BuiltInFont;
use BogdanKharchenko\PdfMill\PendingPdf;

return PdfMill::create()
    ->drawText("Quote #{$quote->id}", 72, 760, size: 24, font: BuiltInFont::HelveticaBold)
    ->when($quote->isDraft(), fn (PendingPdf $pdf) => $pdf->watermark(text: 'DRAFT'))
    ->download("quote-{$quote->id}.pdf");

use BogdanKharchenko\PdfMill\Data\InfoResponse;
use BogdanKharchenko\PdfMill\PdfSource;

$info = PdfMill::info('uploads/form.pdf'); // a LockedInfoResponse if it needs a password

if ($info instanceof InfoResponse) {
    foreach ($info->form->fields as $field) {
        echo "{$field->name} ({$field->type->value}): ".json_encode($field->value);
    }
}

$text = PdfMill::text(PdfSource::file($path, password: 'secret'))->pages[0]->text;

use BogdanKharchenko\PdfMill\Facades\PdfMill;
use BogdanKharchenko\PdfMill\Testing\SentRequest;

PdfMill::fake();

$this->post('/contracts/42/sign')->assertOk();

PdfMill::assertSent('edit', fn (SentRequest $pdf) => $pdf->data['source'] === 'templates/contract.pdf'
    && $pdf->hasOperation('fillForm', ['fields' => ['client' => 'Acme Inc.']])
    && $pdf->hasOperation('watermark', ['text' => 'DRAFT']));

PdfMill::fake([
    'merge' => function (SentRequest $merge): void {
        Storage::disk('s3')->put('reports/42.pdf', '%PDF-1.7');
    },
]);

use BogdanKharchenko\PdfMill\Data\ErrorResponse;
use BogdanKharchenko\PdfMill\Exceptions\UnprocessablePdfException;
use BogdanKharchenko\PdfMill\FileResponse;

PdfMill::fake([
    'merge' => ['pageCount' => 12],
    'download' => new FileResponse($csv, 'text/csv', 'totals.csv'),
    'edit' => new UnprocessablePdfException(422, new ErrorResponse('Not a PDF')),
    'info' => fn (SentRequest $request) => $request->data['source'] === 'locked.pdf' ? $locked : null,
]);