我想在laravel 5.2中创建柱形图,我希望在PDF文件而不是blade.php文件中输出图表。有没有办法在blade.php上进行计算并在pdf文件上写输出(图表)。我想生成一个学生成绩单模块,其中输出(标记和进度图)将以pdf格式发送给学生。
感谢。
答案 0 :(得分:0)
您可以使用DOMPDF Laravel包装器(https://github.com/barryvdh/laravel-dompdf)。这会将刀片(包含控制器的结果数据)转换为PDF输出。
答案 1 :(得分:0)
我使用 Laravel Snappy做到了。
安装:- 对于X86系统:-
$ composer require h4cc/wkhtmltopdf-i386 0.12.x
$ composer require h4cc/wkhtmltoimage-i386 0.12.x
对于X64系统:-
$ composer require h4cc/wkhtmltopdf-amd64 0.12.x
$ composer require h4cc/wkhtmltoimage-amd64 0.12.x
现在,我们应该自行安装Laravel Snappy。
$ composer require barryvdh/laravel-snappy
然后在更新作曲家之后,将ServiceProvider添加到config / app.php中的providers数组中:-
Barryvdh\Snappy\ServiceProvider::class,
(可选)您可以将Facade用于较短的代码。将此添加到您的外墙:
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
最后,您可以发布配置文件:
$ php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
之后,您需要转到config / snappy.php:-
并且是这样的:-
<?php
return array(
'pdf' => array(
'enabled' => true,
'binary' => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'),
'timeout' => false,
'options' => array(),
'env' => array(),
),
'image' => array(
'enabled' => true,
'binary' => 'vendor/h4cc/wkhtmltoimage-amd64/wkhtmltoimage',
'timeout' => false,
'options' => array(),
'env' => array(),
),
);
设置控制器
$ php artisan make:controller ChartController
为pdf创建新路线; 路线:: get(‘/ chartPdf’,‘ChartController @ index’);
,现在在 ChartController.php中:-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ChartController extends Controller
{
public function index()
{
$pdf = \PDF::loadView('chartjs');
$pdf->setOption('enable-javascript', true);
$pdf->setOption('javascript-delay', 5000);
$pdf->setOption('enable-smart-shrinking', true);
$pdf->setOption('no-stop-slow-scripts', true);
return $pdf->download('chart.pdf');
}
}
您的刀片应该是什么样子
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
.pie-chart {
width: 900px;
height: 500px;
margin: 0 auto;
}
</style>
{{-- make sure you are using http, and not https --}}
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
function init() {
google.load("visualization", "1.1", {
packages: ["corechart"],
callback: 'drawCharts'
});
}
function drawCharts() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Coding', 11],
['Eat', 1],
['Commute', 2],
['Looking for code Problems', 4],
['Sleep', 6]
]);
var options = {
title: 'My Daily Activities',
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
</head>
<body onload="init()">
<div id="piechart" class="pie-chart"></div>
</body>
</html>
此答案基于我在medium.com上的文章,此处是更多信息的链接 https://medium.com/@almestaadmicadiab/laravel-snappy-for-pdf-with-charts-e5c9b757d6dd