acesseonline / pyreportjasper

Python Reporting with JasperReports
https://pyreportjasper.readthedocs.io/en/latest/
GNU General Public License v3.0
126 stars 72 forks source link

error - using a mysql database - NameError: Error fill report: Erro fill internal: java.lang.NullPointerException #78

Closed gholakemohit closed 3 years ago

gholakemohit commented 3 years ago

i am trying to generate pdf report using mysql database using following code

# -*- coding: utf-8 -*-
import os
from platform import python_version
from pyreportjasper import PyReportJasper

def processing():
    REPORTS_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)),  r'C:\Users\mohit_gholake\JaspersoftWorkspace\Test')
    input_file = os.path.join(REPORTS_DIR, 'Testing.jrxml')
    output_file = os.path.join(REPORTS_DIR, 'Testing')

    conn = {
        'driver': 'mysql',
        'username': 'root',
        'password': 'Root@123',
        'host': 'localhost',
        'database': 'samples',
        'port': '3306',
        'jdbc_dir': 'C:/Users/mohit_gholake/Downloads/mysql-connector-java-5.1.49/mysql-connector-java-5.1.49/mysql-connector-java-5.1.49.jar'
       }

    pyreportjasper = PyReportJasper()

    pyreportjasper.config(
        input_file,
        output_file,
        output_formats=["pdf", "rtf"],
        parameters={'python_version': python_version()},
        db_connection=conn,
        locale='en_US'
    )

    pyreportjasper.process_report()
processing()

giving below error

Traceback (most recent call last):
  File "C:\Users\mohit_gholake\Desktop\mysqlTest.py", line 34, in <module>
    processing()
  File "C:\Users\mohit_gholake\Desktop\mysqlTest.py", line 32, in processing
    pyreportjasper.process_report()
  File "C:\Users\mohit_gholake\AppData\Local\Programs\Python\Python39\lib\site-packages\pyreportjasper\pyreportjasper.py", line 182, in process_report
    raise error
NameError: Error fill report: Erro fill internal: java.lang.NullPointerException
gholakemohit commented 3 years ago

please help asap

gholakemohit commented 3 years ago

i think there is an error in report.py at con = db.get_connection(self.config)

jadsonbr commented 3 years ago

To inform the jar file use the parameter jdbc_driver and not jdbc_dir, since jdbc_dir is to be informed a directory. Probably the documentation must be informing the incorrect parameter, I will find out.

gholakemohit commented 3 years ago

not working @jadsonbr error NameError: Error fill report: Erro fill internal: java.lang.ClassNotFoundException: C:\Users\mohit_gholake\Downloads\mysql-connector-java-5.1.49.jar

i am having jar file at the place but giving error

gholakemohit commented 3 years ago

I am using this link https://pyreportjasper.readthedocs.io/en/latest/userguide.html

in above link they are using postgres.

i have tried posgtres and mysql too.

can you provide me exact steps and code for mysql or postgres.

thanks.

gholakemohit commented 3 years ago

I think there is an error in report.py at con = db.get_connection(self.config)

I traceback to pyreportjasper files, getting error raised from above line

what do you think?

jadsonbr commented 3 years ago

We investigated and came to the conclusion that the abstraction of the jdbc driver by the library was missing and I will be improving in the next version. Answering your question, you can execute by passing the parameters jdbc_driver and jdbc_dir together, where jdbc_driver should contain the name of the driver to be used and jdbc_dir should be informed the directory where your bank's JDBC connection .jar is located. Below is an example.

# -*- coding: utf-8 -*-
import os
from platform import python_version
from pyreportjasper import PyReportJasper

def processing():
    REPORTS_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)),  r'C:\Users\mohit_gholake\JaspersoftWorkspace\Test')
    input_file = os.path.join(REPORTS_DIR, 'Testing.jrxml')
    output_file = os.path.join(REPORTS_DIR, 'Testing')

    conn = {
        'driver': 'mysql',
        'username': 'root',
        'password': 'Root@123',
        'host': 'localhost',
        'database': 'samples',
        'port': '3306',
        'jdbc_driver': 'com.mysql.jdbc.Driver',
        'jdbc_dir': 'C:/Users/mohit_gholake/Downloads/mysql-connector-java-5.1.49/mysql-connector-java-5.1.49/'
       }

    pyreportjasper = PyReportJasper()

    pyreportjasper.config(
        input_file,
        output_file,
        output_formats=["pdf", "rtf"],
        parameters={'python_version': python_version()},
        db_connection=conn,
        locale='en_US'
    )

    pyreportjasper.process_report()
processing()

Here is an example you can see running online.

gholakemohit commented 3 years ago

Now code running without error.

But generating empty pdf while I can see preview of report in JasperSoft Studio is correct.

gholakemohit commented 3 years ago

By the way now I am using json file for creating pdf report.

But you can give the solution for mysql too.

Thanks.

jadsonbr commented 3 years ago

@gholakemohit I showed you an example with the mysql connection running the jrxml that I put in the example there was no sql to show data but you can build one and test and see that it works. Feel free now to create your report as you would like.

renatolimaster commented 2 months ago

I have that code:

`def generate_congregation_report_mysql(request): try:

Definindo o caminho dos arquivos

    input_file = os.path.join(settings.BASE_DIR, 'jasper_reports/reports/congregation', 'congregation.jasper')
    output_file = os.path.join(settings.BASE_DIR, 'jasper_reports/reports/congregation', 'congregation_reports')
    jdbc_driver_path = os.path.join(settings.BASE_DIR, 'jasper_reports/jdbc', 'mysql-connector-j-8.3.0.jar')
    print("jdbc_driver_path:", jdbc_driver_path)
    logger.info(f"input_file: {input_file}")
    logger.info(f"output_file: {output_file}")
    logger.info(f"jdbc_driver_path: {jdbc_driver_path}")

    print(f"input_file: {input_file}")
    print(f"output_file: {output_file}")
    print(f"jdbc_driver_path: {jdbc_driver_path}")

    # Configurando a conexão do banco de dados
    db_connection = {
        'driver': 'mysql',
        'username': 'bla',
        'password': 'bla',
        'host': 'localhost',
        'database': 'bla',
        'port': 3306,
        'jdbc_driver': 'com.mysql.cj.jdbc.Driver',
        # "'jdbc_driver': 'com.mysql.jdbc.Driver', # deprecated
        'jdbc_dir': jdbc_driver_path
    }

    print("connection:", check_db_connection(db_connection))

    pyreportjasper = PyReportJasper()
    pyreportjasper.config(
        input_file=input_file,
        output_file=output_file,
        output_formats=["pdf", "rtf"],
        db_connection=db_connection
    )

    # Processando o relatório
    pyreportjasper.process_report()

    # Lendo o arquivo PDF gerado
    pdf_file_path = f"{output_file}.pdf"
    with open(pdf_file_path, "rb") as pdf_file:
        response = HttpResponse(pdf_file.read(), content_type='application/pdf')
        response['Content-Disposition'] = f'attachment; filename="congregation_reports.pdf"'
        return response

except Exception as e:
    logger.error(f"Error generating congregation report: {e}", exc_info=True)
    return HttpResponseBadRequest(f"Error generating congregation report: {e}")`

    And I got that error:

File "/home/renato/PycharmProjects/BusArrangementAPP/.venv/lib/python3.11/site-packages/pyreportjasper/pyreportjasper.py", line 191, in process_report raise error NameError: Error fill report: Erro fill internal: java.lang.NoClassDefFoundError: org/exolab/castor/xml/MarshalException

Do you have any idea about that?

I'm using python and django, by the way...