Attachment 'PDF-1.0.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - PageAsPDF action Version 1.0, 6.02.2013
4
5 @copyright: 2013 Ian Riley <ian.riley@internode.on.net>
6
7 History:
8
9 Version 1.0 - 6.02.2013: initial version.
10
11 incorporting code from:
12
13 @copyright: 2001 by Ken Sugino (sugino@mediaone.net),
14 2001-2004 by Juergen Hermann <jh@web.de>,
15 2005 MoinMoin:AlexanderSchremmer,
16 2005 DiegoOngaro at ETSZONE (diego@etszone.com),
17 2005-2007 MoinMoin:ReimarBauer,
18 2007-2008 MoinMoin:ThomasWaldmann
19
20 @license: GNU GPL, see COPYING for details.
21 """
22 import os, time, codecs, datetime
23 from subprocess import call
24 from werkzeug import http_date
25 from MoinMoin import config, wikiutil
26 from MoinMoin.Page import Page
27
28 action_name = __name__.split('.')[-1]
29 actname = {'actname': action_name}
30
31 def execute(pagename, request):
32
33 _ = request.getText
34
35 if not request.user.may.read(pagename):
36 Page(request, pagename).send_page()
37
38 page = request.page
39 filename = '%s.pdf' % page.page_name.replace('/','-')
40
41 #prepare for subprocess call
42 #set app to local path for wkhtmltopdf
43 app = '/Applications/wkhtmltopdf.app/Contents/MacOS/wkhtmltopdf'
44 url = request.url.replace('action='+action_name,'action=print')
45 params = [app, ]
46 params += [
47 #global options
48 '-q',
49 '--margin-top', '20mm',
50 '--margin-bottom', '20mm',
51 '--margin-left', '10mm',
52 '--margin-right', '10mm',
53 '--title', filename,
54 #header options
55 '--header-font-size', '9',
56 '--header-spacing', '10',
57 #'--header-line',
58 '--header-left', '[title]',
59 #'--header-center', '',
60 '--header-right', '[date] [time]',
61 #footer options
62 '--footer-font-size', '9',
63 '--footer-spacing', '10',
64 #'--footer-line',
65 '--footer-left', '[webpage]',
66 #'--footer-center', '',
67 '--footer-right', '[page] of [topage]', ]
68 #page and page options
69 params += [url, ]
70 for name, value in request.cookies.iteritems():
71 ck_name = name
72 ck_val = str(value)
73 params += ['--cookie', ck_name, ck_val,]
74 #output file
75 fpath = request.rootpage.getPagePath('wiki-page.pdf', isfile=1)
76 params += [fpath]
77
78 #make and respond to subprocess call
79 call_error = call(params)
80 if call_error:
81 msg = '%(actname)s failed.' % actname
82 msg += ' Subprocess:' + ' '.join(params)
83 request.theme.add_msg(msg, 'info')
84 Page(request, pagename).send_page()
85 else:
86 filename_enc = filename.encode(config.charset)
87 timestamp = datetime.datetime.fromtimestamp(os.path.getmtime(fpath))
88 mt = wikiutil.MimeType(filename=filename)
89 content_type = mt.content_type()
90 mime_type = mt.mime_type()
91 now = time.time()
92 request.headers['Date'] = http_date(now)
93 request.headers['Content-Type'] = content_type
94 request.headers['Last-Modified'] = http_date(timestamp)
95 request.headers['Expires'] = http_date(now - 365 * 24 * 3600)
96 request.headers['Content-Length'] = os.path.getsize(fpath)
97 content_dispo_string = '%s; filename="%s"' % ('inline', filename_enc)
98 request.headers['Content-Disposition'] = content_dispo_string
99 # send data
100 request.send_file(open(fpath, 'rb'))
You are not allowed to attach a file to this page.