PHP code example of vaneetjoshi / laravel-pdf

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

    

vaneetjoshi / laravel-pdf example snippets


/* config/pdf.php */
'custom_font_path' => base_path('resources/fonts/'),
'custom_fonts' => [
    'poppins' => [
        'R'  => 'Poppins-Regular.ttf',    // Regular
        'B'  => 'Poppins-Bold.ttf',       // Bold
        'I'  => 'Poppins-Italic.ttf',     // Italic
        'BI' => 'Poppins-BoldItalic.ttf'  // Bold Italic
    ],
    // Our built-in Devanagari support:
    'devanagari' => [
        'R' => 'devanagari.ttf', 
        'useOTL' => 0xFF, // Required for complex Hindi/Sanskrit ligatures
    ],
],




namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Vaneetjoshi\LaravelPdf\Facades\Pdf;

class InvoiceController extends Controller
{
    public function generate(Request $request)
    {
        $headerHtml = '
        <div style="border-bottom: 2px solid #000; padding-bottom: 5px;">
            <strong style="font-size: 20px; color: #1a56db;">TRADE VISION VENTURES</strong>
        </div>';

        $footerHtml = '
        <div style="border-top: 1px solid #000; font-size: 10px; text-align: right;">
            Page {PAGENO} of {nbpg}
        </div>';

        $data = [
            'invoice_no' => 'TVV-2026-0803',
            'date' => now()->format('d-M-Y'),
            'total' => 50000
        ];

        return Pdf::paperSize('A4')
            ->orientation('P')
            ->margins(top: 25, right: 15, bottom: 20, left: 15)
            // Add a sleek corporate border
            ->addEdgeBorder([
                'border_line_color' => '#1a56db',
                'border_line_width' => '2px',
                'border_size_mm'    => 5.0,
                'padding_mm'        => 2.0,
            ])
            ->headerHtml($headerHtml, 15)
            ->footerHtml($footerHtml, 10)             ->loadView('pdf.invoice',$data)
            ->stream('Invoice_' . $data['invoice_no'] . '.pdf');
    }
}


use Vaneetjoshi\LaravelPdf\Facades\Pdf;
use Illuminate\Support\Facades\Mail;

public function sendInvoiceEmail($user)
{
    $data = ['name' =>$user->name, 'amount' => 1500];

    // 1. Generate the raw PDF binary string in memory
    $pdfData = Pdf::paperSize('A4')
        ->loadView('pdf.invoice', $data)
        ->output();

    // 2. Attach the raw data directly to the email
    Mail::send('emails.billing', $data, function($message) use ($user,$pdfData) {
        $message->to($user->email)
                ->subject('Your Recent Invoice')
                ->attachData($pdfData, 'Invoice.pdf', [
                    'mime' => 'application/pdf',
                ]);
    });

    return response()->json(['message' => 'Invoice emailed successfully!']);
}


return Pdf::paperSize('A4')
    ->withSanskrit() // Injects .sanskrit-text CSS and enables OTL
    ->addEdgeBorder([
        'border_bg_color'   => '#ffccbc',
        'border_size_mm'    => 12.0,
        // Draw repeating 'Om' symbols around the entire page edge
        'top' => ['text' => 'ॐ', 'color' => '#bf360c', 'font_size' => 16, 'with_sanskrit' => true, 'repeat' => true, 'gap' => 12.0],
        'bottom' => ['text' => 'ॐ', 'color' => '#bf360c', 'font_size' => 16, 'with_sanskrit' => true, 'repeat' => true, 'gap' => 12.0],
        'left' => ['text' => 'ॐ', 'color' => '#bf360c', 'font_size' => 16, 'with_sanskrit' => true, 'repeat' => true, 'gap' => 12.0],
        'right' => ['text' => 'ॐ', 'color' => '#bf360c', 'font_size' => 16, 'with_sanskrit' => true, 'repeat' => true, 'gap' => 12.0]
    ])
    ->pageBackground('#fffbf0', '[https://example.com/texture.png](https://example.com/texture.png)', '4', 'center center')
    ->loadHtml('<h1 class="sanskrit-text" style="text-align:center;">ॐ गं गणपतये नमः</h1>')
    ->stream('Kundli.pdf');


use Vaneetjoshi\LaravelPdf\PdfBuilder;

$builder = new PdfBuilder();


Pdf::paperSize('Legal');


Pdf::orientation('L');


Pdf::margins(top: 30, right: 10, bottom: 20, left: 10);


Pdf::displayMode('fullwidth');


Pdf::loadView('pdf.invoice', ['invoiceNumber' => 'TVV-2026-001', 'total' => 15000]);


Pdf::loadHtml('<h1>Tax Invoice</h1><p>Thank you for your purchase.</p>');


Pdf::addStylesheet(public_path('css/pdf-invoice.css'));


Pdf::withSanskrit()->loadHtml('<p class="sanskrit-text">ॐ गं गणपतये नमः</p>');


$header = '<div style="font-family: devanagari, sans-serif; color: red;">ॐ नमः शिवाय</div>';
Pdf::headerHtml($header, offsetY: 15, offsetX: 10);


Pdf::footerHtml('<div style="text-align: center;">Company Registration No. 03AAAAA0000A1Z5</div>', offsetY: 10);


Pdf::showPageNumbers('bottom-center', 'Page {PAGENO} / {nbpg}');


[
    'border_bg_color'   => '#ffffff', // (string|null) Outer track hex color
    'border_line_color' => '#000000', // (string) Inner framing line hex color
    'border_line_width' => '1px',     // (string) CSS line thickness ('0' for none)
    'border_size_mm'    => 12.0,     // (float) Total border thickness in mm
    'padding_mm'        => 2.0,      // (float) Distance between line and track in mm
    'inner_margin_mm'   => 5.0,      // (float) Safe margin between line and body text

    // Edge Sections: 'top', 'bottom', 'left', 'right'
    'top' => [
        'text'          => 'ॐ',      // (string) Text string or character to render
        'image'         => '',       // (string) URL or path to image
        'align'         => 'center', // (string) 'left', 'center', 'right'
        'color'         => '#000000',// (string) Text hex color
        'font_size'     => 12,       // (int) Font size in px
        'with_sanskrit' => true,     // (bool) Enable Devanagari font for this edge
        'repeat'        => true,     // (bool) Repeat text/symbol across the edge length
        'gap'           => 10.0      // (float) Spacing between repeated items in mm
    ]
]


Pdf::addEdgeBorder([
    'border_bg_color'   => '#ea5151eb',
    'border_line_color' => '#000000',
    'border_line_width' => '1.5px',
    'border_size_mm'    => 12.0,
    'top' => ['text' => 'ॐ', 'color' => '#ffffff', 'font_size' => 16, 'with_sanskrit' => true, 'repeat' => true, 'gap' => 15.0]
]);


Pdf::pageBackground(color: '#fffbf0', imageUrl: '[https://example.com/paper-texture.png](https://example.com/paper-texture.png)');


Pdf::watermarkText('CONFIDENTIAL DRAFT', 0.15);


Pdf::watermarkImage(public_path('images/watermark-logo.png'), 0.1);


Pdf::protect('secret_password_123', ['print']); // Permits printing, blocks copying


return Pdf::loadView('pdf.invoice')->stream('Tax_Invoice.pdf');


return Pdf::loadView('pdf.invoice')->download('Tax_Invoice.pdf');


$success = Pdf::loadView('pdf.invoice')->saveToDisk('s3', 'invoices/2026/INV-001.pdf');


$base64String = Pdf::loadView('pdf.invoice')->base64();

return response()->json([
    'status' => 'success',
    'pdf_base64' => $base64String
]);


$pdfBinary = Pdf::loadView('pdf.invoice')->output();

Mail::send('emails.invoice', $data, function ($message) use ($pdfBinary) {$message->to('[email protected]')
            ->subject('Your Invoice')
            ->attachData($pdfBinary, 'Invoice.pdf', ['mime' => 'application/pdf']);
});

bash
php artisan vendor:publish --tag=pdf-config