danieldboendergaard/phantom-pdf

使用 PhantomJS 生成 PDF 文件的包

v3.1.0 2024-03-20 15:51 UTC

README

使用 PhantomJS 生成 PDF 文件的包。该包与框架无关,但提供与 Laravel 的集成。

注意:此包仅包含 64 位 Linux 版本的 PhantomJS。如果您想使用其他版本,可以在配置中引用它。

安装

运行 composer require danielboendergaard/phantom-pdf

用法

$pdf = new PdfGenerator;

// Set a writable path for temporary files
$pdf->setStoragePath('storage/path');

// Saves the PDF as a file (optional)
$pdf->saveFromView($html, 'filename.pdf');

// Returns a Symfony\Component\HttpFoundation\BinaryFileResponse
return $pdf->createFromView($html, 'filename.pdf');

PhantomJS 版本

此包使用包含在包中的 PhantomJS 1.9.8 x64。如果您想使用其他版本,这很简单。

$pdf->setBinaryPath('/some/path/phantomjs');

自定义转换脚本

如果您想使用其他脚本与 PhantomJS 一起执行,这是如何做的。

$pdf->useScript('`/path/to/script');

Laravel 集成

安装

对于 Laravel 5.5,该包支持自动发现,无需任何配置。

对于 Laravel 4,使用 0.10.0 分支。

config/app.php 中的 providers 数组中添加 LaravelServiceProvider

'providers' => [
  PhantomPdf\Laravel\LaravelServiceProvider::class,
]

门面(可选)

将门面添加到 app/config/app.php 中的 aliases 数组中(可选)

'aliases' => [
  'PDF' => PhantomPdf\Laravel\PDFFacade::class,
]

用法

class SampleController extends Controller
{
  public function index()
  {
    return PDF::createFromView(view('index'), 'filename.pdf');
  }

  // Save the pdf to disk
  public function save()
  {
      PDF::saveFromView(view('index'), 'path/filename.pdf');
  }

  // Usa via injection
  public function foo(PdfGenerator $pdf)
  {
    return $pdf->createFromView(view('path'), 'filename.pdf');
  }
}