定制 tenthfeet/laravel-pdf 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

tenthfeet/laravel-pdf

最新稳定版本:v1.3.1

Composer 安装命令:

composer require tenthfeet/laravel-pdf

包简介

A portable PDF adapter for Laravel supporting multiple drivers.

README 文档

README

Latest Version on Packagist Total Downloads PHP Version Laravel Version License

A flexible, driver-based PDF generation package for Laravel. This package allows you to decouple your PDF templates from specific libraries (like TCPDF or Dompdf), making your application more maintainable and ready for future engine swaps (e.g., adding Indic font support).

Features

  • Driver-Based Architecture: Switch between engines (TCPDF, Dompdf, etc.) without changing your template logic.
  • Unified API: Clean, Laravel-style Facade for generating documents.
  • Template Inheritance: Simplified base classes for building complex PDF reports.
  • Hook System: Integrated support for Headers, Footers, and Watermarks via simple interfaces.
  • Fluent API: Easily stream or download files.

Requirements

  • PHP: ^8.2
  • Laravel: ^10.0 | ^11.0 | ^12.0 | ^13.0

Installation

You can install the package via composer:

composer require tenthfeet/laravel-pdf

Installation

You can install the package via composer:

composer require tenthfeet/laravel-pdf

Configuration

Publish the configuration file:

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

In config/pdf.php, you can set the default driver and configure individual engine settings:

return [
    'default' => env('PDF_DRIVER', 'tcpdf'),

    'drivers' => [
        'tcpdf' => [
            'class' => \Tenthfeet\Pdf\Adapters\TcpdfAdapter::class,
        ],
        // ...
    ],
];

Usage

1. Create a Template

Extend the PdfDocument class and implement the body method.

namespace App\Pdf\Templates;

use Tenthfeet\Pdf\PdfDocument;
use Tenthfeet\Pdf\Contracts\PdfAdapter;

class MyReport extends PdfDocument
{
    public function body(PdfAdapter $pdf): void
    {
        $pdf->writeHTML("<p>This is the document body.</p>");
    }
}

2. Hooks & Interfaces

You can add headers, footers, or watermarks to your templates by implementing the corresponding interfaces.

Headers & Footers

Implement HasHeader or HasFooter to inject content into every page.

use Tenthfeet\Pdf\Contracts\HasHeader;
use Tenthfeet\Pdf\Contracts\HasFooter;

class MyReport extends PdfDocument implements HasHeader, HasFooter
{
    public function header(PdfAdapter $pdf): void
    {
        $pdf->writeHTML("<h1>Page Header</h1>");
    }

    public function footer(PdfAdapter $pdf): void
    {
        $pdf->SetY(-15);
        $pdf->writeHTML("<p>Page Footnote</p>");
    }

    public function body(PdfAdapter $pdf): void
    {
        // ...
    }
}

Watermarks

Implement HasWatermark to add a background watermark.

use Tenthfeet\Pdf\Contracts\HasWatermark;

class MyReport extends PdfDocument implements HasWatermark
{
    public function watermark(PdfAdapter $pdf, string $text = 'DRAFT'): void
    {
        // You can use the built-in helper from the base class
        parent::watermark($pdf, $text);
    }
}

3. Generate the PDF

Using the Pdf facade:

use Tenthfeet\Pdf\Facades\Pdf;
use App\Pdf\Templates\InvoiceReport;

public function download(Request $request)
{
    $document = new InvoiceReport($request->all());

    return Pdf::make($document)->download('invoice.pdf');
}

3. Swapping Drivers

You can override the driver globally in your .env or per-template by adding a $driver property:

class SpecialReport extends PdfDocument
{
    public ?string $driver = 'dompdf'; // Force this template to use Dompdf
}

License

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固