Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to generates and returns pdf version of a report in openerp?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 370
    Comment on it

    In openerp you make a request to read the assets while the transaction creating is not done and As the assets are generated during the same transaction and the rendering of the templates will call them. There is a scenario where the assets are unreachable: when you make a request to read the assets while the transaction creating them is not done. Indeed, when you make an asset request, the controller has to read the 'ir.attachment' table.
    For this use below code in .py file,

    def get_pdf(self, cr, uid, ids, report_name, html=None, data=None, context=None):

    1. if not config['test_enable']:
    2. context = dict(context, commit_assetsbundle=True)
    3.  
    4. if html is None:
    5. html = self.get_html(cr, uid, ids, report_name, data=data, context=context)
    6.  
    7. # The test cursor prevents the use of another environnment while the current
    8. # transaction is not finished, leading to a deadlock when the report requests
    9. # an asset bundle during the execution of test scenarios. In this case, return
    10. # the html version.
    11. if isinstance(cr, TestCursor):
    12. return html
    13.  
    14. html = html.decode('utf-8') # Ensure the current document is utf-8 encoded.
    15.  
    16. # Get the ir.actions.report.xml record we are working on.
    17. report = self._get_report_from_name(cr, uid, report_name)
    18. # Check if we have to save the report or if we have to get one from the db.
    19. save_in_attachment = self._check_attachment_use(cr, uid, ids, report)
    20. # Get the paperformat associated to the report, otherwise fallback on the company one.
    21. if not report.paperformat_id:
    22. user = self.pool['res.users'].browse(cr, uid, uid)
    23. paperformat = user.company_id.paperformat_id
    24. else:
    25. paperformat = report.paperformat_id
    26.  
    27. # Preparing the minimal html pages
    28. headerhtml = []
    29. contenthtml = []
    30. footerhtml = []
    31. irconfig_obj = self.pool['ir.config_parameter']
    32. base_url = irconfig_obj.get_param(cr, SUPERUSER_ID, 'report.url') or irconfig_obj.get_param(cr, SUPERUSER_ID, 'web.base.url')
    33.  
    34. # Minimal page renderer
    35. view_obj = self.pool['ir.ui.view']
    36. render_minimal = partial(view_obj.render, cr, uid, 'report.minimal_layout', context=context)
    37.  
    38. # The received html report must be simplified. We convert it in a xml tree
    39. # in order to extract headers, bodies and footers.
    40. try:
    41. root = lxml.html.fromstring(html)
    42. match_klass = "//div[contains(concat(' ', normalize-space(@class), ' '), ' {} ')]"
    43.  
    44. for node in root.xpath(match_klass.format('header')):
    45. body = lxml.html.tostring(node)
    46. header = render_minimal(dict(subst=True, body=body, base_url=base_url))
    47. headerhtml.append(header)
    48.  
    49. for node in root.xpath(match_klass.format('footer')):
    50. body = lxml.html.tostring(node)
    51. footer = render_minimal(dict(subst=True, body=body, base_url=base_url))
    52. footerhtml.append(footer)
    53.  
    54. for node in root.xpath(match_klass.format('page')):
    55. # Previously, we marked some reports to be saved in attachment via their ids, so we
    56. # must set a relation between report ids and report's content. We use the QWeb
    57. # branding in order to do so: searching after a node having a data-oe-model
    58. # attribute with the value of the current report model and read its oe-id attribute
    59. if ids and len(ids) == 1:
    60. reportid = ids[0]
    61. else:
    62. oemodelnode = node.find(".//*[@data-oe-model='%s']" % report.model)
    63. if oemodelnode is not None:
    64. reportid = oemodelnode.get('data-oe-id')
    65. if reportid:
    66. reportid = int(reportid)
    67. else:
    68. reportid = False
    69.  
    70. # Extract the body
    71. body = lxml.html.tostring(node)
    72. reportcontent = render_minimal(dict(subst=False, body=body, base_url=base_url))
    73.  
    74. contenthtml.append(tuple([reportid, reportcontent]))
    75.  
    76. except lxml.etree.XMLSyntaxError:
    77. contenthtml = []
    78. contenthtml.append(html)
    79. save__attachment = {} # Don't save this potentially malformed document
    80.  
    81. # Get paperformat arguments set in the root html tag. They are prioritized over
    82. # paperformat-record arguments.
    83. specific_paperformat_args = {}
    84. for attribute in root.items():
    85. if attribute[0].startswith('data-report-'):
    86. specific_paperformat_args[attribute[0]] = attribute[1]
    87.  
    88. # Run wkhtmltopdf process
    89. return self._run_wkhtmltopdf(
    90. cr, uid, headerhtml, footerhtml, contenthtml, context.get('landscape'),
    91. paperformat, specific_paperformat_args, save_in_attachment,
    92. context.get('set_viewport_size')
    93. )

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: