ir_actions_report.py 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from markupsafe import Markup
  4. from odoo import api, fields, models, tools, SUPERUSER_ID, _
  5. from odoo.exceptions import UserError, AccessError
  6. from odoo.tools.safe_eval import safe_eval, time
  7. from odoo.tools.misc import find_in_path, ustr
  8. from odoo.tools import check_barcode_encoding, config, is_html_empty, parse_version
  9. from odoo.http import request
  10. from odoo.osv.expression import NEGATIVE_TERM_OPERATORS, FALSE_DOMAIN
  11. import io
  12. import logging
  13. import os
  14. import lxml.html
  15. import tempfile
  16. import subprocess
  17. import re
  18. import json
  19. from lxml import etree
  20. from contextlib import closing
  21. from reportlab.graphics.barcode import createBarcodeDrawing
  22. from PyPDF2 import PdfFileWriter, PdfFileReader
  23. from collections import OrderedDict
  24. from collections.abc import Iterable
  25. from PIL import Image, ImageFile
  26. # Allow truncated images
  27. ImageFile.LOAD_TRUNCATED_IMAGES = True
  28. try:
  29. from PyPDF2.errors import PdfReadError
  30. except ImportError:
  31. from PyPDF2.utils import PdfReadError
  32. _logger = logging.getLogger(__name__)
  33. # A lock occurs when the user wants to print a report having multiple barcode while the server is
  34. # started in threaded-mode. The reason is that reportlab has to build a cache of the T1 fonts
  35. # before rendering a barcode (done in a C extension) and this part is not thread safe. We attempt
  36. # here to init the T1 fonts cache at the start-up of Odoo so that rendering of barcode in multiple
  37. # thread does not lock the server.
  38. try:
  39. createBarcodeDrawing('Code128', value='foo', format='png', width=100, height=100, humanReadable=1).asString('png')
  40. except Exception:
  41. pass
  42. def _get_wkhtmltopdf_bin():
  43. return find_in_path('wkhtmltopdf')
  44. # Check the presence of Wkhtmltopdf and return its version at Odoo start-up
  45. wkhtmltopdf_state = 'install'
  46. wkhtmltopdf_dpi_zoom_ratio = False
  47. try:
  48. process = subprocess.Popen(
  49. [_get_wkhtmltopdf_bin(), '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE
  50. )
  51. except (OSError, IOError):
  52. _logger.info('You need Wkhtmltopdf to print a pdf version of the reports.')
  53. else:
  54. _logger.info('Will use the Wkhtmltopdf binary at %s' % _get_wkhtmltopdf_bin())
  55. out, err = process.communicate()
  56. match = re.search(b'([0-9.]+)', out)
  57. if match:
  58. version = match.group(0).decode('ascii')
  59. if parse_version(version) < parse_version('0.12.0'):
  60. _logger.info('Upgrade Wkhtmltopdf to (at least) 0.12.0')
  61. wkhtmltopdf_state = 'upgrade'
  62. else:
  63. wkhtmltopdf_state = 'ok'
  64. if parse_version(version) >= parse_version('0.12.2'):
  65. wkhtmltopdf_dpi_zoom_ratio = True
  66. if config['workers'] == 1:
  67. _logger.info('You need to start Odoo with at least two workers to print a pdf version of the reports.')
  68. wkhtmltopdf_state = 'workers'
  69. else:
  70. _logger.info('Wkhtmltopdf seems to be broken.')
  71. wkhtmltopdf_state = 'broken'
  72. class IrActionsReport(models.Model):
  73. _name = 'ir.actions.report'
  74. _description = 'Report Action'
  75. _inherit = 'ir.actions.actions'
  76. _table = 'ir_act_report_xml'
  77. _order = 'name'
  78. type = fields.Char(default='ir.actions.report')
  79. binding_type = fields.Selection(default='report')
  80. model = fields.Char(required=True, string='Model Name')
  81. model_id = fields.Many2one('ir.model', string='Model', compute='_compute_model_id', search='_search_model_id')
  82. report_type = fields.Selection([
  83. ('qweb-html', 'HTML'),
  84. ('qweb-pdf', 'PDF'),
  85. ('qweb-text', 'Text'),
  86. ], required=True, default='qweb-pdf',
  87. help='The type of the report that will be rendered, each one having its own'
  88. ' rendering method. HTML means the report will be opened directly in your'
  89. ' browser PDF means the report will be rendered using Wkhtmltopdf and'
  90. ' downloaded by the user.')
  91. report_name = fields.Char(string='Template Name', required=True)
  92. report_file = fields.Char(string='Report File', required=False, readonly=False, store=True,
  93. help="The path to the main report file (depending on Report Type) or empty if the content is in another field")
  94. groups_id = fields.Many2many('res.groups', 'res_groups_report_rel', 'uid', 'gid', string='Groups')
  95. multi = fields.Boolean(string='On Multiple Doc.', help="If set to true, the action will not be displayed on the right toolbar of a form view.")
  96. paperformat_id = fields.Many2one('report.paperformat', 'Paper Format')
  97. print_report_name = fields.Char('Printed Report Name', translate=True,
  98. help="This is the filename of the report going to download. Keep empty to not change the report filename. You can use a python expression with the 'object' and 'time' variables.")
  99. attachment_use = fields.Boolean(string='Reload from Attachment',
  100. help='If enabled, then the second time the user prints with same attachment name, it returns the previous report.')
  101. attachment = fields.Char(string='Save as Attachment Prefix',
  102. help='This is the filename of the attachment used to store the printing result. Keep empty to not save the printed reports. You can use a python expression with the object and time variables.')
  103. @api.depends('model')
  104. def _compute_model_id(self):
  105. for action in self:
  106. action.model_id = self.env['ir.model']._get(action.model).id
  107. def _search_model_id(self, operator, value):
  108. ir_model_ids = None
  109. if isinstance(value, str):
  110. names = self.env['ir.model'].name_search(value, operator=operator)
  111. ir_model_ids = [n[0] for n in names]
  112. elif isinstance(value, Iterable):
  113. ir_model_ids = value
  114. elif isinstance(value, int) and not isinstance(value, bool):
  115. ir_model_ids = [value]
  116. if ir_model_ids:
  117. operator = 'not in' if operator in NEGATIVE_TERM_OPERATORS else 'in'
  118. ir_model = self.env['ir.model'].browse(ir_model_ids)
  119. return [('model', operator, ir_model.mapped('model'))]
  120. elif isinstance(value, bool) or value is None:
  121. return [('model', operator, value)]
  122. else:
  123. return FALSE_DOMAIN
  124. def _get_readable_fields(self):
  125. return super()._get_readable_fields() | {
  126. "report_name", "report_type", "target",
  127. # these two are not real fields of ir.actions.report but are
  128. # expected in the route /report/<converter>/<reportname> and must
  129. # not be removed by clean_action
  130. "context", "data",
  131. # and this one is used by the frontend later on.
  132. "close_on_report_download",
  133. }
  134. def associated_view(self):
  135. """Used in the ir.actions.report form view in order to search naively after the view(s)
  136. used in the rendering.
  137. """
  138. self.ensure_one()
  139. action_ref = self.env.ref('base.action_ui_view')
  140. if not action_ref or len(self.report_name.split('.')) < 2:
  141. return False
  142. action_data = action_ref.read()[0]
  143. action_data['domain'] = [('name', 'ilike', self.report_name.split('.')[1]), ('type', '=', 'qweb')]
  144. return action_data
  145. def create_action(self):
  146. """ Create a contextual action for each report. """
  147. for report in self:
  148. model = self.env['ir.model']._get(report.model)
  149. report.write({'binding_model_id': model.id, 'binding_type': 'report'})
  150. return True
  151. def unlink_action(self):
  152. """ Remove the contextual actions created for the reports. """
  153. self.check_access_rights('write', raise_exception=True)
  154. self.filtered('binding_model_id').write({'binding_model_id': False})
  155. return True
  156. #--------------------------------------------------------------------------
  157. # Main report methods
  158. #--------------------------------------------------------------------------
  159. def retrieve_attachment(self, record):
  160. '''Retrieve an attachment for a specific record.
  161. :param record: The record owning of the attachment.
  162. :return: A recordset of length <=1 or None
  163. '''
  164. attachment_name = safe_eval(self.attachment, {'object': record, 'time': time}) if self.attachment else ''
  165. if not attachment_name:
  166. return None
  167. return self.env['ir.attachment'].search([
  168. ('name', '=', attachment_name),
  169. ('res_model', '=', self.model),
  170. ('res_id', '=', record.id)
  171. ], limit=1)
  172. @api.model
  173. def get_wkhtmltopdf_state(self):
  174. '''Get the current state of wkhtmltopdf: install, ok, upgrade, workers or broken.
  175. * install: Starting state.
  176. * upgrade: The binary is an older version (< 0.12.0).
  177. * ok: A binary was found with a recent version (>= 0.12.0).
  178. * workers: Not enough workers found to perform the pdf rendering process (< 2 workers).
  179. * broken: A binary was found but not responding.
  180. :return: wkhtmltopdf_state
  181. '''
  182. return wkhtmltopdf_state
  183. @api.model
  184. def datamatrix_available(self):
  185. '''Returns whether or not datamatrix creation is possible.
  186. * True: Reportlab seems to be able to create datamatrix without error.
  187. * False: Reportlab cannot seem to create datamatrix, most likely due to missing package dependency
  188. :return: Boolean
  189. '''
  190. return True
  191. def get_paperformat(self):
  192. return self.paperformat_id or self.env.company.paperformat_id
  193. @api.model
  194. def _build_wkhtmltopdf_args(
  195. self,
  196. paperformat_id,
  197. landscape,
  198. specific_paperformat_args=None,
  199. set_viewport_size=False):
  200. '''Build arguments understandable by wkhtmltopdf bin.
  201. :param paperformat_id: A report.paperformat record.
  202. :param landscape: Force the report orientation to be landscape.
  203. :param specific_paperformat_args: A dictionary containing prioritized wkhtmltopdf arguments.
  204. :param set_viewport_size: Enable a viewport sized '1024x1280' or '1280x1024' depending of landscape arg.
  205. :return: A list of string representing the wkhtmltopdf process command args.
  206. '''
  207. if landscape is None and specific_paperformat_args and specific_paperformat_args.get('data-report-landscape'):
  208. landscape = specific_paperformat_args.get('data-report-landscape')
  209. command_args = ['--disable-local-file-access']
  210. if set_viewport_size:
  211. command_args.extend(['--viewport-size', landscape and '1024x1280' or '1280x1024'])
  212. # Passing the cookie to wkhtmltopdf in order to resolve internal links.
  213. if request and request.db:
  214. command_args.extend(['--cookie', 'session_id', request.session.sid])
  215. # Less verbose error messages
  216. command_args.extend(['--quiet'])
  217. # Build paperformat args
  218. if paperformat_id:
  219. if paperformat_id.format and paperformat_id.format != 'custom':
  220. command_args.extend(['--page-size', paperformat_id.format])
  221. if paperformat_id.page_height and paperformat_id.page_width and paperformat_id.format == 'custom':
  222. command_args.extend(['--page-width', str(paperformat_id.page_width) + 'mm'])
  223. command_args.extend(['--page-height', str(paperformat_id.page_height) + 'mm'])
  224. if specific_paperformat_args and specific_paperformat_args.get('data-report-margin-top'):
  225. command_args.extend(['--margin-top', str(specific_paperformat_args['data-report-margin-top'])])
  226. else:
  227. command_args.extend(['--margin-top', str(paperformat_id.margin_top)])
  228. dpi = None
  229. if specific_paperformat_args and specific_paperformat_args.get('data-report-dpi'):
  230. dpi = int(specific_paperformat_args['data-report-dpi'])
  231. elif paperformat_id.dpi:
  232. if os.name == 'nt' and int(paperformat_id.dpi) <= 95:
  233. _logger.info("Generating PDF on Windows platform require DPI >= 96. Using 96 instead.")
  234. dpi = 96
  235. else:
  236. dpi = paperformat_id.dpi
  237. if dpi:
  238. command_args.extend(['--dpi', str(dpi)])
  239. if wkhtmltopdf_dpi_zoom_ratio:
  240. command_args.extend(['--zoom', str(96.0 / dpi)])
  241. if specific_paperformat_args and specific_paperformat_args.get('data-report-header-spacing'):
  242. command_args.extend(['--header-spacing', str(specific_paperformat_args['data-report-header-spacing'])])
  243. elif paperformat_id.header_spacing:
  244. command_args.extend(['--header-spacing', str(paperformat_id.header_spacing)])
  245. command_args.extend(['--margin-left', str(paperformat_id.margin_left)])
  246. if specific_paperformat_args and specific_paperformat_args.get('data-report-margin-bottom'):
  247. command_args.extend(['--margin-bottom', str(specific_paperformat_args['data-report-margin-bottom'])])
  248. else:
  249. command_args.extend(['--margin-bottom', str(paperformat_id.margin_bottom)])
  250. command_args.extend(['--margin-right', str(paperformat_id.margin_right)])
  251. if not landscape and paperformat_id.orientation:
  252. command_args.extend(['--orientation', str(paperformat_id.orientation)])
  253. if paperformat_id.header_line:
  254. command_args.extend(['--header-line'])
  255. if paperformat_id.disable_shrinking:
  256. command_args.extend(['--disable-smart-shrinking'])
  257. # Add extra time to allow the page to render
  258. delay = self.env['ir.config_parameter'].sudo().get_param('report.print_delay', '1000')
  259. command_args.extend(['--javascript-delay', delay])
  260. if landscape:
  261. command_args.extend(['--orientation', 'landscape'])
  262. return command_args
  263. def _prepare_html(self, html, report_model=False):
  264. '''Divide and recreate the header/footer html by merging all found in html.
  265. The bodies are extracted and added to a list. Then, extract the specific_paperformat_args.
  266. The idea is to put all headers/footers together. Then, we will use a javascript trick
  267. (see minimal_layout template) to set the right header/footer during the processing of wkhtmltopdf.
  268. This allows the computation of multiple reports in a single call to wkhtmltopdf.
  269. :param html: The html rendered by render_qweb_html.
  270. :type: bodies: list of string representing each one a html body.
  271. :type header: string representing the html header.
  272. :type footer: string representing the html footer.
  273. :type specific_paperformat_args: dictionary of prioritized paperformat values.
  274. :return: bodies, header, footer, specific_paperformat_args
  275. '''
  276. IrConfig = self.env['ir.config_parameter'].sudo()
  277. # Return empty dictionary if 'web.minimal_layout' not found.
  278. layout = self.env.ref('web.minimal_layout', raise_if_not_found=False)
  279. if not layout:
  280. return {}
  281. base_url = IrConfig.get_param('report.url') or layout.get_base_url()
  282. root = lxml.html.fromstring(html)
  283. match_klass = "//div[contains(concat(' ', normalize-space(@class), ' '), ' {} ')]"
  284. header_node = etree.Element('div', id='minimal_layout_report_headers')
  285. footer_node = etree.Element('div', id='minimal_layout_report_footers')
  286. bodies = []
  287. res_ids = []
  288. body_parent = root.xpath('//main')[0]
  289. # Retrieve headers
  290. for node in root.xpath(match_klass.format('header')):
  291. body_parent = node.getparent()
  292. node.getparent().remove(node)
  293. header_node.append(node)
  294. # Retrieve footers
  295. for node in root.xpath(match_klass.format('footer')):
  296. body_parent = node.getparent()
  297. node.getparent().remove(node)
  298. footer_node.append(node)
  299. # Retrieve bodies
  300. for node in root.xpath(match_klass.format('article')):
  301. # set context language to body language
  302. IrQweb = self.env['ir.qweb']
  303. if node.get('data-oe-lang'):
  304. IrQweb = IrQweb.with_context(lang=node.get('data-oe-lang'))
  305. body = IrQweb._render(layout.id, {
  306. 'subst': False,
  307. 'body': Markup(lxml.html.tostring(node, encoding='unicode')),
  308. 'base_url': base_url,
  309. 'report_xml_id' : self.xml_id
  310. }, raise_if_not_found=False)
  311. bodies.append(body)
  312. if node.get('data-oe-model') == report_model:
  313. res_ids.append(int(node.get('data-oe-id', 0)))
  314. else:
  315. res_ids.append(None)
  316. if not bodies:
  317. body = ''.join(lxml.html.tostring(c, encoding='unicode') for c in body_parent.getchildren())
  318. bodies.append(body)
  319. # Get paperformat arguments set in the root html tag. They are prioritized over
  320. # paperformat-record arguments.
  321. specific_paperformat_args = {}
  322. for attribute in root.items():
  323. if attribute[0].startswith('data-report-'):
  324. specific_paperformat_args[attribute[0]] = attribute[1]
  325. header = self.env['ir.qweb']._render(layout.id, {
  326. 'subst': True,
  327. 'body': Markup(lxml.html.tostring(header_node, encoding='unicode')),
  328. 'base_url': base_url
  329. })
  330. footer = self.env['ir.qweb']._render(layout.id, {
  331. 'subst': True,
  332. 'body': Markup(lxml.html.tostring(footer_node, encoding='unicode')),
  333. 'base_url': base_url
  334. })
  335. return bodies, res_ids, header, footer, specific_paperformat_args
  336. @api.model
  337. def _run_wkhtmltopdf(
  338. self,
  339. bodies,
  340. report_ref=False,
  341. header=None,
  342. footer=None,
  343. landscape=False,
  344. specific_paperformat_args=None,
  345. set_viewport_size=False):
  346. '''Execute wkhtmltopdf as a subprocess in order to convert html given in input into a pdf
  347. document.
  348. :param list[str] bodies: The html bodies of the report, one per page.
  349. :param report_ref: report reference that is needed to get report paperformat.
  350. :param str header: The html header of the report containing all headers.
  351. :param str footer: The html footer of the report containing all footers.
  352. :param landscape: Force the pdf to be rendered under a landscape format.
  353. :param specific_paperformat_args: dict of prioritized paperformat arguments.
  354. :param set_viewport_size: Enable a viewport sized '1024x1280' or '1280x1024' depending of landscape arg.
  355. :return: Content of the pdf as bytes
  356. :rtype: bytes
  357. '''
  358. paperformat_id = self._get_report(report_ref).get_paperformat() if report_ref else self.get_paperformat()
  359. # Build the base command args for wkhtmltopdf bin
  360. command_args = self._build_wkhtmltopdf_args(
  361. paperformat_id,
  362. landscape,
  363. specific_paperformat_args=specific_paperformat_args,
  364. set_viewport_size=set_viewport_size)
  365. files_command_args = []
  366. temporary_files = []
  367. if header:
  368. head_file_fd, head_file_path = tempfile.mkstemp(suffix='.html', prefix='report.header.tmp.')
  369. with closing(os.fdopen(head_file_fd, 'wb')) as head_file:
  370. head_file.write(header.encode())
  371. temporary_files.append(head_file_path)
  372. files_command_args.extend(['--header-html', head_file_path])
  373. if footer:
  374. foot_file_fd, foot_file_path = tempfile.mkstemp(suffix='.html', prefix='report.footer.tmp.')
  375. with closing(os.fdopen(foot_file_fd, 'wb')) as foot_file:
  376. foot_file.write(footer.encode())
  377. temporary_files.append(foot_file_path)
  378. files_command_args.extend(['--footer-html', foot_file_path])
  379. paths = []
  380. for i, body in enumerate(bodies):
  381. prefix = '%s%d.' % ('report.body.tmp.', i)
  382. body_file_fd, body_file_path = tempfile.mkstemp(suffix='.html', prefix=prefix)
  383. with closing(os.fdopen(body_file_fd, 'wb')) as body_file:
  384. body_file.write(body.encode())
  385. paths.append(body_file_path)
  386. temporary_files.append(body_file_path)
  387. pdf_report_fd, pdf_report_path = tempfile.mkstemp(suffix='.pdf', prefix='report.tmp.')
  388. os.close(pdf_report_fd)
  389. temporary_files.append(pdf_report_path)
  390. try:
  391. wkhtmltopdf = [_get_wkhtmltopdf_bin()] + command_args + files_command_args + paths + [pdf_report_path]
  392. process = subprocess.Popen(wkhtmltopdf, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  393. out, err = process.communicate()
  394. err = ustr(err)
  395. if process.returncode not in [0, 1]:
  396. if process.returncode == -11:
  397. message = _(
  398. 'Wkhtmltopdf failed (error code: %s). Memory limit too low or maximum file number of subprocess reached. Message : %s')
  399. else:
  400. message = _('Wkhtmltopdf failed (error code: %s). Message: %s')
  401. _logger.warning(message, process.returncode, err[-1000:])
  402. raise UserError(message % (str(process.returncode), err[-1000:]))
  403. else:
  404. if err:
  405. _logger.warning('wkhtmltopdf: %s' % err)
  406. except:
  407. raise
  408. with open(pdf_report_path, 'rb') as pdf_document:
  409. pdf_content = pdf_document.read()
  410. # Manual cleanup of the temporary files
  411. for temporary_file in temporary_files:
  412. try:
  413. os.unlink(temporary_file)
  414. except (OSError, IOError):
  415. _logger.error('Error when trying to remove file %s' % temporary_file)
  416. return pdf_content
  417. @api.model
  418. def _get_report_from_name(self, report_name):
  419. """Get the first record of ir.actions.report having the ``report_name`` as value for
  420. the field report_name.
  421. """
  422. report_obj = self.env['ir.actions.report']
  423. conditions = [('report_name', '=', report_name)]
  424. context = self.env['res.users'].context_get()
  425. return report_obj.with_context(context).sudo().search(conditions, limit=1)
  426. @api.model
  427. def _get_report(self, report_ref):
  428. """Get the report (with sudo) from a reference
  429. report_ref: can be one of
  430. - ir.actions.report id
  431. - ir.actions.report record
  432. - ir.model.data reference to ir.actions.report
  433. - ir.actions.report report_name
  434. """
  435. ReportSudo = self.env['ir.actions.report'].sudo()
  436. if isinstance(report_ref, int):
  437. return ReportSudo.browse(report_ref)
  438. if isinstance(report_ref, models.Model):
  439. if report_ref._name != self._name:
  440. raise ValueError("Expected report of type %s, got %s" % (self._name, report_ref._name))
  441. return report_ref.sudo()
  442. report = ReportSudo.search([('report_name', '=', report_ref)], limit=1)
  443. if report:
  444. return report
  445. report = self.env.ref(report_ref)
  446. if report:
  447. if report._name != "ir.actions.report":
  448. raise ValueError("Fetching report %r: type %s, expected ir.actions.report" % (report_ref, report._name))
  449. return report.sudo()
  450. raise ValueError("Fetching report %r: report not found" % report_ref)
  451. @api.model
  452. def barcode(self, barcode_type, value, **kwargs):
  453. defaults = {
  454. 'width': (600, int),
  455. 'height': (100, int),
  456. 'humanreadable': (False, lambda x: bool(int(x))),
  457. 'quiet': (True, lambda x: bool(int(x))),
  458. 'mask': (None, lambda x: x),
  459. 'barBorder': (4, int),
  460. # The QR code can have different layouts depending on the Error Correction Level
  461. # See: https://en.wikipedia.org/wiki/QR_code#Error_correction
  462. # Level 'L' – up to 7% damage (default)
  463. # Level 'M' – up to 15% damage (i.e. required by l10n_ch QR bill)
  464. # Level 'Q' – up to 25% damage
  465. # Level 'H' – up to 30% damage
  466. 'barLevel': ('L', lambda x: x in ('L', 'M', 'Q', 'H') and x or 'L'),
  467. }
  468. kwargs = {k: validator(kwargs.get(k, v)) for k, (v, validator) in defaults.items()}
  469. kwargs['humanReadable'] = kwargs.pop('humanreadable')
  470. if barcode_type == 'UPCA' and len(value) in (11, 12, 13):
  471. barcode_type = 'EAN13'
  472. if len(value) in (11, 12):
  473. value = '0%s' % value
  474. elif barcode_type == 'auto':
  475. symbology_guess = {8: 'EAN8', 13: 'EAN13'}
  476. barcode_type = symbology_guess.get(len(value), 'Code128')
  477. elif barcode_type == 'DataMatrix':
  478. # Prevent a crash due to a lib change from pylibdmtx to reportlab
  479. barcode_type = 'ECC200DataMatrix'
  480. elif barcode_type == 'QR':
  481. # for `QR` type, `quiet` is not supported. And is simply ignored.
  482. # But we can use `barBorder` to get a similar behaviour.
  483. if kwargs['quiet']:
  484. kwargs['barBorder'] = 0
  485. if barcode_type in ('EAN8', 'EAN13') and not check_barcode_encoding(value, barcode_type):
  486. # If the barcode does not respect the encoding specifications, convert its type into Code128.
  487. # Otherwise, the report-lab method may return a barcode different from its value. For instance,
  488. # if the barcode type is EAN-8 and the value 11111111, the report-lab method will take the first
  489. # seven digits and will compute the check digit, which gives: 11111115 -> the barcode does not
  490. # match the expected value.
  491. barcode_type = 'Code128'
  492. try:
  493. barcode = createBarcodeDrawing(barcode_type, value=value, format='png', **kwargs)
  494. # If a mask is asked and it is available, call its function to
  495. # post-process the generated QR-code image
  496. if kwargs['mask']:
  497. available_masks = self.get_available_barcode_masks()
  498. mask_to_apply = available_masks.get(kwargs['mask'])
  499. if mask_to_apply:
  500. mask_to_apply(kwargs['width'], kwargs['height'], barcode)
  501. return barcode.asString('png')
  502. except (ValueError, AttributeError):
  503. if barcode_type == 'Code128':
  504. raise ValueError("Cannot convert into barcode.")
  505. elif barcode_type == 'QR':
  506. raise ValueError("Cannot convert into QR code.")
  507. else:
  508. return self.barcode('Code128', value, **kwargs)
  509. @api.model
  510. def get_available_barcode_masks(self):
  511. """ Hook for extension.
  512. This function returns the available QR-code masks, in the form of a
  513. list of (code, mask_function) elements, where code is a string identifying
  514. the mask uniquely, and mask_function is a function returning a reportlab
  515. Drawing object with the result of the mask, and taking as parameters:
  516. - width of the QR-code, in pixels
  517. - height of the QR-code, in pixels
  518. - reportlab Drawing object containing the barcode to apply the mask on
  519. """
  520. return {}
  521. def _render_template(self, template, values=None):
  522. """Allow to render a QWeb template python-side. This function returns the 'ir.ui.view'
  523. render but embellish it with some variables/methods used in reports.
  524. :param values: additional methods/variables used in the rendering
  525. :returns: html representation of the template
  526. :rtype: bytes
  527. """
  528. if values is None:
  529. values = {}
  530. # Browse the user instead of using the sudo self.env.user
  531. user = self.env['res.users'].browse(self.env.uid)
  532. view_obj = self.env['ir.ui.view'].with_context(inherit_branding=False)
  533. values.update(
  534. time=time,
  535. context_timestamp=lambda t: fields.Datetime.context_timestamp(self.with_context(tz=user.tz), t),
  536. user=user,
  537. res_company=self.env.company,
  538. web_base_url=self.env['ir.config_parameter'].sudo().get_param('web.base.url', default=''),
  539. )
  540. return view_obj._render_template(template, values).encode()
  541. @api.model
  542. def _merge_pdfs(self, streams):
  543. writer = PdfFileWriter()
  544. for stream in streams:
  545. try:
  546. reader = PdfFileReader(stream)
  547. writer.appendPagesFromReader(reader)
  548. except (PdfReadError, TypeError, NotImplementedError, ValueError):
  549. raise UserError(_("Odoo is unable to merge the generated PDFs."))
  550. result_stream = io.BytesIO()
  551. streams.append(result_stream)
  552. writer.write(result_stream)
  553. return result_stream
  554. def _render_qweb_pdf_prepare_streams(self, report_ref, data, res_ids=None):
  555. if not data:
  556. data = {}
  557. data.setdefault('report_type', 'pdf')
  558. # access the report details with sudo() but evaluation context as current user
  559. report_sudo = self._get_report(report_ref)
  560. collected_streams = OrderedDict()
  561. # Fetch the existing attachments from the database for later use.
  562. # Reload the stream from the attachment in case of 'attachment_use'.
  563. if res_ids:
  564. records = self.env[report_sudo.model].browse(res_ids)
  565. for record in records:
  566. stream = None
  567. attachment = None
  568. if report_sudo.attachment:
  569. attachment = report_sudo.retrieve_attachment(record)
  570. # Extract the stream from the attachment.
  571. if attachment and report_sudo.attachment_use:
  572. stream = io.BytesIO(attachment.raw)
  573. # Ensure the stream can be saved in Image.
  574. if attachment.mimetype.startswith('image'):
  575. img = Image.open(stream)
  576. new_stream = io.BytesIO()
  577. img.convert("RGB").save(new_stream, format="pdf")
  578. stream.close()
  579. stream = new_stream
  580. collected_streams[record.id] = {
  581. 'stream': stream,
  582. 'attachment': attachment,
  583. }
  584. # Call 'wkhtmltopdf' to generate the missing streams.
  585. res_ids_wo_stream = [res_id for res_id, stream_data in collected_streams.items() if not stream_data['stream']]
  586. is_whtmltopdf_needed = not res_ids or res_ids_wo_stream
  587. if is_whtmltopdf_needed:
  588. if self.get_wkhtmltopdf_state() == 'install':
  589. # wkhtmltopdf is not installed
  590. # the call should be catched before (cf /report/check_wkhtmltopdf) but
  591. # if get_pdf is called manually (email template), the check could be
  592. # bypassed
  593. raise UserError(_("Unable to find Wkhtmltopdf on this system. The PDF can not be created."))
  594. # Disable the debug mode in the PDF rendering in order to not split the assets bundle
  595. # into separated files to load. This is done because of an issue in wkhtmltopdf
  596. # failing to load the CSS/Javascript resources in time.
  597. # Without this, the header/footer of the reports randomly disappear
  598. # because the resources files are not loaded in time.
  599. # https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2083
  600. additional_context = {'debug': False}
  601. # As the assets are generated during the same transaction as the rendering of the
  602. # templates calling them, there is a scenario where the assets are unreachable: when
  603. # you make a request to read the assets while the transaction creating them is not done.
  604. # Indeed, when you make an asset request, the controller has to read the `ir.attachment`
  605. # table.
  606. # This scenario happens when you want to print a PDF report for the first time, as the
  607. # assets are not in cache and must be generated. To workaround this issue, we manually
  608. # commit the writes in the `ir.attachment` table. It is done thanks to a key in the context.
  609. if not config['test_enable'] and 'commit_assetsbundle' not in self.env.context:
  610. additional_context['commit_assetsbundle'] = True
  611. html = self.with_context(**additional_context)._render_qweb_html(report_ref, res_ids_wo_stream, data=data)[0]
  612. bodies, html_ids, header, footer, specific_paperformat_args = self.with_context(**additional_context)._prepare_html(html, report_model=report_sudo.model)
  613. if report_sudo.attachment and set(res_ids_wo_stream) != set(html_ids):
  614. raise UserError(_(
  615. "The report's template %r is wrong, please contact your administrator. \n\n"
  616. "Can not separate file to save as attachment because the report's template does not contains the"
  617. " attributes 'data-oe-model' and 'data-oe-id' on the div with 'article' classname.",
  618. self.name,
  619. ))
  620. pdf_content = self._run_wkhtmltopdf(
  621. bodies,
  622. report_ref=report_ref,
  623. header=header,
  624. footer=footer,
  625. landscape=self._context.get('landscape'),
  626. specific_paperformat_args=specific_paperformat_args,
  627. set_viewport_size=self._context.get('set_viewport_size'),
  628. )
  629. pdf_content_stream = io.BytesIO(pdf_content)
  630. # Printing a PDF report without any records. The content could be returned directly.
  631. if not res_ids:
  632. return {
  633. False: {
  634. 'stream': pdf_content_stream,
  635. 'attachment': None,
  636. }
  637. }
  638. # Split the pdf for each record using the PDF outlines.
  639. # Only one record: append the whole PDF.
  640. if len(res_ids_wo_stream) == 1:
  641. collected_streams[res_ids_wo_stream[0]]['stream'] = pdf_content_stream
  642. return collected_streams
  643. # In case of multiple docs, we need to split the pdf according the records.
  644. # In the simplest case of 1 res_id == 1 page, we use the PDFReader to print the
  645. # pages one by one.
  646. html_ids_wo_none = [x for x in html_ids if x]
  647. reader = PdfFileReader(pdf_content_stream)
  648. if reader.numPages == len(res_ids_wo_stream):
  649. for i in range(reader.numPages):
  650. attachment_writer = PdfFileWriter()
  651. attachment_writer.addPage(reader.getPage(i))
  652. stream = io.BytesIO()
  653. attachment_writer.write(stream)
  654. collected_streams[res_ids[i]]['stream'] = stream
  655. return collected_streams
  656. # In cases where the number of res_ids != the number of pages,
  657. # we split the pdf based on top outlines computed by wkhtmltopdf.
  658. # An outline is a <h?> html tag found on the document. To retrieve this table,
  659. # we look on the pdf structure using pypdf to compute the outlines_pages from
  660. # the top level heading in /Outlines.
  661. if len(res_ids_wo_stream) > 1 and set(res_ids_wo_stream) == set(html_ids_wo_none):
  662. root = reader.trailer['/Root']
  663. has_valid_outlines = '/Outlines' in root and '/First' in root['/Outlines']
  664. if not has_valid_outlines:
  665. return {False: {
  666. 'report_action': self,
  667. 'stream': pdf_content_stream,
  668. 'attachment': None,
  669. }}
  670. outlines_pages = []
  671. node = root['/Outlines']['/First']
  672. while True:
  673. outlines_pages.append(root['/Dests'][node['/Dest']][0])
  674. if '/Next' not in node:
  675. break
  676. node = node['/Next']
  677. outlines_pages = sorted(set(outlines_pages))
  678. # The number of outlines must be equal to the number of records to be able to split the document.
  679. has_same_number_of_outlines = len(outlines_pages) == len(res_ids)
  680. # There should be a top-level heading on first page
  681. has_top_level_heading = outlines_pages[0] == 0
  682. if has_same_number_of_outlines and has_top_level_heading:
  683. # Split the PDF according to outlines.
  684. for i, num in enumerate(outlines_pages):
  685. to = outlines_pages[i + 1] if i + 1 < len(outlines_pages) else reader.numPages
  686. attachment_writer = PdfFileWriter()
  687. for j in range(num, to):
  688. attachment_writer.addPage(reader.getPage(j))
  689. stream = io.BytesIO()
  690. attachment_writer.write(stream)
  691. collected_streams[res_ids[i]]['stream'] = stream
  692. return collected_streams
  693. collected_streams[False] = {'stream': pdf_content_stream, 'attachment': None}
  694. return collected_streams
  695. def _render_qweb_pdf(self, report_ref, res_ids=None, data=None):
  696. if not data:
  697. data = {}
  698. if isinstance(res_ids, int):
  699. res_ids = [res_ids]
  700. data.setdefault('report_type', 'pdf')
  701. # In case of test environment without enough workers to perform calls to wkhtmltopdf,
  702. # fallback to render_html.
  703. if (tools.config['test_enable'] or tools.config['test_file']) and not self.env.context.get('force_report_rendering'):
  704. return self._render_qweb_html(report_ref, res_ids, data=data)
  705. collected_streams = self._render_qweb_pdf_prepare_streams(report_ref, data, res_ids=res_ids)
  706. # access the report details with sudo() but keep evaluation context as current user
  707. report_sudo = self._get_report(report_ref)
  708. # Generate the ir.attachment if needed.
  709. if report_sudo.attachment:
  710. attachment_vals_list = []
  711. for res_id, stream_data in collected_streams.items():
  712. # An attachment already exists.
  713. if stream_data['attachment']:
  714. continue
  715. # if res_id is false
  716. # we are unable to fetch the record, it won't be saved as we can't split the documents unambiguously
  717. if not res_id:
  718. _logger.warning(
  719. "These documents were not saved as an attachment because the template of %s doesn't "
  720. "have any headers seperating different instances of it. If you want it saved,"
  721. "please print the documents separately", report_sudo.report_name)
  722. continue
  723. record = self.env[report_sudo.model].browse(res_id)
  724. attachment_name = safe_eval(report_sudo.attachment, {'object': record, 'time': time})
  725. # Unable to compute a name for the attachment.
  726. if not attachment_name:
  727. continue
  728. attachment_vals_list.append({
  729. 'name': attachment_name,
  730. 'raw': stream_data['stream'].getvalue(),
  731. 'res_model': report_sudo.model,
  732. 'res_id': record.id,
  733. 'type': 'binary',
  734. })
  735. if attachment_vals_list:
  736. attachment_names = ', '.join(x['name'] for x in attachment_vals_list)
  737. try:
  738. self.env['ir.attachment'].create(attachment_vals_list)
  739. except AccessError:
  740. _logger.info("Cannot save PDF report %r attachments for user %r", attachment_names, self.env.user.display_name)
  741. else:
  742. _logger.info("The PDF documents %r are now saved in the database", attachment_names)
  743. # Merge all streams together for a single record.
  744. streams_to_merge = [x['stream'] for x in collected_streams.values() if x['stream']]
  745. if len(streams_to_merge) == 1:
  746. pdf_content = streams_to_merge[0].getvalue()
  747. else:
  748. with self._merge_pdfs(streams_to_merge) as pdf_merged_stream:
  749. pdf_content = pdf_merged_stream.getvalue()
  750. for stream in streams_to_merge:
  751. stream.close()
  752. if res_ids:
  753. _logger.info("The PDF report has been generated for model: %s, records %s.", report_sudo.model, str(res_ids))
  754. return pdf_content, 'pdf'
  755. @api.model
  756. def _render_qweb_text(self, report_ref, docids, data=None):
  757. if not data:
  758. data = {}
  759. data.setdefault('report_type', 'text')
  760. report = self._get_report(report_ref)
  761. data = self._get_rendering_context(report, docids, data)
  762. return self._render_template(report.report_name, data), 'text'
  763. @api.model
  764. def _render_qweb_html(self, report_ref, docids, data=None):
  765. if not data:
  766. data = {}
  767. data.setdefault('report_type', 'html')
  768. report = self._get_report(report_ref)
  769. data = self._get_rendering_context(report, docids, data)
  770. return self._render_template(report.report_name, data), 'html'
  771. def _get_rendering_context_model(self, report):
  772. report_model_name = 'report.%s' % report.report_name
  773. return self.env.get(report_model_name)
  774. def _get_rendering_context(self, report, docids, data):
  775. # If the report is using a custom model to render its html, we must use it.
  776. # Otherwise, fallback on the generic html rendering.
  777. report_model = self._get_rendering_context_model(report)
  778. data = data and dict(data) or {}
  779. if report_model is not None:
  780. data.update(report_model._get_report_values(docids, data=data))
  781. else:
  782. docs = self.env[report.model].browse(docids)
  783. data.update({
  784. 'doc_ids': docids,
  785. 'doc_model': report.model,
  786. 'docs': docs,
  787. })
  788. data['is_html_empty'] = is_html_empty
  789. return data
  790. @api.model
  791. def _render(self, report_ref, res_ids, data=None):
  792. report = self._get_report(report_ref)
  793. report_type = report.report_type.lower().replace('-', '_')
  794. render_func = getattr(self, '_render_' + report_type, None)
  795. if not render_func:
  796. return None
  797. return render_func(report_ref, res_ids, data=data)
  798. def report_action(self, docids, data=None, config=True):
  799. """Return an action of type ir.actions.report.
  800. :param docids: id/ids/browse record of the records to print (if not used, pass an empty list)
  801. :param data:
  802. :param bool config:
  803. :rtype: bytes
  804. """
  805. context = self.env.context
  806. if docids:
  807. if isinstance(docids, models.Model):
  808. active_ids = docids.ids
  809. elif isinstance(docids, int):
  810. active_ids = [docids]
  811. elif isinstance(docids, list):
  812. active_ids = docids
  813. context = dict(self.env.context, active_ids=active_ids)
  814. report_action = {
  815. 'context': context,
  816. 'data': data,
  817. 'type': 'ir.actions.report',
  818. 'report_name': self.report_name,
  819. 'report_type': self.report_type,
  820. 'report_file': self.report_file,
  821. 'name': self.name,
  822. }
  823. discard_logo_check = self.env.context.get('discard_logo_check')
  824. if self.env.is_admin() and not self.env.company.external_report_layout_id and config and not discard_logo_check:
  825. return self._action_configure_external_report_layout(report_action)
  826. return report_action
  827. def _action_configure_external_report_layout(self, report_action):
  828. action = self.env["ir.actions.actions"]._for_xml_id("web.action_base_document_layout_configurator")
  829. py_ctx = json.loads(action.get('context', {}))
  830. report_action['close_on_report_download'] = True
  831. py_ctx['report_action'] = report_action
  832. action['context'] = py_ctx
  833. return action