quilhasoft/jasperphp

纯PHP库,用于读取由'JasperSoft Studio'制作的JRXML文件并生成PDF报告

dev-master 2024-08-15 19:51 UTC

This package is auto-updated.

Last update: 2024-09-15 20:00:17 UTC


README

alt text

JasperPHP

用于生成由JasperSoft Studio应用程序创建的报告的库
纯PHP库,无需Java服务器或Jasper Server

请考虑捐款以支持我们 Donate

https://jasperphp.net 上查看更多博客、文档等

导出格式

PDF
XLS

支持的标签/组件

* 支持递归和无限制地使用子报告

其他功能

  • 变量的总和、平均值、最小值、最大值
  • 读取和计算子报告变量
  • 对象数组作为输入数据
  • 具有数据替换的HTML渲染的文本字段
  • 活动记录
  • 条件样式也准备好了
  • 支持Laravel DB Facade,在jrxml文件中添加标签 `property name="net.sf.jasperreports.data.adapter" value="laravel.sqlsrv"`,或在JasperSoft Studio的报告属性上编辑默认数据适配器

  • 通用示例

    <?php
    
    use JasperPHP\Report;
    use JasperPHP\ado\TTransaction;
    use JasperPHP\ado\TLogger;
    use JasperPHP\ado\TLoggerHTML;
    
    //use \NumberFormatter;
    //use PHPexcel as PHPexcel; // experimental
    /**
     * classe TJasper
     *
     * @author   Rogerio Muniz de Castro <rogerio@quilhasoft.com.br>
     * @version  2018.10.15
     * @access   restrict
     * 
     * 2015.03.11 -- create
     * 2018.10.15 -- revision and internationalize, add TLogger classes
     * */
    class TJasper {
    
        private $report;
        private $type;
        private $param;
    
        /**
         * method __construct()
         * 
         * @param $jrxml = a jrxml file name
         * @param $param = a array with params to use into jrxml report
         */
        public function __construct($jrxml, array $param) {
            $GLOBALS['reports'] = array();
            $xmlFile = $jrxml;
            $this->type = (array_key_exists('type', $param)) ? $param['type'] : 'pdf';
            //error_reporting(0);
            $this->param = $param;
            $this->report = new JasperPHP\Report($xmlFile, $param); // $GLOBALS['reports'][$xmlFile];
            switch ($this->type) {
                case 'pdf':
                    JasperPHP\Instructions::prepare($this->report);
                    break;
                case 'xls':
                    JasperPHP\Instructions::setProcessor('\JasperPHP\XlsProcessor');
                    JasperPHP\Instructions::prepare($this->report);
                    break;
            }
        }
    
        public function outpage($type = 'pdf') {
            $this->report->generate();
            $this->report->out();
            switch ($this->type) {
                case 'pdf':
                    $pdf = JasperPHP\Instructions::get();
                    $pdf->Output('report.pdf', "I");
                    break;
                case 'xls':
                    header('Content-Type: application/vnd.ms-excel');
                    header('Content-Disposition: attachment;filename="01simple.xls"');
                    header('Cache-Control: max-age=0');
                    // If you're serving to IE 9, then the following may be needed
                    header('Cache-Control: max-age=1');
                    // If you're serving to IE over SSL, then the following may be needed
                    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
                    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
                    header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
                    header('Pragma: public'); // HTTP/1.0
                    $objWriter = PHPExcel_IOFactory::createWriter(JasperPHP\Instructions::$objOutPut, 'Excel5');
                    $objWriter->save('php://output');
                    break;
            }
        }
    
        public function setVariable($name, $value) {
            $this->report->arrayVariable[$name]['initialValue'] = $value;
        }
    
    }
    
    require('autoloader.php');
    require('../../tecnickcom/tcpdf/tcpdf.php'); // point to tcpdf class previosly instaled , (probaly in composer instalations)
    require('../../phpoffice/phpexcel/Classes/PHPExcel.php'); // point to tcpdf class previosly instaled , (probaly in composer instalations)
    //require('../TCPDF/tcpdf.php'); // point to tcpdf class previosly instaled , (probaly in stand alone instalations)
    // on production using composer instalation is not necessaty 
    
    $report_name = isset($_GET['report']) ? $_GET['report'] : 'testReport.jrxml';  // sql into testReport.txt report do not select any table.
    TTransaction::open('dev');
    TTransaction::setLogger(new TLoggerHTML('log.html'));
    $jasper = new TJasper($report_name, $_GET);
    $jasper->outpage();
    ?>

    要求

    • PHP 5.2+
    • "tecnickcom/tcpdf":"6.2.*"
    • "PHPOffice/PHPExcel" 仅限XLS导出

    如何使用此示例

    将数据库连接参数定义在文件 config\dev.ini 中
    查看文件 src\ado\TConection.php 以定义数据库类型
    示例URL
    https:///vendor/quilhasoft/JasperPHP/TJasper.class.php?param1=foo&param2=bar
    传递到URL的URL参数是定义在xmlr文件中的参数。

    使用composer

    将 "quilhasoft/jasperphp":"dev-master" 添加到您的composer配置文件中,并更新/安装

    实时示例

    • 基本测试: 这里
    • 烧毁测试,201页,托管在默认的hostgator服务器上: 这里
    • 巴西付款方式 "boleto" 在 "carne" 中:这里
    • 巴西付款方式 "boleto" 在 A4 中:这里
      ** 巴西boleto项目在QuilhaSoft/JasperPHP-OpenBoleto中可用。

    许可

    • MIT许可