Pyjasper shows error (NameError: Invalid resource directory!)

329 views Asked by At

I'm trying to generate .jrxml by using python with pyreport library, but it shows error when I'm using .exe converted from .py using Pyinstaller.

Pyinstaller version is 3.5, pyJasper version is 0.41, pyreportJasper version is 1.0.2 and Python version is 3.7.1.

It's able to generate files (pdf,xlms) when I used Pycharm to run .py file with a warning "Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.". According to this link: "Loading class com.mysql.jdbc.Driver ... is deprecated" message, I think this is not the cause but it's only FYI.

The example of fileName is "115kV Line No.1_2019-08-25".

Here's the code.

def createFolder(output_file, folderName):
    print("createFolder function started...")
    try:
        os.mkdir(output_file + folderName)
    except OSError:
        print("Creation of the directory %s failed." % output_file)
    else:
        print("Successfully created the directory %s." % output_file)


def jasperReport(input_file, output_file, reportParameters, folderName, fileName, fileFormat):
    print("JasperReport function started...")
    con = {
        'driver': 'mysql',
        'username': 'root',
        'password': 'root',
        'host': 'localhost',
        'database': 'cscs_prj',
        'port': '3306'
    }
    createFolder(output_file, folderName)
    output = output_file + folderName + "/" + fileName
    jasper = pyreportjasper.JasperPy()
    jasper.process(
        input_file,
        output_file=output,
        format_list=fileFormat,
        parameters=reportParameters,
        db_connection=con,
        locale='en_US'  # LOCALE Ex.:(en_US, de_GE)
    )

However, when I execute .exe, it shows an error below.

  File "JasperReport.py", line 37, in jasperReport
  File "site-packages\pyreportjasper\jasperpy.py", line 151, in process
  File "site-packages\pyreportjasper\jasperpy.py", line 198, in execute
NameError: Invalid resource directory!
[5288] Failed to execute script Executer

Do you guys have any idea why the error occurred?

1

There are 1 answers

0
Kritkorn Supyen On BEST ANSWER

I found out that the problem came from pyinstaller is not include .jar and jasperstarter.exe. I solve this problem by defining the .jar directory with jdbc_dir in db_connection and jasperstarter.exe directory with jasper.path_executable.

Here is the example below.

def jasperReport(input_file, output_file, reportParameters, folderName, fileName, fileFormat, mySQLConfig, jasperConfig):
    print("JasperReport function started...")
    con = {
        'driver': 'mysql',
        'username': 'root',
        'password': 'root',
        'host': 'localhost',
        'database': 'cscs_prj',
        'jdbc_dir': 'C:/SCPS_PRJ/JasperStarter/jdbc/',
        'port': '3306'

    }
    createFolder(output_file, folderName)
    output = output_file + folderName + "/" + fileName
    jasper = pyreportjasper.JasperPy()
    jasper.path_executable = "C:/SCPS_PRJ/JasperStarter/bin/"
    jasper.process(
        input_file,
        output_file=output,
        format_list=fileFormat,
        parameters=reportParameters,
        db_connection=con,
        locale='en_US'  # LOCALE Ex.:(en_US, de_GE)
    )

To make this example works, please don't forget to put .jar in "C:/SCPS_PRJ/JasperStarter/jdbc/" folder ,and jasperstarter.exe in "C:/SCPS_PRJ/JasperStarter/bin/" folder.