Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage Examples in Postgres and Oracle #340

Open
fernandomirassol opened this issue Dec 14, 2023 · 3 comments
Open

Usage Examples in Postgres and Oracle #340

fernandomirassol opened this issue Dec 14, 2023 · 3 comments

Comments

@fernandomirassol
Copy link

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');
        }
    }
@LucasESchuster2
Copy link

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

@fernandomirassol
Copy link
Author

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

@triemaryanto
Copy link

usage: jasperstarter process [-h] -f [ ...] [-o ] [-w] [-a []] [-P [ ...]] [-r []] [-t ] [-H ] [-u ] [-p ] [-n ] [--db-sid ] [--db-port ] [--db-driver ] [--db-url ] [--jdbc-dir

] [--data-file ] [--csv-first-row] [--csv-columns ] [--csv-record-del ] [--csv-field-del ] [--csv-charset ] [--xml-xpath ] [--json-query ] [--jsonql-query ] [-N ] [-d] [-s ] [-c ] [--out-field-del ] [--out-charset ] jasperstarter: error: argument -p: expected one argument

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants