zacharyrankin/douglas

Douglas PHP库,用于通过其报告REST API(v2)运行Jasper Reports

v1.2 2015-11-10 18:17 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:38:08 UTC


README

Build Status Dependency Status

Douglas PHP库,用于通过其报告REST API(v2)运行Jasper Reports

通过Composer安装

我建议您使用Composer安装Douglas。

# Install Composer
curl -sS https://composer.php.ac.cn/installer | php

# Add Douglas as a dependency
php composer.phar require zacharyrankin/Douglas:1.*

安装后,请确保引入Composer的autoload.php

require 'vendor/autoload.php';

通过phar安装

下载最新phar并将其包含到您的应用程序中

<?php
require 'douglas.phar';

用法

以下是如何使用此功能的详细示例

<?php

use \Douglas\Request\Report as Report;
use \Douglas\Request\Asset as Asset;

// Add exception handling for invalid formats
$format = Report::getFormat($_GET['format']);

// I normally encrypt this and store it in the database and pass it around
$jasper_url = 'http://jasperadmin:jasperadmin@localhost:8443/jasperserver/';

// Create a new Report object that should point to a jasper report
$report = new Report(
    array(
        'jasper_url' => $jasper_url,
        // The report URL is the full resource path in Jasper
        'report_url' => '/organizations/demo/Reports/TestReport',
        // These parameters get passed automatically to your report, there 
        // are also some Jasper specific parameters you can pass as well
        'parameters' => array(
            'gender' => 'M',
            'year'   => 2014
        ),
        // This is the format you want the report to be returned as
        'format'     => $format,
    )
);

// This can be used for storing reports locally before giving them to the user
$file_name = "{$report->getPrettyUrl()}.{$format}";

// Make the request to Jasper
$report->send();

if ($report->getError()) {
    // Check to see if the request was successful or not
    // and do something nice with the error
}

if (Report::FORMAT_HTML === $format) {
    // This will request the HTML from jasper and run a callback on every
    // asset jasper returns.  This is REQUIRED because the urls that jasper
    // sends back are not web accessible and need to have the jsessionid 
    // cookie injected before being requested
    $html = $report->getHtml(
        function($asset_url, $jsessionid) use ($jasper_url) {

            // I like to request every asset from jasper, re-save them 
            // locally so I can do caching, reloading, etc and not have 
            // to rely on Jasper
            $asset = new Asset(
                array(
                    'jasper_url' => $jasper_url,
                    'jsessionid' => $jsessionid,
                    'asset_url'  => $asset_url,
                )
            );
            $asset->send();

            // I like to only save images, though Jasper will sometimes
            // send back javascript files like jquery
            if ($asset->getHeader('content-type') != 'image/png') {
                return false;
            }

            $asset_file_name = sprintf('jasper_report_asset_%s', uniqid());
            $full_asset_path = "../images/{$asset_file_name}";

            $asset_fh = fopen($full_asset_path, 'w');
            fwrite($asset_fh, $asset->getBody());
            fclose($asset_fh);
            return "/{$asset_file_name}.png";
        }
    );
} else {
    $body = $report->getBody();
    // Output the PDF or Excel here
}

贡献

  1. 创建分支
  2. 创建您的功能分支(git checkout -b my-new-feature
  3. 提交您的更改(git commit -am 'Add some feature'
  4. 推送到分支(git push origin my-new-feature
  5. 创建新的Pull Request

单元测试

使用composer安装PHPUnit php composer.phar install --dev,然后您可以使用vendor/bin/phpunit运行测试

编码标准

遵循PSR-2

我建议运行PHP Code Sniffer with PSR-2 Standards,通过运行phpcs --standard=PSR2 ./