minkbear/phpjasper

PHP 报告生成器

3.3.1 2020-03-18 14:10 UTC

README

PHPJasper logo

PHPJasper

PHP 报告生成器

Build Status Coverage Status Latest Stable Version Minimum PHP Version Total Downloads License PHPStan All Contributors

文档

Language-pt_BR

请考虑 捐赠,支持我们的活动

关于

PHPJasper 是使用 PHP 编译和加工 JasperReports (.jrxml & .jasper 文件) 的最佳解决方案,简单来说:使用 PHP 生成报告。

我们的 Discord 频道

https://discord.gg/7FpDnQ

注意

  • PHPJasper 可以在不考虑您的 PHP 框架的情况下使用
  • 对于低于 7.0 的 PHP 版本,请参阅: v1.16
  • 这里 有几个使用 PHPJasper 的示例

为什么选择 PHPJasper?

您是否曾需要为您的优秀网络应用创建带有大量字段的精美发票?

我必须这样做,但现有的解决方案并不完美。生成 HTML + CSS 以创建 PDF?这毫无意义! :)

然后我发现了 JasperReports,这是最好的开源报告解决方案。

我能用它做什么?

好吧,一切。JasperReports 是一个强大的报告和 BI 工具。

从他们的网站

JasperReports 库是全球最受欢迎的开源报告引擎。它是完全用 Java 编写的,能够使用来自任何类型数据源的数据,并生成像素完美的文档,可以在各种文档格式中查看、打印或导出,包括 HTML、PDF、Excel、OpenOffice 和 Word。

建议使用 Jaspersoft Studio 来构建您的报告,将其连接到数据源(例如 MySQL、POSTGRES),遍历结果并将其输出到 PDF、XLS、DOC、RTF、ODF 等。

您可以做的示例

  • 发票
  • 报告
  • 列表

需求

  • PHP 7.2 或更高版本
  • Java JDK 1.8

可选

  • 任何 jdbc 驱动程序,用于从数据库(MySQL、PostgreSQL、MSSQL...)生成报告,必须复制到文件夹 bin/jasperstarter/jdbc
  • 我们在 bin/jasperstarter/jdbc 目录中提供了 PostgreSQL(42.2.9)。
  • 我们在 bin/jasperstarter/jdbc 目录中提供了 MySQL 连接器(v5.1.48)。
  • Microsoft JDBC Drivers SQL Server.
  • Jaspersoft Studio(用于绘制报告)。

安装

如果没有安装,请安装 Composer

composer require geekcom/phpjasper

或者,在您的文件 'composer.json' 中添加

{
    "require": {
        "geekcom/phpjasper": "^3.2.0"
    }
}

然后只需运行

composer install

就完成了。

PHPJasper 与 Docker

在安装了 Docker CE 和 docker-compose 的情况下,只需运行

  • docker-compose up -d
  • docker exec -it phpjasper composer install

要执行测试

  • docker exec -it phpjasper sudo composer test
  • docker exec -it phpjasper sudo composer testdox

要手动查看测试覆盖率,请执行文件: tests/log/report/index.html

帮助我们编写新的测试,创建一个分支 :)

示例

《Hello World》示例。

请前往仓库根目录下的示例目录(《vendor/geekcom/phpjasper/examples》)。使用 Jaspersoft Studio 或您喜欢的文本编辑器打开 hello_world.jrxml 文件,查看源代码。

编译

首先,我们需要将我们的 JRXML 文件编译成 JASPER 二进制文件。我们只需做一次。

注意1:如果您使用的是 Jaspersoft Studio,则不需要执行此步骤。您可以直接在程序内编译。

require __DIR__ . '/vendor/autoload.php';

use PHPJasper\PHPJasper;

$input = __DIR__ . '/vendor/geekcom/phpjasper/examples/hello_world.jrxml';   

$jasper = new PHPJasper;
$jasper->compile($input)->execute();

此命令将 hello_world.jrxml 源文件编译为 hello_world.jasper 文件。

处理

现在让我们处理之前编译的报告

require __DIR__ . '/vendor/autoload.php';

use PHPJasper\PHPJasper;

$input = __DIR__ . '/vendor/geekcom/phpjasper/examples/hello_world.jasper';  
$output = __DIR__ . '/vendor/geekcom/phpjasper/examples';    
$options = [ 
    'format' => ['pdf', 'rtf'] 
];

$jasper = new PHPJasper;

