2015-05-16 70 views

回答

1

在一般情況Qweb報告可以打印在雙向

  1. HTML
  2. PDF

每一次聽到,當你調用根據報告類型報告不同的報告方法正在調用。

如果調用報告PDF那麼get_pdf()方法被調用或如果你打電話報告類型爲HTML然後get_html()方法被稱爲報表模塊。

因此,在我們的情況下,您必須重寫我們模塊中的上述兩種方法,然後添加一些像這樣的東西。

改寫報表模塊的get_pdf()方法:

class Report(osv.Model): 
    _inherit = "report" 
    _description = "Report" 

@api.v7 
def get_pdf(self, cr, uid, ids, report_name, html=None, data=None, context=None): 
    """This method generates and returns pdf version of a report. 
    """ 
    order_pool=self.pool.get('sale.order') 
    for order in order_pool.browse(cr, uid, ids, context=None): 
     if order.state: 
      if order.state == 'draft': 
       raise osv.except_osv(_("Warning!"), _("Your Printed Report is in Draft State ...!! ")) 

    if context is None: 
     context = {} 

    if html is None: 
     html = self.get_html(cr, uid, ids, report_name, data=data, context=context) 

    html = html.decode('utf-8') # Ensure the current document is utf-8 encoded. 

    # Get the ir.actions.report.xml record we are working on. 

    report = self._get_report_from_name(cr, uid, report_name) 
    # Check if we have to save the report or if we have to get one from the db. 
    save_in_attachment = self._check_attachment_use(cr, uid, ids, report) 
    # Get the paperformat associated to the report, otherwise fallback on the company one. 
    if not report.paperformat_id: 
     user = self.pool['res.users'].browse(cr, uid, uid) 
     paperformat = user.company_id.paperformat_id 
    else: 
     paperformat = report.paperformat_id 

    # Preparing the minimal html pages 
    css = '' # Will contain local css 
    headerhtml = [] 
    contenthtml = [] 
    footerhtml = [] 
    irconfig_obj = self.pool['ir.config_parameter'] 
    base_url = irconfig_obj.get_param(cr, SUPERUSER_ID, 'report.url') or irconfig_obj.get_param(cr, SUPERUSER_ID, 'web.base.url') 

    # Minimal page renderer 
    view_obj = self.pool['ir.ui.view'] 
    render_minimal = partial(view_obj.render, cr, uid, 'report.minimal_layout', context=context) 

    # The received html report must be simplified. We convert it in a xml tree 
    # in order to extract headers, bodies and footers. 
    try: 
     root = lxml.html.fromstring(html) 
     match_klass = "//div[contains(concat(' ', normalize-space(@class), ' '), ' {} ')]" 

     for node in root.xpath("//html/head/style"): 
      css += node.text 

     for node in root.xpath(match_klass.format('header')): 
      body = lxml.html.tostring(node) 
      header = render_minimal(dict(css=css, subst=True, body=body, base_url=base_url)) 
      headerhtml.append(header) 

     for node in root.xpath(match_klass.format('footer')): 
      body = lxml.html.tostring(node) 
      footer = render_minimal(dict(css=css, subst=True, body=body, base_url=base_url)) 
      footerhtml.append(footer) 

     for node in root.xpath(match_klass.format('page')): 
      # Previously, we marked some reports to be saved in attachment via their ids, so we 
      # must set a relation between report ids and report's content. We use the QWeb 
      # branding in order to do so: searching after a node having a data-oe-model 
      # attribute with the value of the current report model and read its oe-id attribute 
      if ids and len(ids) == 1: 
       reportid = ids[0] 
      else: 
       oemodelnode = node.find(".//*[@data-oe-model='%s']" % report.model) 
       if oemodelnode is not None: 
        reportid = oemodelnode.get('data-oe-id') 
        if reportid: 
         reportid = int(reportid) 
       else: 
        reportid = False 

      # Extract the body 
      body = lxml.html.tostring(node) 
      reportcontent = render_minimal(dict(css=css, subst=False, body=body, base_url=base_url)) 

      contenthtml.append(tuple([reportid, reportcontent])) 

    except lxml.etree.XMLSyntaxError: 
     contenthtml = [] 
     contenthtml.append(html) 
     save_in_attachment = {} # Don't save this potentially malformed document 

    # Get paperformat arguments set in the root html tag. They are prioritized over 
    # paperformat-record arguments. 
    specific_paperformat_args = {} 
    for attribute in root.items(): 
     if attribute[0].startswith('data-report-'): 
      specific_paperformat_args[attribute[0]] = attribute[1] 

    # Run wkhtmltopdf process 
    return self._run_wkhtmltopdf(
     cr, uid, headerhtml, footerhtml, contenthtml, context.get('landscape'), 
     paperformat, specific_paperformat_args, save_in_attachment 
    ) 

由於相同的方法,你可以在你的模塊中爲get_html覆蓋(),並檢查它

聽到的代碼將檢查銷售訂單報告行動。

上面的代碼可以從我身邊成功測試。

我希望這對你有所幫助.. :)