-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_pdf_both_sides.py
269 lines (232 loc) · 12.2 KB
/
report_pdf_both_sides.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding: utf-8 -*-
##############################################################################
#
# Extraschool
# Copyright (C) 2008-2014
# Jean-Michel Abé - Town of La Bruyère (<http://www.labruyere.be>)
# Michael Michot - Imio (<http://www.imio.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import api
from openerp import SUPERUSER_ID
from openerp.exceptions import AccessError
from openerp.osv import osv, fields
from openerp.tools import config
from openerp.tools.misc import find_in_path
from openerp.tools.translate import _
from openerp.addons.web.http import request
from openerp.tools.safe_eval import safe_eval as eval
import re
import time
import base64
import logging
import tempfile
import lxml.html
import os
import subprocess
from contextlib import closing
from distutils.version import LooseVersion
from functools import partial
from pyPdf import PdfFileWriter, PdfFileReader
from pyPdf import PdfFileWriter, PdfFileReader
import tempfile
from contextlib import closing
import os
try:
import cStringIO as StringIO
except ImportError:
import StringIO
#--------------------------------------------------------------------------
# Helpers
#--------------------------------------------------------------------------
_logger = logging.getLogger(__name__)
def _get_wkhtmltopdf_bin():
wkhtmltopdf_bin = find_in_path('wkhtmltopdf')
if wkhtmltopdf_bin is None:
raise IOError
return wkhtmltopdf_bin
class report_paperformat(osv.osv):
_inherit = 'report.paperformat'
_columns = {
'both_sides': fields.boolean('Both sides'),
}
class report_pdf_both_sides(osv.Model):
_inherit = 'report'
def _merge_pdf(self, documents, both_sides = False):
print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print "merge_pdf %s" % (both_sides)
print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
"""Merge PDF files into one.
:param documents: list of path of pdf files
:returns: path of the merged pdf
"""
blankpdfstr = '''JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURl
Y29kZT4+CnN0cmVhbQp4nDPQM1Qo5ypUMFAwALJMLU31jBQsTAz1LBSKUrnCtRTyuAIVAIcdB3IK
ZW5kc3RyZWFtCmVuZG9iagoKMyAwIG9iago0MgplbmRvYmoKCjUgMCBvYmoKPDwKPj4KZW5kb2Jq
Cgo2IDAgb2JqCjw8L0ZvbnQgNSAwIFIKL1Byb2NTZXRbL1BERi9UZXh0XQo+PgplbmRvYmoKCjEg
MCBvYmoKPDwvVHlwZS9QYWdlL1BhcmVudCA0IDAgUi9SZXNvdXJjZXMgNiAwIFIvTWVkaWFCb3hb
MCAwIDU5NSA4NDJdL0dyb3VwPDw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QkU5NjVGMzBDNTND
MkRGNUFGNgo+PgpzdGFydHhyZWYKNzM2CiUlRU9GCg=='''
writer = PdfFileWriter()
blank_page = PdfFileReader(StringIO.StringIO(blankpdfstr.decode("base64"))).pages[0]
streams = [] # We have to close the streams *after* PdfFilWriter's call to write()
for document in documents:
pdfreport = file(document, 'rb')
streams.append(pdfreport)
reader = PdfFileReader(pdfreport)
for page in range(0, reader.getNumPages()):
writer.addPage(reader.getPage(page))
if reader.getNumPages() % 2 and both_sides:
writer.addPage(blank_page)
merged_file_fd, merged_file_path = tempfile.mkstemp(suffix='.html', prefix='report.merged.tmp.')
with closing(os.fdopen(merged_file_fd, 'w')) as merged_file:
writer.write(merged_file)
for stream in streams:
stream.close()
return merged_file_path
def _run_wkhtmltopdf(self, cr, uid, headers, footers, bodies, landscape, paperformat, spec_paperformat_args=None, save_in_attachment=None):
"""Execute wkhtmltopdf as a subprocess in order to convert html given in input into a pdf
document.
:param header: list of string containing the headers
:param footer: list of string containing the footers
:param bodies: list of string containing the reports
:param landscape: boolean to force the pdf to be rendered under a landscape format
:param paperformat: ir.actions.report.paperformat to generate the wkhtmltopf arguments
:param specific_paperformat_args: dict of prioritized paperformat arguments
:param save_in_attachment: dict of reports to save/load in/from the db
:returns: Content of the pdf as a string
"""
command_args = []
# Passing the cookie to wkhtmltopdf in order to resolve internal links.
try:
if request:
command_args.extend(['--cookie', 'session_id', request.session.sid])
except AttributeError:
pass
# Wkhtmltopdf arguments
command_args.extend(['--quiet']) # Less verbose error messages
if paperformat:
# Convert the paperformat record into arguments
command_args.extend(self._build_wkhtmltopdf_args(paperformat, spec_paperformat_args))
both_sides = paperformat.both_sides
else:
both_sides = False
# Force the landscape orientation if necessary
if landscape and '--orientation' in command_args:
command_args_copy = list(command_args)
for index, elem in enumerate(command_args_copy):
if elem == '--orientation':
del command_args[index]
del command_args[index]
command_args.extend(['--orientation', 'landscape'])
elif landscape and not '--orientation' in command_args:
command_args.extend(['--orientation', 'landscape'])
# Execute WKhtmltopdf
pdfdocuments = []
temporary_files = []
for index, reporthtml in enumerate(bodies):
local_command_args = []
pdfreport_fd, pdfreport_path = tempfile.mkstemp(suffix='.pdf', prefix='report.tmp.')
temporary_files.append(pdfreport_path)
# Directly load the document if we already have it
if save_in_attachment and save_in_attachment['loaded_documents'].get(reporthtml[0]):
with closing(os.fdopen(pdfreport_fd, 'w')) as pdfreport:
pdfreport.write(save_in_attachment['loaded_documents'][reporthtml[0]])
pdfdocuments.append(pdfreport_path)
continue
else:
os.close(pdfreport_fd)
# Wkhtmltopdf handles header/footer as separate pages. Create them if necessary.
if headers:
head_file_fd, head_file_path = tempfile.mkstemp(suffix='.html', prefix='report.header.tmp.')
temporary_files.append(head_file_path)
with closing(os.fdopen(head_file_fd, 'w')) as head_file:
head_file.write(headers[index])
local_command_args.extend(['--header-html', head_file_path])
if footers:
foot_file_fd, foot_file_path = tempfile.mkstemp(suffix='.html', prefix='report.footer.tmp.')
temporary_files.append(foot_file_path)
with closing(os.fdopen(foot_file_fd, 'w')) as foot_file:
foot_file.write(footers[index])
local_command_args.extend(['--footer-html', foot_file_path])
# Body stuff
content_file_fd, content_file_path = tempfile.mkstemp(suffix='.html', prefix='report.body.tmp.')
temporary_files.append(content_file_path)
with closing(os.fdopen(content_file_fd, 'w')) as content_file:
content_file.write(reporthtml[1])
try:
wkhtmltopdf = [_get_wkhtmltopdf_bin()] + command_args + local_command_args
wkhtmltopdf += [content_file_path] + [pdfreport_path]
process = subprocess.Popen(wkhtmltopdf, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if process.returncode not in [0, 1]:
raise osv.except_osv(_('Report (PDF)'),
_('Wkhtmltopdf failed (error code: %s). '
'Message: %s') % (str(process.returncode), err))
# Save the pdf in attachment if marked
if reporthtml[0] is not False and save_in_attachment.get(reporthtml[0]):
with open(pdfreport_path, 'rb') as pdfreport:
attachment = {
'name': save_in_attachment.get(reporthtml[0]),
'datas': base64.encodestring(pdfreport.read()),
'datas_fname': save_in_attachment.get(reporthtml[0]),
'res_model': save_in_attachment.get('model'),
'res_id': reporthtml[0],
}
try:
self.pool['ir.attachment'].create(cr, uid, attachment)
except AccessError:
_logger.warning("Cannot save PDF report %r as attachment",
attachment['name'])
else:
_logger.info('The PDF document %s is now saved in the database',
attachment['name'])
pdfdocuments.append(pdfreport_path)
except:
raise
# Return the entire document
if len(pdfdocuments) == 1:
entire_report_path = pdfdocuments[0]
else:
print "---------------------------------------"
print "---------------------------------------"
print "both sides : %s" % (both_sides)
print "---------------------------------------"
print "---------------------------------------"
entire_report_path = self._merge_pdf(pdfdocuments,both_sides)
temporary_files.append(entire_report_path)
with open(entire_report_path, 'rb') as pdfdocument:
content = pdfdocument.read()
# Manual cleanup of the temporary files
for temporary_file in temporary_files:
try:
os.unlink(temporary_file)
except (OSError, IOError):
_logger.error('Error when trying to remove file %s' % temporary_file)
return content