def generate(self, xmldata, output_type="pdf"):
        """Generate Output with JasperReports."""
        start = time.time()
        xmlpath = os.path.join(TMPDIR, "xml")
        outputpath = os.path.join(TMPDIR, "output")
        oid = "%s-%s" % (md5.new(xmldata).hexdigest(), gen_uuid())
        ensure_dirs([xmlpath, outputpath])
        xmlfile = os.path.join(xmlpath, oid + ".xml")
        fdesc = open(xmlfile, "w")
        fdesc.write(xmldata.encode("utf-8"))
        fdesc.close()

        output_filename = os.path.abspath(os.path.join(outputpath, oid + "." + output_type))
        datasource = JRXmlDataSource(xmlfile, self.xpath)

        # convert to a java.util.Map so it can be passed as parameters
        map = JMap()
        for i in self.compiled_design:
            map[i] = self.compiled_design[i]

        # add the original xml document source so subreports can make a new datasource.
        map["XML_FILE"] = xmlfile

        jasper_print = JasperFillManager.fillReport(self.compiled_design["main"], map, datasource)

        if output_type == "pdf":
            output_file = open(output_filename, "wb")
            JasperExportManager.exportReportToPdfStream(jasper_print, output_file)
            output_file.close()
        elif output_type == "xml":
            output_file = open(output_filename, "w")
            JasperExportManager.exportReportToXmlStream(jasper_print, output_file)
            output_file.close()
        elif output_type == "rtf":
            self.generate_rtf(jasper_print, output_filename)
        elif output_type == "xls":
            self._generate_xls(jasper_print, output_filename)
        elif output_type == "csv":
            self._generate_csv(jasper_print, output_filename)
        elif output_type == "text":
            self._generate_text(jasper_print, output_filename)
        elif output_type == "html":
            self._generate_html(jasper_print, output_filename)
        else:
            raise RuntimeError("Unknown output type %r" % (output_type))
        delta = time.time() - start
        sys.stderr.write("report %r generated in %.3f seconds\n" % (output_filename, delta))
        return open(output_filename, "rb").read()
    def generate(self, xmldata, output_type='pdf'):
        """Generate Output with JasperReports."""
        start = time.time()
        xmlpath = os.path.join(TMPDIR, 'xml')
        outputpath = os.path.join(TMPDIR, 'output')
        oid = "%s-%s" % (md5.new(xmldata).hexdigest(), gen_uuid())
        ensure_dirs([xmlpath, outputpath])
        xmlfile = os.path.join(xmlpath, oid + '.xml')
        fdesc = open(xmlfile, 'w')
        fdesc.write(xmldata.encode('utf-8'))
        fdesc.close()
        
        output_filename = os.path.abspath(os.path.join(outputpath, oid + '.' + output_type))
        datasource = JRXmlDataSource(xmlfile, self.xpath)

        # convert to a java.util.Map so it can be passed as parameters
        map = JMap()
        for i in self.compiled_design:
            map[i] = self.compiled_design[i]

        # add the original xml document source so subreports can make a new datasource.
        map['XML_FILE'] = xmlfile

        jasper_print = JasperFillManager.fillReport(self.compiled_design['main'], map, datasource)

        if output_type == 'pdf':
            output_file = open(output_filename, 'wb')
            JasperExportManager.exportReportToPdfStream(jasper_print, output_file)
            output_file.close()
        elif output_type == 'xml':
            output_file = open(output_filename, 'w')
            JasperExportManager.exportReportToXmlStream(jasper_print, output_file)
            output_file.close()
        elif output_type == 'rtf':
            self.generate_rtf(jasper_print, output_filename)
        elif output_type == 'xls':
            self._generate_xls(jasper_print, output_filename)
        elif output_type == 'csv':
            self._generate_csv(jasper_print, output_filename)
        elif output_type == 'text':
            self._generate_text(jasper_print, output_filename)
        elif output_type == 'html':
            self._generate_html(jasper_print, output_filename)
        else:
            raise RuntimeError("Unknown output type %r" % (output_type))
        delta = time.time() - start
        sys.stderr.write('report %r generated in %.3f seconds\n' % (output_filename, delta))
        return open(output_filename, 'rb').read()
    def generate(self, xmldata, output_type='pdf'):
        """Generate Output with JasperReports."""
        start = time.time()
        xmlpath = os.path.join(TMPDIR, 'xml')
        outputpath = os.path.join(TMPDIR, 'output')
        oid = "%s-%s" % (md5.new(xmldata).hexdigest(), gen_uuid())
        ensure_dirs([xmlpath, outputpath])
        xmlfile = os.path.join(xmlpath, oid + '.xml')
        fdesc = open(xmlfile, 'w')
        fdesc.write(xmldata.encode('utf-8'))
        fdesc.close()
        
        output_filename = os.path.abspath(os.path.join(outputpath, oid + '.' + output_type))
        datasource = JRXmlDataSource(xmlfile, self.xpath)

        # convert to a java.util.Map so it can be passed as parameters
        map = JMap()
        for i in self.compiled_design:
            map[i] = self.compiled_design[i]

        # add the original xml document source so subreports can make a new datasource.
        map['XML_FILE'] = xmlfile

        jasper_print = JasperFillManager.fillReport(self.compiled_design['main'], map, datasource)

        if output_type == 'pdf':
            output_file = open(output_filename, 'wb')
            JasperExportManager.exportReportToPdfStream(jasper_print, output_file)
            output_file.close()
        elif output_type == 'xml':
            output_file = open(output_filename, 'w')
            JasperExportManager.exportReportToXmlStream(jasper_print, output_file)
            output_file.close()
        elif output_type == 'rtf':
            self.generate_rtf(jasper_print, output_filename)
        elif output_type == 'xls':
            self._generate_xls(jasper_print, output_filename)
        elif output_type == 'csv':
            self._generate_csv(jasper_print, output_filename)
        elif output_type == 'text':
            self._generate_text(jasper_print, output_filename)
        elif output_type == 'html':
            self._generate_html(jasper_print, output_filename)
        else:
            raise RuntimeError("Unknown output type %r" % (output_type))
        delta = time.time() - start
        sys.stderr.write('report %r generated in %.3f seconds\n' % (output_filename, delta))
        return open(output_filename, 'rb').read()
Beispiel #4
0
    def handle(self):
        try:
            size = int(self._readline())
            data = self._readbytes(size)
            params = pickle.loads(data)

            props = Properties()
            props.put('user', 'postgres')
            props.put('password', 'postgres')
            
            conn = Driver().connect('jdbc:postgresql:grafiexpress', props)
            h = HashMap()
            for k, v in params["params"].items():

                h.put(k,v)
                
            #h.put(JRParameter.REPORT_LOCALE, java.util.Locale(""))
            print params
            print params["report"]
            print h
            print conn
            report = JasperFillManager.fillReport(params["report"], h, conn)
            output = java.io.ByteArrayOutputStream()
            JasperExportManager.exportReportToPdfStream(report, output)
            output.close()
            data = output.toByteArray()

        except:
            print '\nexcept \n'
            self.request.send("0\n")
            print traceback.print_exc()
        else:
            print '\nelse \n'
            self.request.send(str(len(data))+"\n")
            self.request.send(data)
            print '\nelse \n'
    def generate(self, xmldata, output_type='pdf', sign_keyname=None, sign_reason=None, metadata=None):
        """Generate Output with JasperReports."""
        start = time.time()
        xmldata_utf8 = xmldata.encode('utf-8')
        xmlpath = os.path.join(TMPDIR, 'xml')
        outputpath = os.path.join(TMPDIR, 'output')
        oid = "%s-%s" % (hashlib.md5(xmldata_utf8).hexdigest(), uuid.uuid1())
        ensure_dirs([xmlpath, outputpath])
        xmlfile = os.path.join(xmlpath, oid + '.xml')
        fdesc = open(xmlfile, 'w')
        fdesc.write(xmldata_utf8)
        fdesc.close()

        output_filename = os.path.abspath(os.path.join(outputpath, oid + '.' + output_type))
        datasource = JRXmlDataSource(xmlfile, self.xpath)

        # convert to a java.util.Map so it can be passed as parameters
        map = JMap()
        for i in self.compiled_design:
            map[i] = self.compiled_design[i]

        # add the original xml document source so subreports can make a new datasource.
        map['XML_FILE'] = xmlfile

        jasper_print = JasperFillManager.fillReport(self.compiled_design['main'], map, datasource)

        if output_type == 'pdf':
            # Get PDF content from JasperReports
            stream = ByteArrayOutputStream()
            JasperExportManager.exportReportToPdfStream(jasper_print, stream)

            # Add metadata
            if metadata:
                try:
                    inputstream = ByteArrayInputStream(stream.toByteArray())
                    stream = self.addMetadata(inputstream, metadata)
                except:
                    raise

            # Try to sign the PDF file if a keyname is given
            if sign_keyname:
                try:
                    inputstream = ByteArrayInputStream(stream.toByteArray())
                    stream = self.sign(inputstream, sign_keyname, sign_reason)
                except ValueError:
                    raise

            # Write PDF to output file
            output_file = open(output_filename, 'wb')
            stream.writeTo(output_file)
            output_file.close()

        elif output_type == 'xml':
            output_file = open(output_filename, 'w')
            JasperExportManager.exportReportToXmlStream(jasper_print, output_file)
            output_file.close()
        elif output_type == 'rtf':
            self.generate_rtf(jasper_print, output_filename)
        elif output_type == 'xls':
            self._generate_xls(jasper_print, output_filename)
        elif output_type == 'csv':
            self._generate_csv(jasper_print, output_filename)
        elif output_type == 'text':
            self._generate_text(jasper_print, output_filename)
        elif output_type == 'html':
            self._generate_html(jasper_print, output_filename)
        else:
            raise RuntimeError("Unknown output type %r" % (output_type))
        delta = time.time() - start
        sys.stderr.write('report %r generated in %.3f seconds\n' % (output_filename, delta))
        return open(output_filename, 'rb').read()