Welcome Guest, Not a member yet? Register   Sign In
PHP Java Bridge
#1

[eluser]mmax[/eluser]
Hi,

I'm new to codeigniter and need to generate Jasperreports within the CI Framework. To access Java from PHP i installed php-java-bridge which ich working from a pure php site. But i can't generate reports from CI.

This is how my controller function looks like:
Code:
public function submit() {
    if( $this->checkJavaExtension() ) {
      // Java Extension loaded
                
      try {                    
    // Load Mysql driver and connect to database
    java("java.lang.Class")->forName("com.mysql.jdbc.Driver");
    $driverManager = new JavaClass("java.sql.DriverManager");
    $dbConnection = $driverManager->getConnection("jdbc:mysql://HOST/DATABASE","USER","PASSWORD");
                    
    try {
      $inputFile = "path/to/file.jrxml";
      $outputFile = "path/to/file.pdf";
                        
      // Open jasper report
      $compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
      $report = $compileManager->compileReport($inputFile);
                        
      // Set report parameters
      $params = new Java("java.util.HashMap");
                        
      // Fill report
      $fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
      $jasperPrint = $fillManager->fillReport($report, $params, $dbConnection);
                        
      // Export report to PDF
      $exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
      $exportManager->exportReportToPdfFile($jasperPrint, $outputFile);
                        
      echo "Report generated";
                        
    } catch(JavaException $ex) {
      // ERROR generating Report
      echo "ERROR generating Report: ".$ex->getCause();
    }
                    
      } catch(JavaException $ex) {
    // ERROR connecting to Database
    echo "ERROR connecting to Database: ".$ex->getCause();
      }
                
    } else {
      // ERROR loading Java Extension
      echo "ERROR loading Java Extension";
    }
  }
}

But the the page keeps blank and i don't get any errors in th apache/tomcat logfile. If i run this code in a simple php script, it works's. I also tried to put the code into a library but it's still the same problem. Btw ... the function checkJavaExtension() returns true. Any Ideas?

Thanks,
Max
#2

[eluser]Unknown[/eluser]
Hi mmax,

I have got the same problem - did you perhaps find a work around for this?

Thanks!

R
#3

[eluser]mmax[/eluser]
Hi,

I don't have to use Jasper Reports that way any more. I now directly request the generated PDF document via HTTP url from the Jasper Server where you can set all parameters.

But I also managed to generate Jaspa Reports via PHP-Java-Bridge at the end ... but thats allready a year ago so I can't really remember what was the problem. But I still have my Jasperreport.php Libraray I used with de CI Framework ... hope that will help you:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Jasperreport {
    var $CI;
    var $files;
    var $driver;
    var $connection;
    var $params;
    
    function Jasperreport() {
        $this->CI =& get_instance();
        log_message('debug', "Servicereport Class Initialized");
        
        $this->files = array( 'jrxml' => '', 'html' => '', 'pdf' => '');
        $this->checkJavaExtension();
        $this->params = new Java("java.util.HashMap");
    }
    
    public function setJrxmlFile( $jrxml ) { $this->files['jrxml'] = $jrxml; }
    public function setHtmlFile( $html ) { $this->files['html'] = $html; }
    public function setPdfFile( $pdf ) { $this->files['pdf'] = $pdf; }
    public function setDriver( $driver ) { $this->driver = $driver;    }
    public function setConnection( $connection ) { $this->connection = $connection;    }
    
    public function setParameter( $type, $param, $value ) {
        $this->params->put($param, new Java($type,$value) );
    }
    
    public function runReport() {
        if( $this->checkJavaExtension() ) {
            // Java Extension loaded
            
            try {                    
                // Load driver and connect to database
                java("java.lang.Class")->forName($this->driver);
                $driverManager = new JavaClass("java.sql.DriverManager");
                $dbConnection = $driverManager->getConnection($this->connection);
                
                try {
                    // Open jasper report
                    $compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
                    $report = $compileManager->compileReport($this->files['jrxml']);
                    
                    // Fill report
                    $fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
                    $jasperPrint = $fillManager->fillReport($report, $this->params, $dbConnection);
                    
                    // Export report to PDF
                    $exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
                    
                    if( strlen($this->files['pdf']) ) {
                        $exportManager->exportReportToPdfFile($jasperPrint,$this->files['pdf']);                        
                    }
                    
                    if( strlen($this->files['html'])) {
                        $exportManager->exportReportToHtmlFile($jasperPrint,$this->files['html']);
                    }
                    
                    return true;
                    
                } catch(JavaException $ex) {
                    // ERROR generating Report
                    echo "<b>ERROR generating Report:</b><br/> ".$ex->getCause();
                    return false;
                }
                
            } catch(JavaException $ex) {
                // ERROR connecting to Database
                echo "<b>ERROR connecting to Database:</b><br/> ".$ex->getCause();
                return false;
            }
            
        } else {
            // ERROR loading Java Extension
            echo "<b>ERROR loading Java Extension</b>";
            return false;
        }
        
        return false;
    }
    
    public function checkJavaExtension() {
        $java_bridge_lib = $_SERVER["DOCUMENT_ROOT"]."/javabridge/Java.inc";
        
        if(!extension_loaded('java')) {
            $sapi_type = php_sapi_name();
            //$port = (isset($_SERVER['SERVER_PORT']) && (($_SERVER['SERVER_PORT'])>1024)) ? $_SERVER['SERVER_PORT'] : '8080';
            if ($sapi_type == "cgi" || $sapi_type == "cgi-fcgi" || $sapi_type == "cli")    {
                if(!(PHP_SHLIB_SUFFIX=="so" &&
                    @dl('java.so')) &&
                    !(PHP_SHLIB_SUFFIX=="dll" &&
                    @dl('php_java.dll')) &&
                    !(@include_once($java_bridge_lib)) &&
                    !(require_once($java_bridge_lib)))
                {
                    echo "java extension not installed.";
                    return false;
                }
            } else {
                if(!(@include_once($java_bridge_lib))) {
                    require_once($java_bridge_lib);
                }
            }
        }
        
        if(!function_exists("java_get_server_name"))
        {
            echo "The loaded java extension is not the PHP/Java Bridge";
            return false;
        }
        return true;
    }    
}
?&gt;
#4

