pyreportjasper icon indicating copy to clipboard operation
pyreportjasper copied to clipboard

About the exported File can not be open before the jvm terminated fully

Open lypch opened this issue 3 years ago • 1 comments

Hi, Recently, I was tried to use the package to generate the pdf with JSON source. But the time from the program starting to end will take about 10 seconds. it's unreasonable for my situation. Because I will use it as a service which is needed to respond in 1 second. So when I start the service, I start the JVM first. then each time I call the package to generate the pdf, it will be complete in 1 second. So far it looks ok. but I found the file output can not be open with Adobe PDF Reader, which will inform that the file is opened, but if use other tools, like Chrome browser, will not inform the message and the pdf content is correct. I haven't tested other tools. until the JVM instance shutdown, the message was dismissed. So I check the implement detail, I found there is a function named get_output_stream in the Report package. which will get an output stream through the Java FileOutStream class.

    def get_output_stream(self, suffix):
        """
         Return a file-based output stream with the given suffix
        :param suffix:
        :return: FileOutputStream
        """
        if os.path.isdir(self.config.output):
            base = os.path.basename(self.input_file)
            name_file = os.path.splitext(base)[0]
            output_path = os.path.splitext(self.config.output)[0] + name_file + suffix
        else:
            output_path = os.path.splitext(self.config.output)[0] + suffix
        try:
            output_stream = self.FileOutputStream(self.File(output_path))
            return output_stream
        except Exception as ex:
            raise NameError('Unable to create outputStream to {}: {}'.format(output_path, str(ex)))

and the output stream will be used to export the file in the following export serials function, for example:

    def export_pdf(self):
        self.JasperExportManager.exportReportToPdfStream(self.jasper_print, self.get_output_stream('.pdf'))

but I didn't find where to close the output stream. So I think it's key to the issue I encounter. So I made following the change.

    def export_pdf(self):
        outputstream = self.get_output_stream('.pdf')
        self.JasperExportManager.exportReportToPdfStream(self.jasper_print, outputstream)
        outputstream.flush() # if no buffer used, it can be ignored.
        outputstream.close()

after changing, the issue is solved.

Summary:

  1. Using the close method of the OutputFileStream to close the output stream instance after the file exported.
  2. it's better to supply a method to start the JVM. Currently, I export a file before the services to start the JVM.

Thanks a lot.

lypch avatar Feb 11 '22 01:02 lypch

Resolved with version 2.1.3.

jadsonbr avatar Dec 29 '23 11:12 jadsonbr