PHP code example of crushjs / mini-pdf

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

    

crushjs / mini-pdf example snippets


return MiniPdf::view('invoices.show', compact('invoice'))
    ->landscape()
    ->pageNumbers('ទំព័រ {PAGENO} / {nbpg}')
    ->name("invoice-{$invoice->id}");

use Crushjs\MiniPdf\Facades\MiniPdf;

MiniPdf::view('invoice', ['invoice' => $invoice]);   // Blade view
MiniPdf::html('<h1>Hello</h1>');                      // raw HTML
MiniPdf::file(resource_path('templates/letter.html'));

// or the helper
pdf('invoice', ['invoice' => $invoice]);
pdf()->html('<h1>Hello</h1>');

// From a controller: just return it (shown inline in the browser)
return MiniPdf::view('invoice', $data)->name('invoice-001');

$pdf->download('invoice.pdf');           // force download
$pdf->stream('invoice.pdf');             // show in browser
$pdf->save(storage_path('app/a.pdf'));   // local path
$pdf->save('invoices/a.pdf', disk: 's3'); // filesystem disk
$pdf->render();                          // raw bytes
$pdf->base64();                          // e.g. for email/API payloads

use Crushjs\MiniPdf\Enums\Orientation;
use Crushjs\MiniPdf\Enums\Paper;

$pdf->paper(Paper::A4)                   // A3, A4, A5, Letter, Legal
    ->paper('Letter', Orientation::Landscape)
    ->paper([80, 200])                   // custom size in mm (e.g. receipts)
    ->landscape()                        // or ->portrait()
    ->margins(10)                        // CSS shorthand, in mm
    ->margins(top: 20, right: 10)
    ->font('dejavusans', 12);

$pdf->header('<div style="text-align:right">ACME Co.</div>')
    ->headerView('pdf.header', ['company' => $company])
    ->footerView('pdf.footer')
    ->pageNumbers()                            // "1 / 3"
    ->pageNumbers('Page {PAGENO} of {nbpg}', align: 'right');

$pdf->watermark('CONFIDENTIAL', opacity: 0.1)
    ->watermark('សម្ងាត់')                       // Khmer watermarks work too
    ->watermarkImage(public_path('logo.png'))
    ->password('user-pass', ownerPassword: 'owner-pass', permissions: ['print'])
    ->title('Invoice #001')
    ->author('ACME')
    ->subject('Billing')
    ->keywords(['invoice', 'billing']);

$pdf->when($invoice->isDraft(), fn ($pdf) => $pdf->watermark('DRAFT'));

// AppServiceProvider::boot()
\Crushjs\MiniPdf\Pdf::macro('receipt', fn () => $this->paper([80, 200])->margins(4));

MiniPdf::view('receipt', $data)->receipt()->download();

'khmer' => [
    'enabled' => true,
    'font' => 'battambang',
],

'fonts' => [
    'path' => resource_path('fonts'),
    'families' => [
        'battambang' => [
            'R' => 'Battambang-Regular.ttf',
            'B' => 'Battambang-Bold.ttf',
        ],
    ],
],

use Crushjs\MiniPdf\Facades\MiniPdf;
use Crushjs\MiniPdf\Pdf;

public function test_invoice_can_be_downloaded(): void
{
    $fake = MiniPdf::fake();

    $this->get('/invoices/1/pdf')->assertOk();

    $fake->assertDownloaded('invoice-1.pdf', fn (Pdf $pdf) => $pdf->getView() === 'invoice')
        ->assertSee('Invoice #1');
}

$pdf->option('dpi', 150)
    ->option(['useSubstitutions' => true])
    ->withMpdf(fn (\Mpdf\Mpdf $mpdf) => $mpdf->SetDirectionality('rtl'));

$mpdf = $pdf->toMpdf(); // fully configured instance
bash
php artisan vendor:publish --tag=mini-pdf-config