Jump to content

Jasper Report Library: how to directly open a pdf report to browser without writing pdf file to disk

Recommended Posts

I have developed an application in PHP that uses JasperReports library and runs a report from PHP. To make PHP and jasper reports library to comunicate, I installed PHP/Java Bridge on Tomcat, so that php application can speak with java.

I made a simple prototype of the php code that comunicate with java and run jasper report. Here the code:

<?php/*  This file is free software: you can redistribute it and/or modify  it under the terms of the GNU General Public License as published by  the Free Software Foundation, either version 3 of the License, or  (at your option) any later version.
This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with This file. If not, see <http://www.gnu.org/licenses/>. */
try {
$jasperxml = new Java("net.sf.jasperreports.engine.xml.JRXmlLoader");
$jasperDesign = $jasperxml->load(realpath("customer.jrxml"));
$query = new Java("net.sf.jasperreports.engine.design.JRDesignQuery");
$query->setText("SELECT customer.first_name AS customer_first_name, customer.last_name AS customer_last_name, customer.email AS customer_email FROM customer customer");
$compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
$report = $compileManager->compileReport($jasperDesign);}
catch (JavaException $ex) { echo $ex;}
$fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
$params = new Java("java.util.HashMap");
$params->put("title", "Customer");
$class = new JavaClass("java.lang.Class");
$driverManager = new JavaClass("java.sql.DriverManager");
$conn = $driverManager->getConnection("jdbc:mysql://localhost:3306/sakila?zeroDateTimeBehavior=convertToNull", "myuser", "mypassword");
$jasperPrint = $fillManager->fillReport($report, $params, $conn);
$exporter = new java("net.sf.jasperreports.engine.JRExporter");
$outputPath = realpath(".") . "/" . "output.pdf";
$exporter = new java("net.sf.jasperreports.engine.export.JRPdfExporter");
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=output.pdf");

The code is correctly runnning. As you can see, actually the code export the report to pdf file and then open the browser. I would like to know how I could directly open the report in the browser without writing the pdf file on disk; in this way I can avoid to give write permission on the directory.



Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Popular Days

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...