[MERGE] Forward-port of latest 7.0 bugfixes, up to rev. 9929 revid:dle@openerp.com...
[odoo/odoo.git] / addons / report_webkit / webkit_report.py
index 4f56d7e..60fb536 100644 (file)
@@ -112,6 +112,7 @@ def webkit_report_extender(report_name):
         return fct
     return fct1
 
+
 class WebKitParser(report_sxw):
     """Custom class that use webkit to render HTML reports
        Code partially taken from report openoffice. Thanks guys :)
@@ -173,7 +174,7 @@ class WebKitParser(report_sxw):
                                  ),
                                 'w'
                             )
-            head_file.write(header.encode('utf-8'))
+            head_file.write(self._sanitize_html(header.encode('utf-8')))
             head_file.close()
             file_to_del.append(head_file.name)
             command.extend(['--header-html', head_file.name])
@@ -184,7 +185,7 @@ class WebKitParser(report_sxw):
                                  ),
                                 'w'
                             )
-            foot_file.write(footer.encode('utf-8'))
+            foot_file.write(self._sanitize_html(footer.encode('utf-8')))
             foot_file.close()
             file_to_del.append(foot_file.name)
             command.extend(['--footer-html', foot_file.name])
@@ -205,7 +206,7 @@ class WebKitParser(report_sxw):
         for html in html_list :
             html_file = file(os.path.join(tmp_dir, str(time.time()) + str(count) +'.body.html'), 'w')
             count += 1
-            html_file.write(html.encode('utf-8'))
+            html_file.write(self._sanitize_html(html.encode('utf-8')))
             html_file.close()
             file_to_del.append(html_file.name)
             command.append(html_file.name)
@@ -366,7 +367,6 @@ class WebKitParser(report_sxw):
         pdf = self.generate_pdf(bin, report_xml, head, foot, htmls)
         return (pdf, 'pdf')
 
-
     def create(self, cursor, uid, ids, data, context=None):
         """We override the create function in order to handle generator
            Code taken from report openoffice. Thanks guys :) """
@@ -387,11 +387,18 @@ class WebKitParser(report_sxw):
             report_xml.report_sxw = None
         else:
             return super(WebKitParser, self).create(cursor, uid, ids, data, context)
-        if report_xml.report_type != 'webkit' :
+        if report_xml.report_type != 'webkit':
             return super(WebKitParser, self).create(cursor, uid, ids, data, context)
         result = self.create_source_pdf(cursor, uid, ids, data, report_xml, context)
         if not result:
             return (False,False)
         return result
 
+    def _sanitize_html(self, html):
+        """wkhtmltopdf expects the html page to declare a doctype.
+        """
+        if html and html[:9].upper() != "<!DOCTYPE":
+            html = "<!DOCTYPE html>\n" + html
+        return html
+
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: