PHP code example of khmer-pdf / laravel-kh-pdf

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

    

khmer-pdf / laravel-kh-pdf example snippets


use KhmerPdf\LaravelKhPdf\Facades\PdfKh;

class PdfController extends Controller
{
    public function generatePdf()
    {
        $html = view('pdf.template', ['title' => 'សួស្តី ពិភពលោក!'])->render();
        return PdfKh::loadHtml($html)->download('khmer_document.pdf');
    }
}

public function generateCustomPdf()
{
    $html = view('pdf.template', ['title' => 'Custom PDF'])->render();
    PdfKh::loadHtml($html)->addMPdfConfig([
        'mode' => 'utf-8',
        'format' => 'A4-L',
        'margin_top' => 10,
        'margin_bottom' => 10
    ])->download('custom_config.pdf');
}

public function savePdf()
{
    $html = view('pdf.template', ['title' => 'Report'])->render();
    $path = PdfKh::loadHtml($html)->save('pdfs/report.pdf', 'public');
    return response()->json(['pdf_url' => asset('storage/' . $path)]);
}

public function streamPdf()
{
    $html = view('pdf.template', ['title' => 'Live Preview'])->render();
    return PdfKh::loadHtml($html)->stream('live_preview.pdf');
}

public function watermarkPdf()
{
    $html = view('pdf.template', ['title' => 'Secret Document'])->render();
    return PdfKh::loadHtml($html)->watermarkText('Confidential', 0.2, 'khmeros', 100, 45, '#FF0000')->download('watermarked.pdf');
}

public function watermarkImagePdf()
{
    $html = view('pdf.template', ['title' => 'Image Watermark'])->render();
    return PdfKh::loadHtml($html)->watermarkImage('path/to/image.png', 'p', 'p', 1, false)->download('image_watermarked.pdf');
}

public function barcodePdf()
{
    $html = view('pdf.template', ['title' => 'Barcode PDF'])->render();
    return PdfKh::loadHtml($html)->writeBarcode('123456789', 10, 10, true, 1, true)->download('barcode.pdf');
}

'pdf' => [
    'default_font' => 'battambang', // Set your default font here

    // Path to the font files in your public directory
    'font_path' => public_path('fonts/'),

    'font_data' => [
        'battambang' => [ // lowercase letters only in font key
            'R' => 'KhmerOSbattambang.ttf',
            'B' => 'KhmerOSBattambang-Bold.ttf',
            'useOTL' => 0xFF,
        ],
        'khmermuol' => [ // lowercase letters only in font key
            'R' => 'KhmerOSmuol.ttf',
            'useOTL' => 0xFF,
        ],
    ],
],
bash
php artisan vendor:publish --tag=khPdf