PHPJasper/phpjasper

Usage Examples in Postgres and Oracle

fernandomirassol opened this issue · 3 comments

Hello! I've been utilizing PHPJasper for several years, and I find it to be an excellent library. I would like to propose the addition of more up-to-date examples to facilitate comprehension, especially for those who are just starting out. Below, I've included some straightforward code examples that I've integrated into my application:

The relatorios_entregas function receives information via POST from a modal in the system, along with specific parameters. It validates these details and then returns success or error information to the invoking function, depending on the outcome of the data validation process. In the case of a successful validation, the method is subsequently invoked again using GET. This, in turn, triggers additional functions responsible for generating the PDF and displaying it on a new page.

    function relatorios_entregas(Request $request)
            {
        if ($request->isMethod('GET')) {
            $params = [
                'P_NOTA_INICIO' => $request->nf_inicial,
                'P_NOTA_FIM' => $request->nf_final,
            ];
            return gerar_relatorio_pdf($params, 'entregas_por_separacao.jasper');
        } else {
            $validator = Validator::make($request->all(), [
                'nf_inicial' => 'required|integer',
                'nf_final' => 'required|integer|gte:nf_inicial',
            ]);        
            if ($validator->passes()) {
                return response()->json(['success' => $request->only(['nf_inicial', 'nf_final'])]);
            } else {
                return response()->json(['error' => $validator->errors()]);
            }
        }
    }

    function get_config(string $tipo = 'postgres'): array
    {
        if ($tipo === 'postgres') {
            $driver = env('DB_CONNECTION');
            if ($driver === 'pgsql') {
                $driver = 'postgres';
            }
            return [
                'driver' => $driver,
                'host' => env('DB_HOST'),
                'port' => env('DB_PORT'),
                'username' => env('DB_USERNAME'),
                'password' => env('DB_PASSWORD'),
                'database' => env('DB_DATABASE'),
            ];
        } elseif ($tipo === 'oracle') {
            return [
                'driver' => 'generic',
                'jdbc_driver' => 'oracle.jdbc.driver.OracleDriver',
                'jdbc_url' => sprintf(
                    'jdbc:oracle:thin:@%s:%s:%s',
                    env('DB_HOST_ORA'),
                    env('DB_PORT_ORA'),
                    env('DB_DATABASE_ORA')
                ),
                'username' => env('DB_USERNAME_ORA'),
                'password' => env('DB_PASSWORD_ORA'),
                'jdbc_dir' => '/var/www/gestao_pedidos/vendor/geekcom/phpjasper/bin/jasperstarter/jdbc',
            ];
        }
        return [];
    }



    function gerar_relatorio_pdf($parametros, $arquivoJasper, $apagar = 'S', $exibirTela = 'S')
    {
        $report = new PHPJasper;
        $nome = uniqid();
        $output = storage_path('app/public') . '/relatorios_pdf/relatorio_' . $nome;
        $input  = public_path()  . '/reports_jasper/' . $arquivoJasper;
        $db_config =  getDatabaseConfig('oracle');
        $options = [
            'format' => ['pdf'],
            'params' => $parametros,
            'db_connection' => $db_config
        ];
        $report->process(
            $input,
            $output,
            $options
        )->execute();
        $file = $output . '.pdf';
        $path = $file;
        if (!file_exists($file)) {
            abort(404);
        }
        $file = file_get_contents($file);
        if ($apagar == 'S') {
            unlink($path);
        }
        if ($exibirTela == 'S') {
            return response($file, 200)
                ->header('Content-Type', 'application/pdf');
        }
    }

Thank you very much, this helped me a lot to understand how this library works. By the way, do you know any way to insert images into the report without using a URL? I couldn't find anything related here

I am not aware of any other way besides using a URL.