soap/dompdf-thai

Add Thai fonts for Laravel DomPdf wrapper package

v1.0.0 2023-10-11 04:15 UTC

This package is auto-updated.

Last update: 2024-12-11 08:29:45 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

Add thai font support for barryvdh/laravel-dompdf package. Currently only THSanrabunNew is included.

Installation

You can install the package via composer:

composer require soap/dompdf-thai

Publish the assets (font files) into public/vendor/dompdf-thai

php artisan vendor:publish --tag="dompdf-thai-assets"

Create storage/fonts to store cache files.

mkdir storage/fonts

Usage

Add @DompdfThaiFont inside head of your pdf view layout. You may put in resources/views/layouts/pdf.blade.php file. The following file was adapted from Laravel Breeze Guest layout file.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="csrf-token" content="{{ csrf_token() }}">

        <title>{{ config('app.name', 'Laravel') }}</title>

        <!-- Fonts -->
        <link rel="preconnect" href="https://fonts.bunny.net">
        <link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet" />

        <!-- Scripts -->
        @DompdfThaiFont
        @vite(['resources/css/app.css', 'resources/js/app.js'])
        <style>
            body {
                font-family: 'THSarabunNew';
            }
        </style>
    </head>
    <body class="font-sans antialiased text-gray-900">
        <div class="flex flex-col items-center min-h-screen pt-6 bg-gray-100 sm:justify-center sm:pt-0 dark:bg-gray-900">
            <div>
                <a href="/">
                    <x-application-logo class="w-20 h-20 text-gray-500 fill-current" />
                </a>
            </div>

            <div class="w-full px-6 py-4 mt-6 overflow-hidden bg-white shadow-md sm:max-w-md dark:bg-gray-800 sm:rounded-lg">
                {{ $slot }}
            </div>
        </div>
    </body>
</html>

The following file is in resources/views/products. This file will be used by ProductController to stream pdf content.

<x-pdf-layout>
    <div class="py-12">
        <div class="mx-auto max-w-7xl sm:px-6 lg:px-8">
            <div class="overflow-hidden bg-white shadow-sm dark:bg-gray-800 sm:rounded-lg">
                <div class="p-6 text-gray-900 dark:text-gray-100">
                    ทดสอบ This is a test for pdf
                </div>
            </div>
        </div>
    </div>
</x-pdf-layout>

This is part of the product controller.

  namespace App\Http\Controllers;

  use App\Models\Product;
  use App\Http\Requests\StoreProductRequest;
  use App\Http\Requests\UpdateProductRequest;
  use Barryvdh\DomPDF\Facade\Pdf;

  ....
    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Product $product)
    {
        //
    }

    public function toPdf()
    {
        $pdf = Pdf::loadView('products.pdf'); //, ['products' => Product::all()]);

        return $pdf->stream();
    }

Please consult the barryvdh/laravel-dompdf documentation for detail how to produce pdf from Laravel view.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email prasit.gebsaap@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.