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.

42 lines
1.5 KiB

  1. from odoo.addons.web.controllers import main as report
  2. from odoo.http import route
  3. from werkzeug.urls import url_decode
  4. import json
  5. import logging
  6. from io import BytesIO
  7. _logger = logging.getLogger(__name__)
  8. try:
  9. from PyPDF2 import PdfFileReader, PdfFileWriter
  10. except ImportError as err:
  11. _logger.debug(err)
  12. class ReportController(report.ReportController):
  13. @route()
  14. def report_download(self, data, token):
  15. result = super().report_download(data, token)
  16. requestcontent = json.loads(data)
  17. url, type = requestcontent[0], requestcontent[1]
  18. if (
  19. type in ['qweb-pdf'] and
  20. result.headers['Content-Type'] == "application/pdf" and
  21. '?' in url
  22. ):
  23. url_data = dict(url_decode(url.split('?')[1]).items())
  24. if 'context' in url_data:
  25. context_data = json.loads(url_data['context'])
  26. if 'encrypt_password' in context_data:
  27. # We need to encrypt here because this function is not
  28. # passing context, so we need to implement this again
  29. data = result.get_data()
  30. output_pdf = PdfFileWriter()
  31. in_buff = BytesIO(data)
  32. pdf = PdfFileReader(in_buff)
  33. output_pdf.appendPagesFromReader(pdf)
  34. output_pdf.encrypt(context_data['encrypt_password'])
  35. buff = BytesIO()
  36. output_pdf.write(buff)
  37. result.set_data(buff.getvalue())
  38. return result