Search code examples
laraveljasperstarter

Does JASPERPHP still works with Laravel 8?


I'm currently using cossou/JasperPHP, and I'm having a problem with generating the pdf file. it just gives me this error all the time Your report has an error and couldn't be processed! Try to output the command using the function output(); and run it manually in the console. I tried changing the ->execute() to ->output() and it gives me the error that it couldn't find a pdf file.

Any other package suggestions for printing and making reports just like Crystal Report or Jaspersoft?

public function dbConfig(){
        //JasperPHP::compile(base_path('/vendor/cossou/jasperphp/examples/hello_world.jrxml'))->execute();
        $jdbc_dir = 'D:\xampp\htdocs\TestTO\vendor\cossou\jasperphp\src\JasperStarter\jdbc';
        return [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST'),
            'port' => env('DB_PORT'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'database' => env('DB_DATABASE'),
            'jdbc_driver' => 'com.microsoft.sqlserver.jdbc.SQLServerDriver',
            'jdbc_url' => 'jdbc:sqlserver://localhost:1433;databaseName=Employee;DataSource=(local)',
            'jdbc_dir' => $jdbc_dir
        ];
    }

    public function generateReport(){
        $jasper = new JasperPHP;

        $extension = 'pdf';
        $name = 'Employee';
        $filename = $name . time();
        $output = base_path('/public/reports/' .$filename);
        // JasperPHP::compile(storage_path('app/public'). '/reports/Employee.jrxml')->execute();

        $jasper->process(
            storage_path('app/public/reports/Employee.jasper'),
            $output,
            array($extension),
            array('id' => 1014),
            $this->dbConfig(),
            "pt_BR"
        )->execute();

        $file = $output . '.' . $extension;

        if(!file_exists($file)){
        }
        if($extension == 'xls'){
            header('Content-Description: Arquivo Excel');
            header('Content-Type: application/x-msexcel');
            header('Content-Disposition: attachment; filename="'.basename($file).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            flush(); // Flush system output buffer
            readfile($file);
            unlink($file) ;
            die();
        }
        else if($extension == 'pdf')
        {
            return response()->file($file)->deleteFileAfterSend();
        }
    }

Solution

  • Ya for me in laravel 8 it works fine....

    I did composer require cossou/jasperphp

    Then in

    File config/app.php

    <?php
    //...
    'providers' => [
        //...
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,
    
        //insert jasper service provider here
        JasperPHP\JasperPHPServiceProvider::class
    ],
    

    Inside web.php i added

     use JasperPHP\JasperPHP as JasperPHP;
    
    
         Route::get('/java', function () {
        
                $jasper = new JasperPHP;
            
                // Compile a JRXML to Jasper
              $t=  $jasper->compile( '/home/midhun/hi/hello.jrxml')->execute();
             
            var_dump($t);
         
                // Process a Jasper file to PDF and RTF (you can use directly the .jrxml)
                $jasper->process(
                    '/home/midhun/hi/hello.jrxml',
                    false,
                    array("pdf", "rtf"),
                    array("php_version" => "8.0.3")
                )->execute();
            
                // List the parameters from a Jasper file.
                $array = $jasper->list_parameters(
                    '/home/midhun/hi/hello.jrxml'
                )->execute();
                var_dump($array);
                return view('welcome');
            });
    

    After Running php artisian serve

    and opening in browser localhost:8000/java

    I got the pdf file

    enter image description here

    and the pdf as

    enter image description here