def build_report(self) -> Tuple[bool, Union[io.BytesIO, None]]: """ Assembles, builds and renders the report file In case of errors, response is (False, io.BytesIO(response_message)), with headers already processed In case of success, response is (True, io.BytesIO()); response should be processed from the zpt path for the report file :return: (bool, io.BytesIO) """ global _zpt _zpt = None try: # load file or build file, according to path if self.is_report_file: sys.stdout.write("Reloading report file...\n") _zpt = ReportFileLoader.load_file(self.report_path) else: sys.stdout.write("Rebuilding report from path...\n") bresult, zpt = ReportFileBuilder.build_zipfs(self.report_path) if not bresult.success(): return False, self.error_500(";".join( bresult.get_errors())) # create ReportFile from zipfs _zpt = ReportFileLoader.load_zipfs(zpt) # render template to REPORT_FILE_NAME # returns a dummy BytesIO object JinjaRender(_zpt).render() return True, io.BytesIO() except Exception as e: return False, self.error_500(str(e))
def build_zpt(self): zptfile = Path(self.temp_dir) / 'test.zpt' result = ReportFileBuilder.build_file(RPT_FILTER_EXAMPLE_PATH, zptfile) assert result.success() is True zpt = ReportFileLoader.load_file(zptfile) assert zpt is not None return zpt
def list(self, args) -> bool: if len(args) > 1: self.error("Error: Invalid command syntax") return False if len(args) == 0: path = Path(os.getcwd()) else: path = Path(args[0]) if not path.exists(): self.error("Error: Invalid path") return False if not path.is_dir(): self.error("Error: Path is not a valid directory") return False for (dirpath, dirnames, filenames) in os.walk(path): for f in filenames: if f.endswith('.zpt'): try: zpt = ReportFileLoader.load_file(f) print(self.LIST_LINE % (f, zpt.get_param(const.MANIFEST_TITLE, ""))) except Exception: # ignore file pass return True
def build_zpt(self, report: str): zptfile = Path(self.temp_dir) / 'test.zpt' result = ReportFileBuilder.build_file(report, zptfile) assert result.success() is True zpt = ReportFileLoader.load_file(zptfile) assert zpt is not None return zpt
def generate_pdf_cli(zipreport_cli:str, report: str, data: dict, output_file: str) -> bool: zpt = ReportFileLoader.load(report) # load zpt file JinjaRender(zpt).render(data) # render jinja template with parameters job = ReportJob(zpt) # create new rendering job job.set_jsevent(True) # report uses event hook job.set_jsevent_timeout(500) job.set_process_timeout(600) job.set_render_timeout(600) result = ZipReportCli(zipreport_cli).render(job, data) if result.success: with open(output_file, 'wb') as rpt: rpt.write(result.report.read()) return True return False
def generate_pdf_server(report:str, data: dict, output_file:str) -> bool: zpt = ReportFileLoader.load(report) # load zpt file JinjaRender(zpt).render(data) # render jinja template with parameters job = ReportJob(zpt) # create new rendering job job.set_jsevent(True) # report uses event hook job.set_jsevent_timeout(500) job.set_process_timeout(600) job.set_render_timeout(600) client = ZipReportClient('http://127.0.0.1:6543', "") # zipreport-server client result = ZipReportProcessor(client).process(job) # render if result.success: with open(output_file, 'wb') as rpt: rpt.write(result.report.read()) return True return False
def info(self, args) -> bool: if len(args) == 0: self.error("Error: Invalid command syntax") return False for fn in args: path = Path(fn) if not path.exists(): self.error("Error: Invalid path") return False if not path.is_file(): self.error("Error: Path {} is not a valid file".format(path)) return False try: zpt = ReportFileLoader.load_file(path) print(self.LIST_LINE % (path, zpt.get_param(const.MANIFEST_TITLE, ""))) except Exception: self.error("Error: {} is not a valid zipreport file".format(path)) return True
from zipreport import ZipReport from zipreport.report import ReportFileLoader if __name__ == "__main__": args = sys.argv[1:] if len(args) != 1: print("Usage: python3 zipreport-server.py <destination_file.pdf>") exit(1) output_file = args[0] # template variables report_data = { 'title': "Example report using Jinja templating", 'color_list': ['red', 'blue', 'green'], 'description': 'a long text field with some filler description so the page isn\'t that empty', } # load report from file zpt = ReportFileLoader.load("reports/simple.zpt") # render the report with default job options result = ZipReport('http://127.0.0.1:6543', "").render_defaults(zpt, report_data) if result.success: with open(output_file, 'wb') as rpt: rpt.write(result.report.read())
# Assemble report directly from the report folder, without using zpt file # ReportFileBuilder.build_zipfs() will create an in-memory zpt file suitable to be used by the library # Not: when using zpt files, this step, as well as the assembly of the ReportFile object is not necessary; # instead, it could be loaded the following way: # # report = ReportFileLoader.load(zpt_file_path) # report_path = Path("../../reports/newsletter").absolute() status, zfs = ReportFileBuilder.build_zipfs(report_path) if not status.success(): print("Error loading the report") exit(1) # assemble ReportFile object from the in-memory zfs report = ReportFileLoader.load_zipfs(zfs) # template variables report_data = { 'name': "John Connor", } # render using zipreport-cli processor result = MIMEReport().render_defaults(report, report_data) if not result.success: print("An error occured while generating the email:", result.error) exit(1) # save io.BytesIO buffer to file # result.report is of type EmailMessage() with open(email_name, 'wb') as f:
"Usage: python3 main.py <path_to_zptcli_binary> <destination_file.pdf>" ) exit(1) zipreport_cli = Path(args[0]) # zipreport-cli binary path pdf_name = Path(args[1]) # output file path if not zipreport_cli.exists() or zipreport_cli.is_dir(): print("zpt-cli not found") exit(1) if pdf_name.exists(): print("{} already exists".format(pdf_name)) exit(1) report_name = "simple_report.zpt" report = ReportFileLoader.load(report_name) # template variables report_data = { # our callback function to generate the image 'colored_rectangle_fn': render_image, # desired color to use 'rectangle_color': 'pink', } # render using zipreport-cli processor result = ZipReportCli(zipreport_cli).render_defaults(report, report_data) if not result.success: print("An error occured while generating the pdf:", result.error) exit(1)
if not zipreport_cli.exists() or zipreport_cli.is_dir(): print("zpt-cli not found") exit(1) if pdf_name.exists(): print("{} already exists".format(pdf_name)) exit(1) report_path = Path('simple.zpt') if not report_path.exists(): print("Missing report file. Did you run build.py first?") exit(1) # load report from file report = ReportFileLoader.load(report_path) # template variables report_data = { 'title': "Example report using Jinja templating", 'color_list': ['red', 'blue', 'green'], 'description': 'a long text field with some filler description so the page isn\'t that empty', } # render using zipreport-cli processor result = ZipReportCli(zipreport_cli).render_defaults(report, report_data) if not result.success: print("An error occured while generating the report:", result.error) exit(1) # save io.BytesIO buffer to file