andrelokal/pdf-lumen-bundle

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

2.0.6 2019-09-19 19:21 UTC

This package is not auto-updated.

Last update: 2024-09-28 11:04:53 UTC


README

更新DOMPDF 09/2019

k98jurz/pdf-lumen的兼容性分支,它将Thujohn/Pdf转换为Laravel Lumen 5.*的简单Dompdf包装器包(使用Dompdf稳定版本0.5)

Lumen安装 >= 5.4.*

tibonilab/pdf-lumen-bundle添加到composer.json

"tibonilab/pdf-lumen-bundle": "2.0.0"

Lumen安装 <= 5.3.*

tibonilab/pdf-lumen-bundle添加到composer.json

"tibonilab/pdf-lumen-bundle": "1.0.0"

在为您的Lumen安装要求正确的版本标记后,运行composer update以拉取Pdf的最新版本。

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

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

并添加别名。

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

用法

显示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);
});