
      <?php
      class jasperReport {
      private $conn;
      private $reportsPath, $reportsLibPath;
      private $javaOutputStream;
      function __construct($reportPath, $reportLibPath, $javaBridgePath) {
        $this->initJasperReport($reportPath, $reportLibPath, $javaBridgePath);
      }
      public function isJavaBridge() {
          if (extension_loaded('java')) {
                return true;
          } else {
                return false;
          } 
      }


      public function initJasperReport($reportPath = '', $reportLibPath = '', $javaBridgePath = "http://127.0.0.1:8080/") {
          if ($reportPath != '')
                $this->setReportPath($reportPath);

          if ($reportLibPath != '')
                $this->setReportLib($reportLibPath);

          if (!$this->isJavaBridge()) {
              if (!(require_once($javaBridgePath."JavaBridge/java/Java.inc"))) {
                throw new Exception("Please check Java bridge has been running");
              }
          }
          $java_locale = java("java.util.Locale");
          $java_locale->setDefault($java_locale->KOREAN); //locale

          if ($this->reportsLibPath != '') {
              $java_libs = '';
              $handle = @opendir($this->reportsLibPath);
              while(($fn = readdir($handle)) !== false) {
                  $java_libs .= "file:" . $this->reportsLibPath . "/" . $fn . ";";

              }
              java_require($java_libs);

          }


          $this->javaOutputStream = new java("java.io.ByteArrayOutputStream");

      }

      public function initJDBC($dbDriver, $dbConn, $user, $password) {
          $this->conn = new Java("org.zeronine.japserreports.jdbcConnector");
          $this->conn->setDriver($dbDriver);
          $this->conn->setConnectString($dbConn);
          $this->conn->setUser($user);
         $this->conn->setPassword($password);
      }

      public function compileReport($reportFn) {
          $this->reportFn = $reportFn;
          $sJcm = java("net.sf.jasperreports.engine.JasperCompileManager");
          $report = $sJcm->compileReport($this->reportsPath . "/". $reportFn . ".jrxml");
          return $report;
      }

      public function fillReport($report, $param) {
          if (is_string($report)) {
              $this->reportFn = $report;
              $report = $this->reportsPath . "/". $report . ".jasper";
          }
          $sJfm = java("net.sf.jasperreports.engine.JasperFillManager");
          $parameters = new Java("java.util.HashMap", $param);
          $print = $sJfm->fillReport($report, $parameters, $this->conn->getConnection());
          return $print;

      }

      public function outputObject($report) {
          set_time_limit(120);
          java_set_file_encoding("ISO-8859-1");
          $stream = new java("java.io.ObjectOutputStream", $this->javaOutputStream);
          $stream->writeObject($report);
          $stream->flush();
          $stream->close();
          header('Content-Type: application/octet-stream');
          header('Content-Transfer-Encoding: binary');
          header('Pragma: no-cache');
          header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
          header('Expires: 0');
          echo java_cast($this->javaOutputStream->toByteArray(),"S");
      }

      public function outputPDF($report) {
          set_time_limit(120);
          java_set_file_encoding("ISO-8859-1");
          $exporter = new java("net.sf.jasperreports.engine.export.JRPdfExporter");
          $exParm = java("net.sf.jasperreports.engine.JRExporterParameter");
          $exporter->setParameter($exParm->JASPER_PRINT, $report);
          $exporter->setParameter($exParm->OUTPUT_STREAM, $this->javaOutputStream);
          $exporter->exportReport();
          header('Content-Type: application/pdf');
          header('Content-Transfer-Encoding: binary');
          header('Content-disposition: attachment; filename="'.$this->reportFn.'.pdf"');
          header('Pragma: no-cache');
          header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
          header('Expires: 0');
          echo java_cast($this->javaOutputStream->toByteArray(),"S");
      }

      public function outputXLS($report) {
          set_time_limit(120);
          java_set_file_encoding("ISO-8859-1");
          $exporter = new java("net.sf.jasperreports.engine.export.JRXlsExporter");
          $exParm = java("net.sf.jasperreports.engine.JRExporterParameter");
          $exXlsParm = java("net.sf.jasperreports.engine.export.JRXlsExporterParameter");
          $exporter->setParameter($exParm->JASPER_PRINT, $report);
          $exporter->setParameter($exParm->OUTPUT_STREAM, $this->javaOutputStream);
          $exporter->setParameter($exXlsParm->IS_ONE_PAGE_PER_SHEET, true);
          $exporter->setParameter($exXlsParm->IS_DETECT_CELL_TYPE, true);
          $exporter->exportReport();
          header('Content-Type: application/xls');
          header('Content-Transfer-Encoding: binary');
          header('Content-disposition: attachment; filename="'.$this->reportFn.'.xls"');
          header('Pragma: no-cache');
          header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
          header('Expires: 0');
          echo java_cast($this->javaOutputStream->toByteArray(),"S");
      }

      public function setReportPath($path) {
            $this->reportsPath = $path;
      }

      public function getReportPath() {
             return $this->reportsPath;
      }

      public function setReportLib($path) {
             $this->reportsLibPath = $path;
      }

      public function getReportLib() {
             return $this->reportsLibPath;
      }

      }