azakur4/lum-pdf

k98kurz/pdf-lumen (dompdf 包装器) 的兼容性分支,用于 Laravel Lumen

1.0.0 2016-06-20 15:14 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:48:34 UTC


README

为 Laravel Lumen 5.* 的 k98kurz/pdf-lumen 兼容性转换,简单 Dompdf 包装器包(使用 Dompdf 稳定版 0.5)

作者:Azakur4/lum-pdf

安装

azakur4/lum-pdf 添加到 composer.json

"azakur4/lum-pdf": "dev-master"

运行 composer update 以获取最新版本的 Pdf。

现在打开 bootstrap/app.php 并添加服务提供者。

	$app->register('azakur4\Pdf\PdfServiceProvider');

现在添加别名。

    class_alias('azakur4\Pdf\PdfFacade', 'PDF');

配置

将此项目配置文件夹中的默认配置文件复制到您的应用程序根配置文件夹 dompdf.php

现在打开 bootstrap/app.php 并添加此行

   $app->configure('dompdf');

使用方法

显示 PDF

$app->get('/', function () {
	$html = '<html><body>'
			. '<p>Put your html here, or generate it with your favourite '
			. 'templating system.</p>'
			. '</body></html>';
	return PDF::load($html, 'A4', 'portrait')->show();
});

下载 PDF

$app->get('/', function () {
	$html = '<html><body>'
			. '<p>Put your html here, or generate it with your favourite '
			. 'templating system.</p>'
			. '</body></html>';
	return PDF::load($html, 'A4', 'portrait')->download('my_pdf');
});

返回 PDF 字符串

$app->get('/', function () {
	$html = '<html><body>'
			. '<p>Put your html here, or generate it with your favourite '
			. 'templating system.</p>'
			. '</body></html>';
	$pdf = PDF::load($html, 'A4', 'portrait')->output();
});

多个 PDF

for ($i=1;$i<=2;$i++) {
	$pdf = new \k98kurz\Pdf\Pdf();
	$content = $pdf->load(View::make('pdf.image'))->output();
	File::put(public_path('test'.$i.'.pdf'), $content);
}
PDF::clear();

示例

将 PDF 保存到特定文件夹中的文件,然后作为附件发送邮件。由 @w0rldart 提供

define('BUDGETS_DIR', public_path('uploads/budgets')); // I define this in a constants.php file

if (!is_dir(BUDGETS_DIR)){
	mkdir(BUDGETS_DIR, 0755, true);
}

$outputName = str_random(10); // str_random is a [Laravel helper](https://laravel.net.cn/docs/helpers#strings)
$pdfPath = BUDGETS_DIR.'/'.$outputName.'.pdf';
File::put($pdfPath, PDF::load($view, 'A4', 'portrait')->output());

Mail::send('emails.pdf', $data, function($message) use ($pdfPath){
	$message->from('us@example.com', 'Laravel');
	$message->to('you@example.com');
	$message->attach($pdfPath);
});