[eluser]Unknown[/eluser]
[quote author="mmax" date="1278366007"]Hi,

I'm new to codeigniter and need to generate Jasperreports within the CI Framework. To access Java from PHP i installed php-java-bridge which ich working from a pure php site. But i can't generate reports from CI.

This is how my controller function looks like:
Code:
public function submit() {
    if( $this->checkJavaExtension() ) {
      // Java Extension loaded
                
      try {                    
    // Load Mysql driver and connect to database
    java("java.lang.Class")->forName("com.mysql.jdbc.Driver");
    $driverManager = new JavaClass("java.sql.DriverManager");
    $dbConnection = $driverManager->getConnection("jdbc:mysql://HOST/DATABASE","USER","PASSWORD");
                    
    try {
      $inputFile = "path/to/file.jrxml";
      $outputFile = "path/to/file.pdf";
                        
      // Open jasper report
      $compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
      $report = $compileManager->compileReport($inputFile);
                        
      // Set report parameters
      $params = new Java("java.util.HashMap");
                        
      // Fill report
      $fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
      $jasperPrint = $fillManager->fillReport($report, $params, $dbConnection);
                        
      // Export report to PDF
      $exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
      $exportManager->exportReportToPdfFile($jasperPrint, $outputFile);
                        
      echo "Report generated";
                        
    } catch(JavaException $ex) {
      // ERROR generating Report
      echo "ERROR generating Report: ".$ex->getCause();
    }
                    
      } catch(JavaException $ex) {
    // ERROR connecting to Database
    echo "ERROR connecting to Database: ".$ex->getCause();
      }
                
    } else {
      // ERROR loading Java Extension
      echo "ERROR loading Java Extension";
    }
  }
}

But the the page keeps blank and i don't get any errors in th apache/tomcat logfile. If i run this code in a simple php script, it works's. I also tried to put the code into a library but it's still the same problem. Btw ... the function checkJavaExtension() returns true. Any Ideas?

Thanks,
Max[/quote]

hello mmax, according what you mention above, i would ask about how to integrate CI with PHP/Java Bridge.Last day i've try that but i still got no result...
could you help me...
sorry for my bad english

regards,

pinkpanther
#5

[eluser]mmax[/eluser]
Hi pinkpanther,

I'm from austria, so if you speak german that would be ok for me.

I don't know whats your problem or whats not working ... you should give more detailed explanation.
But It's allready a year ago I used the PHP/JavaBridge with Codeigniter and can't remember much.

Max




Theme © iAndrew 2016 - Forum software by © MyBB