khmer-pdf/laravel-kh-pdf 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

khmer-pdf/laravel-kh-pdf

最新稳定版本:v1.0.2

Composer 安装命令:

composer require khmer-pdf/laravel-kh-pdf

包简介

A simple Laravel package for supporting Khmer font in PDFs using mPdf.

README 文档

README

Latest Stable Version Total Downloads License

Laravel PDF with Khmer Font Support using mPDF.

Laravel Khmer PDF

Laravel Khmer PDF is a package designed to simplify PDF generation in Laravel with built-in support for Khmer fonts. It integrates seamlessly with mPDF to create professional-looking PDFs.

Installation

Install the package via Composer:

composer require khmer-pdf/laravel-kh-pdf

Publish the configuration file:

php artisan vendor:publish --tag=khPdf

This will create a config/khPdf.php file where you can customize font settings and mPDF configurations.

Usage

Basic PDF Generation

Use the PdfKh facade to generate PDFs:

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');
    }
}

Methods

The trait provides the following methods:

Method Description Example
loadHtml($html) Sets the HTML content for the PDF. PdfKh::loadHtml(view('pdf.template')->render());
download($filename) Prompts the browser to download the generated PDF. PdfKh::download('file.pdf');
stream($filename) Displays the PDF directly in the browser. PdfKh::stream('file.pdf');
save($path, $disk) Saves the PDF to the specified storage disk. PdfKh::save('pdfs/report.pdf', 'public');
addMPdfConfig($config) Adds custom mPDF configuration settings. PdfKh::addMPdfConfig(['mode' => 'utf-8', 'format' => 'A4-L']);
watermarkText($text, $opacity, $font, $size, $angle, $color, $config) Adds a text watermark to the PDF. PdfKh::watermarkText('Confidential', 0.2, 'khmeros', 100, 45, '#FF0000', []);
watermarkImage($path, $size, $position, $opacity, $behindContent, $config) Adds an image watermark to the PDF. PdfKh::watermarkImage('path/to/image.png', 'p', 'p', 1, false, []);
writeBarcode($code, $horizontal, $vertical, $showIsbn, $size, $border) Writes a barcode in the PDF. PdfKh::writeBarcode('123456789', 10, 10, true, 1, true);

Examples

template.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        p{
            font-size: 25px;
            /* font-family: 'battambang';
            font-weight: bold; */

            font-family: 'khmermuol';
        }
    </style>
</head>
<body>
    <p>សួស្តី ​ពិភពលោក ! Hello World</p>
</body>
</html>

Adding custom mPDF Config

You can configure mPDF settings using the addMPdfConfig method:

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');
}

Saving a 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)]);
}

Streaming a PDF

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

Adding a Text Watermark

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');
}

Adding an Image Watermark

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');
}

Adding a Barcode

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');
}

Configuration

The khPdf.php config file allows you to adjust font paths, default styles, and mPDF options.

Example:

'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,
        ],
    ],
],

License

This package is open-source software licensed under the MIT license.

Contributions

Contributions are welcome! Feel free to submit pull requests or issues on the GitHub repository.

统计信息

  • 总下载量: 678
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-12