OCA reporting engine fork for dev and update.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.9 KiB

  1. # Copyright 2017 Creu Blanca
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from io import BytesIO
  4. import os
  5. from contextlib import closing
  6. import logging
  7. import tempfile
  8. from subprocess import Popen, PIPE
  9. from odoo import api, models, tools
  10. _logger = logging.getLogger(__name__)
  11. try:
  12. from fdfgen import forge_fdf
  13. EXTERNAL_DEPENDENCY_BINARY_PDFTK = tools.find_in_path('pdftk')
  14. except (ImportError, IOError) as err:
  15. _logger.debug(err)
  16. raise err
  17. class ReportFillPDFAbstract(models.AbstractModel):
  18. _name = 'report.report_fillpdf.abstract'
  19. def fill_report(self, docids, data):
  20. objs = self.env[self.env.context.get('active_model')].browse(docids)
  21. return self.fill_pdf_form(
  22. self.get_form(data, objs),
  23. self.get_document_values(data, objs)), 'pdf'
  24. @api.model
  25. def get_original_document_path(self, data, objs):
  26. raise NotImplementedError()
  27. @api.model
  28. def get_form(self, data, objs):
  29. with open(self.get_original_document_path(data, objs), 'rb') as file:
  30. result = file.read()
  31. return result
  32. @api.model
  33. def get_document_values(self, data, objs):
  34. return {}
  35. @api.model
  36. def fill_pdf_form(self, form, vals):
  37. fdf = forge_fdf("", vals.items(), [], [], [])
  38. document_fd, document_path = tempfile.mkstemp(
  39. suffix='.pdf', prefix='')
  40. with closing(os.fdopen(document_fd, 'wb')) as body_file:
  41. body_file.write(form)
  42. args = [
  43. EXTERNAL_DEPENDENCY_BINARY_PDFTK,
  44. document_path,
  45. "fill_form", "-",
  46. "output", "-",
  47. "dont_ask",
  48. "flatten"
  49. ]
  50. p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
  51. stdout, stderr = p.communicate(fdf)
  52. os.unlink(document_path)
  53. if stderr.strip():
  54. raise IOError(stderr)
  55. return BytesIO(stdout).getvalue()