1+ # -*- coding: utf-8 -*-
2+ ##############################################################################
3+ #
4+ # OpenERP, Open Source Management Solution
5+ # Copyright (C) 2014 CodUP (<http://codup.com>).
6+ #
7+ # This program is free software: you can redistribute it and/or modify
8+ # it under the terms of the GNU Affero General Public License as
9+ # published by the Free Software Foundation, either version 3 of the
10+ # License, or (at your option) any later version.
11+ #
12+ # This program is distributed in the hope that it will be useful,
13+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ # GNU Affero General Public License for more details.
16+ #
17+ # You should have received a copy of the GNU Affero General Public License
18+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
19+ #
20+ ##############################################################################
21+
22+ import subprocess
23+ import os
24+ import tempfile
25+ import time
26+ from openerp .tools .translate import _
27+ from openerp .osv .osv import except_osv
28+ from openerp .addons .report_webkit .webkit_report import WebKitParser
29+
30+ import logging
31+ _logger = logging .getLogger (__name__ )
32+
33+ def generate_pdf (self , comm_path , report_xml , header , footer , html_list , webkit_header = False ):
34+ """Call webkit in order to generate pdf"""
35+ if not webkit_header :
36+ webkit_header = report_xml .webkit_header
37+ tmp_dir = tempfile .gettempdir ()
38+ out_filename = tempfile .mktemp (suffix = ".pdf" , prefix = "webkit.tmp." )
39+ file_to_del = [out_filename ]
40+ if comm_path :
41+ command = [comm_path ]
42+ else :
43+ command = ['wkhtmltopdf' ]
44+
45+ command .append ('--quiet' )
46+ # default to UTF-8 encoding. Use <meta charset="latin-1"> to override.
47+ command .extend (['--encoding' , 'utf-8' ])
48+ if header :
49+ head_file = file ( os .path .join (
50+ tmp_dir ,
51+ str (time .time ()) + '.head.html'
52+ ),
53+ 'w'
54+ )
55+ head_file .write (header )
56+ head_file .close ()
57+ file_to_del .append (head_file .name )
58+ command .extend (['--header-html' , head_file .name ])
59+ if footer :
60+ foot_file = file ( os .path .join (
61+ tmp_dir ,
62+ str (time .time ()) + '.foot.html'
63+ ),
64+ 'w'
65+ )
66+ foot_file .write (footer )
67+ foot_file .close ()
68+ file_to_del .append (foot_file .name )
69+ command .extend (['--footer-html' , foot_file .name ])
70+
71+ if webkit_header .scale :
72+ command .extend (['--zoom' , str (webkit_header .scale ).replace (',' , '.' )])
73+ if webkit_header .margin_top :
74+ command .extend (['--margin-top' , str (webkit_header .margin_top ).replace (',' , '.' )])
75+ if webkit_header .margin_bottom :
76+ command .extend (['--margin-bottom' , str (webkit_header .margin_bottom ).replace (',' , '.' )])
77+ if webkit_header .margin_left :
78+ command .extend (['--margin-left' , str (webkit_header .margin_left ).replace (',' , '.' )])
79+ if webkit_header .margin_right :
80+ command .extend (['--margin-right' , str (webkit_header .margin_right ).replace (',' , '.' )])
81+ if webkit_header .orientation :
82+ command .extend (['--orientation' , str (webkit_header .orientation ).replace (',' , '.' )])
83+ if webkit_header .format :
84+ command .extend (['--page-size' , str (webkit_header .format ).replace (',' , '.' )])
85+ count = 0
86+ for html in html_list :
87+ html_file = file (os .path .join (tmp_dir , str (time .time ()) + str (count ) + '.body.html' ), 'w' )
88+ count += 1
89+ html_file .write (html )
90+ html_file .close ()
91+ file_to_del .append (html_file .name )
92+ command .append (html_file .name )
93+ command .append (out_filename )
94+ stderr_fd , stderr_path = tempfile .mkstemp (text = True )
95+ file_to_del .append (stderr_path )
96+ try :
97+ status = subprocess .call (command , stderr = stderr_fd )
98+ os .close (stderr_fd ) # ensure flush before reading
99+ stderr_fd = None # avoid closing again in finally block
100+ fobj = open (stderr_path , 'r' )
101+ error_message = fobj .read ()
102+ fobj .close ()
103+ if not error_message :
104+ error_message = _ ('No diagnosis message was provided' )
105+ else :
106+ error_message = _ ('The following diagnosis message was provided:\n ' ) + error_message
107+ if status :
108+ raise except_osv (_ ('Webkit error' ),
109+ _ ("The command 'wkhtmltopdf' failed with error code = %s. Message: %s" ) % (status , error_message ))
110+ pdf_file = open (out_filename , 'rb' )
111+ pdf = pdf_file .read ()
112+ pdf_file .close ()
113+ finally :
114+ if stderr_fd is not None :
115+ os .close (stderr_fd )
116+ for f_to_del in file_to_del :
117+ try :
118+ os .unlink (f_to_del )
119+ except (OSError , IOError ), exc :
120+ _logger .error ('cannot remove file %s: %s' , f_to_del , exc )
121+ return pdf
122+
123+ WebKitParser .generate_pdf = generate_pdf
124+
125+ # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
0 commit comments