承接 breuer/laravel-make-pdf 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

breuer/laravel-make-pdf

最新稳定版本:v0.2.1

Composer 安装命令:

composer require breuer/laravel-make-pdf

包简介

Convert HTML to PDF using a headless Chrome instance

README 文档

README

Latest Version on Packagist Total Downloads GitHub Tests Action Status GitHub PHPStan Action Status GitHub Pint Action Status

This package allows you to easily convert HTML to PDF using headless Chrome through Selenium, without needing Node.js. It is inspired by Spatie's laravel-pdf package, which uses BrowserShot and Puppeteer, but our solution offers a more PHP-centric approach using Selenium.

Requirements

Laravel Make PDF requires PHP 8.1+ and Laravel 10+.

Installation & Setup

You can install the package via Composer:

composer require breuer/laravel-make-pdf

After installation, download headless Chrome using the following Artisan command:

php artisan make-pdf:install

To customize the package configuration, publish the configuration file:

php artisan vendor:publish --tag="laravel-make-pdf-config"

Here is the content of the published config file:

return [
    // Configuration options will go here
];

Usage

Converting HTML to PDF with this package is simple and efficient. Below are a few common use cases:

Basic Example

Convert a Blade view to a PDF and stream it to the browser:

use Breuer\MakePDF\Facades\PDF;

Route::get('/', function () {
    return PDF::view('view.name', [])->response();
});

Or force the browser to download the PDF file

return PDF::view('view.name', [])->download();

Options

Render Raw HTML:

Instead of passing a Blade view, you can directly pass HTML:

PDF::html('<h1>Hello World</h1>')

Header and Footer

You can include a view in the header and footer of every page:

PDF::view('view.name', [])
    ->headerView('view.header')
    ->footerView('view.footer')
    ->response();

Alternatively, set raw HTML for the header and footer:

->headerHtml('<div>My header</div>')
->footerHtml('<div>My footer</div>')

In the header or footer, the following placeholders can be used and will be replaced with their print-specific values:

<span class="date"></span>
<span class="title"></span>
<span class="pageNumber"></span>
<span class="totalPages"></span>

Note: The header and footer do not inherit the same CSS as the main content, and the default font size is 0. You should include any required CSS directly in the header/footer. Here’s an example of a styled footer view:

<style>
    footer {
        font-size: 13px;
        color: black;
    }
</style>
<footer>
    <span class="date"></span>
    <span class="pageNumber"></span> / <span class="totalPages"></span>
</footer>

Landscape Orientation

Switch the page orientation to landscape:

use Breuer\MakePDF\Enums\Orientation;

PDF::landscape()

Set Paper Format

Specify a standard paper format:

use Breuer\MakePDF\Enums\Format;

PDF::format(Format::A4)

The following formats are available: LETTER, LEGAL, A0, A1, A2, A3, A4, A5, A6.

Set Custom Paper Size

Set a custom paper size, specifying height and width in inches (or another unit):

use Breuer\MakePDF\Enums\Unit;

PDF::paperSize($height, $width)  // Uses inches by default
PDF::paperSize(29.7, 21, Unit::CENTIMETER)  // Uses centimeters and converts to inches

Set Margins

Set custom margins for the PDF document:

use Breuer\MakePDF\Enums\Unit;

PDF::margins($top, $right, $bottom, $left) // Uses inches by default
PDF::margins(2.54, 1.27, 2.54, 1.27, Unit::CENTIMETER)  // Centimeters, converted to inches

Custom Filename

Define a custom name for the PDF when downloading from the browser. The .pdf extension is automatically appended if omitted:

PDF::view('view.name', [])
    ->name('custom_filename')
    ->response();

Save to File

Use the save method to store the PDF at a given file path:

->save('/path/to/save/yourfile.pdf')

Retrieve PDF as a String

To obtain the raw PDF content as a string, use the raw method:

$content = PDF::view('view.name', [])->raw();

Stream PDF

Display the PDF directly in the browser without saving it to disk:

->response()

Force Download

Prompt the browser to immediately download the PDF:

->download()

License

This package is open-sourced software licensed under the MIT License. Please see the License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-27