optimize generation of pdf if its not stored to db

This commit is contained in:
Frederik Jaeckel 2024-12-12 12:37:56 +01:00
parent ba824215c1
commit 23bace99bb
2 changed files with 48 additions and 22 deletions

View file

@ -118,21 +118,10 @@ class RunXRechnungReport(Wizard):
'invoice_report_cache_id': None,
'invoice_report_format': None}])
to_generate = [x.id for x in invoices if not x.invoice_report_cache]
to_generate = [x for x in invoices if not x.invoice_report_cache]
if to_generate:
report_action = XReport.get_used_report()
# run selected report on invoices w/o stored report-data
data2 = {}
data2.update(data)
data2['action_id'] = report_action.id
data2['model'] = report_action.model
data2['id'] = to_generate[0]
data2['ids'] = to_generate
RepInvoice = pool.get(report_action.report_name, type='report')
with Transaction().set_context({'with_rec_name': False}):
RepInvoice.execute(to_generate, data2)
# the pdf should be stored in db...
XReport.generate_pdf_data(to_generate)
def do_export(self, action):
""" run export
@ -151,7 +140,6 @@ class RunXRechnungReport(Wizard):
if data['edocument'] == 'edocument.facturxext.invoice-ferd':
# pdf is stored to db
self.generate_invoice_reports(data)
return action, data
# end RunXRechnungReport

View file

@ -11,6 +11,7 @@ from trytond.report import Report
from trytond.pool import Pool
from trytond.exceptions import UserError
from trytond.i18n import gettext
from trytond.transaction import Transaction
from .wizard_runreport import edoc_versions
@ -112,6 +113,36 @@ class XReport(Report):
'account_invoice_xrechnung.msg_no_report_found'))
return act_report
@classmethod
def generate_pdf_data(cls, records):
""" generate pdf of invoice using defined report
Args:
records (list): records of account.invoice
Returns:
list: dicts - {'content': <pdf-data>, 'fname': 'file-name'}
"""
report_action = cls.get_used_report()
RepInvoice = Pool().get(report_action.report_name, type='report')
# run selected report on invoices w/o stored report-data
data2 = {}
data2['action_id'] = report_action.id
data2['model'] = report_action.model
result = []
for record in records:
data2['id'] = record.id
data2['ids'] = [record.id]
with Transaction().set_context({'with_rec_name': False}):
(ext, content, print1, fname) = RepInvoice.execute(
[record.id], data2)
result.append({
'content': content,
'fname': fname})
return result
@classmethod
def get_zugferd_pdf(cls, invoice, invoice_xml):
""" generate ZugFeRD-PDF
@ -120,15 +151,22 @@ class XReport(Report):
invoice (record): model account.invoice
invoice_xml (str): xml-data
"""
# pdf was already stored to db
if not (invoice.invoice_report_cache and (
invoice.invoice_report_format == 'pdf')):
# pdf was already stored to db, must be pdf
if invoice.invoice_report_cache:
if invoice.invoice_report_format != 'pdf':
raise UserError(gettext(
'account_invoice_xrechnung.msg_invalid_cachecontent',
invoice_name=invoice.rec_name))
pdf_data = invoice.invoice_report_cache
else:
# run report, could fail because we are in
# readonly-transaction
report_data = cls.generate_pdf_data([invoice])
if report_data:
pdf_data = report_data[0]['content']
zugferd_pdf = generate_from_binary(
pdf_file=invoice.invoice_report_cache,
pdf_file=pdf_data,
xml=invoice_xml,
check_xsd=True,
pdf_metadata={