$jasper->process(
    $input,
    $output,
    $options
)->execute();

现在检查示例文件夹! :) 太棒了!您现在有 2 个文件,hello_world.pdfhello_world.rtf

查看 src/JasperPHP.php 中的 methods compileprocess 以获取更多详细信息

列出参数

查询 jasper 文件以检查给定 jasper 报告文件中可用的参数

require __DIR__ . '/vendor/autoload.php';

use PHPJasper\PHPJasper;

$input = __DIR__ . '/vendor/geekcom/phpjasper/examples/hello_world_params.jrxml';

$jasper = new PHPJasper;
$output = $jasper->listParameters($input)->execute();

foreach($output as $parameter_description)
    print $parameter_description . '<pre>';

使用数据库生成报告

我们还可以指定连接到数据库的参数

require __DIR__ . '/vendor/autoload.php';

use PHPJasper\PHPJasper;    

$input = '/your_input_path/your_report.jasper';   
$output = '/your_output_path';
$options = [
    'format' => ['pdf'],
    'locale' => 'en',
    'params' => [],
    'db_connection' => [
        'driver' => 'postgres', //mysql, ....
        'username' => 'DB_USERNAME',
        'password' => 'DB_PASSWORD',
        'host' => 'DB_HOST',
        'database' => 'DB_DATABASE',
        'port' => '5432'
    ]
];

$jasper = new PHPJasper;

$jasper->process(
        $input,
        $output,
        $options
)->execute();

注意2

有关完整地区列表,请参阅 支持的地区

使用 MSSQL 数据库

require __DIR__ . '/vendor/autoload.php';

use PHPJasper\PHPJasper;

$input = '/your_input_path/your_report.jasper or .jrxml';   
$output = '/your_output_path';
$jdbc_dir = __DIR__ . '/vendor/geekcom/phpjasper/bin/jaspertarter/jdbc';
$options = [
    'format' => ['pdf'],
    'locale' => 'en',
    'params' => [],
    'db_connection' => [
        'driver' => 'generic',
        'host' => '127.0.0.1',
        'port' => '1433',
        'database' => 'DataBaseName',
        'username' => 'UserName',
        'password' => 'password',
        'jdbc_driver' => 'com.microsoft.sqlserver.jdbc.SQLServerDriver',
        'jdbc_url' => 'jdbc:sqlserver://127.0.0.1:1433;databaseName=Teste',
        'jdbc_dir' => $jdbc_dir
    ]
];

$jasper = new PHPJasper;

$jasper->process(
        $input,
        $output,
        $options
    )->execute();

从 XML 生成报告

require __DIR__ . '/vendor/autoload.php';

use PHPJasper\PHPJasper;

$input = '/your_input_path/your_report.jasper';   
$output = '/your_output_path';
$data_file = __DIR__ . '/your_data_files_path/your_xml_file.xml';
$options = [
    'format' => ['pdf'],
    'params' => [],
    'locale' => 'en',
    'db_connection' => [
        'driver' => 'xml',
        'data_file' => $data_file,
        'xml_xpath' => '/your_xml_xpath'
    ]
];

$jasper = new PHPJasper;

$jasper->process(
    $input,
    $output,
    $options
)->execute();

从 JSON 生成报告

require __DIR__ . '/vendor/autoload.php';

use PHPJasper\PHPJasper;

$input = '/your_input_path/your_report.jasper';   
$output = '/your_output_path';

$data_file = __DIR__ . '/your_data_files_path/your_json_file.json';
$options = [
    'format' => ['pdf'],
    'params' => [],
    'locale' => 'en',
    'db_connection' => [
        'driver' => 'json',
        'data_file' => $data_file,
        'json_query' => 'your_json_query'
    ]
];

$jasper = new PHPJasper;

$jasper->process(
    $input,
    $output,
    $options
)->execute();

性能

取决于复杂性、数据量以及您的机器资源(告诉我您的用例)。

我有一个生成带有数据库连接、图片和多页的 发票 的报告,它处理大约需要 3/4 秒。我建议您使用工作器在后台生成报告。

谢谢

Cenote GmbH 提供了 JasperStarter 工具。

JetBrains 提供了 PhpStorm 和所有优秀的工具。

有问题?

打开一个新 问题 或查找已关闭的问题

许可

MIT

贡献

贡献者 ✨

感谢这些可爱的人(emoji key

此项目遵循 all-contributors 规范。欢迎所有类型的贡献!