Skip to content

Introduction

PHP Version Build Status codecov Scrutinizer Code Quality PHPStan License

PDF report generation with jasper reports from PHP

What is Jasperreports ?

JasperReports is a template based opensource reporting library popular amongst Java developers. It comes with a pretty decent visual designer Jasperstudio and supports an impressive list of features such as barcode, fonts, layout, pagination, page size, orientation, columns, precise text spacing / wrapping, charts, subreports, svg, translations... and allow creation of sophisticated reports. It's opinionated and requires to catch few principles, like report bands, parameters, datasources, subreports...

What's the use case ?

Whenever you need to achieve near pixel-perfect, semi-complex reports layouts that would simply make no sense to develop with pure-php or puppeteer / headless chrome based approaches. Think of a product catalog, a complex invoice, tracking labels...

What is soluble-jasper ?

soluble-jasper relies on a network based bridge to the JVM and allows to manipulate the report creation from the PHP side. Basically you just run the jrxml template designed with JasperStudio:

<?php declare(strict_types=1);

use Soluble\Japha\Bridge\Adapter as JavaBridgeAdapter;
use Soluble\Jasper\{ReportRunnerFactory, Report, ReportParams};
use Soluble\Jasper\DataSource\JavaSqlConnection;
use Soluble\Jasper\Exporter\PDFExporter;

// Create a connection to the Jasper bridge.
$reportRunner = ReportRunnerFactory::getBridgedReportRunner(
    new JavaBridgeAdapter([
        'servlet_address' => 'localhost:8080/JasperReports/servlet.phpjavabridge'    
    ])
);

$report = new Report(

     // Set the report template file
     '/path/my_report.jrxml',

     // Param values that sent to the report template
     // i.e. <textFieldExpression><![CDATA[$P{BookTitle}]]></textFieldExpression> 
     new ReportParams([
            'BookTitle'    => 'Soluble Jasper',
            'BookSubTitle' => 'Generated on JVM with Jasper reports'
     ]),

     // Set a database connection if you're using queries in your 
     // report
     new JavaSqlConnection(
         'jdbc:mysql://localhost/my_db?user=user&password=password',
         'com.mysql.jdbc.Driver'
     )
);

// Step 3: Export the report

$pdfExporter = new PDFExporter($report, $reportRunner);
$pdfExporter->saveFile('/path/my_report_output.pdf', [
    // PDF metadata
    'author' => 'John Doe',
    'title' => 'My document'
]);

This approach have few drawbacks (the main one is having to install a bridge server running jasper report on the JVM) but offers a great level of flexibility and speed (bridged api, no cli, no startup time). See comparison of alternatives.

Features

  • Report generation in PDF (other formats can be supported, open an issue)
  • Datasources for JDBC, JSON and XML (url or filesystem)
  • Support for PSR-7 responses (stream)

Requirements

  • PHP 7.1+
  • JasperBridge (see install)
  • Java

Alternatives

JasperReport based

PHP based

Others

Convert HTML, Markdown, Office to PDF

  • Gotenberg A Docker-powered stateless API for converting HTML, Markdown and Office documents to PDF
  • Unoconv

Coding standards and interop