laravel - JASPERPHP 是否仍然适用于 Laravel 8?
问题描述
我目前正在使用 cossou/JasperPHP,但在生成 pdf 文件时遇到问题。它只是一直给我这个错误Your report has an error and couldn't be processed! Try to output the command using the function
output(); and run it manually in the console.
我尝试将 to 更改->execute()
为->output()
,它给了我找不到 pdf 文件的错误。
还有其他用于打印和制作报告的软件包建议,例如 Crystal Report 或 Jaspersoft?
public function dbConfig(){
//JasperPHP::compile(base_path('/vendor/cossou/jasperphp/examples/hello_world.jrxml'))->execute();
$jdbc_dir = 'D:\xampp\htdocs\TestTO\vendor\cossou\jasperphp\src\JasperStarter\jdbc';
return [
'driver' => 'sqlsrv',
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'database' => env('DB_DATABASE'),
'jdbc_driver' => 'com.microsoft.sqlserver.jdbc.SQLServerDriver',
'jdbc_url' => 'jdbc:sqlserver://localhost:1433;databaseName=Employee;DataSource=(local)',
'jdbc_dir' => $jdbc_dir
];
}
public function generateReport(){
$jasper = new JasperPHP;
$extension = 'pdf';
$name = 'Employee';
$filename = $name . time();
$output = base_path('/public/reports/' .$filename);
// JasperPHP::compile(storage_path('app/public'). '/reports/Employee.jrxml')->execute();
$jasper->process(
storage_path('app/public/reports/Employee.jasper'),
$output,
array($extension),
array('id' => 1014),
$this->dbConfig(),
"pt_BR"
)->execute();
$file = $output . '.' . $extension;
if(!file_exists($file)){
}
if($extension == 'xls'){
header('Content-Description: Arquivo Excel');
header('Content-Type: application/x-msexcel');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
flush(); // Flush system output buffer
readfile($file);
unlink($file) ;
die();
}
else if($extension == 'pdf')
{
return response()->file($file)->deleteFileAfterSend();
}
}
解决方案
对我来说,在 laravel 8 中它工作正常......
我做了composer require cossou/jasperphp
然后在
文件config/app.php
<?php
//...
'providers' => [
//...
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
//insert jasper service provider here
JasperPHP\JasperPHPServiceProvider::class
],
在 web.php 里面我添加了
use JasperPHP\JasperPHP as JasperPHP;
Route::get('/java', function () {
$jasper = new JasperPHP;
// Compile a JRXML to Jasper
$t= $jasper->compile( '/home/midhun/hi/hello.jrxml')->execute();
var_dump($t);
// Process a Jasper file to PDF and RTF (you can use directly the .jrxml)
$jasper->process(
'/home/midhun/hi/hello.jrxml',
false,
array("pdf", "rtf"),
array("php_version" => "8.0.3")
)->execute();
// List the parameters from a Jasper file.
$array = $jasper->list_parameters(
'/home/midhun/hi/hello.jrxml'
)->execute();
var_dump($array);
return view('welcome');
});
运行 php artisian serve 后
并在浏览器中打开 localhost:8000/java
我得到了pdf文件
和pdf为
推荐阅读
- python - 尝试通过从外部函数调用变量来初始化类中的成员
- angular - 在 Jest 中以 Angular 提交的响应式表单的单元测试
- powershell - 选择对象 - 保留默认列但添加一列
- d3.js - D3 Y 轴有多个标签
- json - 如何使用 MATLAB 将多个嵌套的 JSON 文件转换为单个 CSV 文件?
- javascript - Javascript:“选择”和“点击”行为不符合预期
- date - 如何使用 awk 和 $date 正则表达式今天或前几天的日期?
- networking - WinCe 操作系统中未识别的 USB 设备问题
- reactjs - 在 React JS 中提交表单后如何关闭引导模式?
- td-engine - 获取TDengine数据库查询结果中显示的全文