forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpreso
executable file
·162 lines (135 loc) · 6.35 KB
/
gpreso
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
#!/usr/bin/env python
# python-gtk-webkit presentation program.
# Copyright (C) 2009,2011 by Akkana Peck.
# Share and enjoy under the GPL v2 or later.
# Version 0.6
import sys, os
import gtk, gobject
import webkit
class PresoViewer(gtk.Window):
def __init__(self, url, fullscreen=False, zoom=1.0, show_notes=True):
# Are we making screenshots? TODO: make this a command-line param.
self.make_screenshots = False
self.imgnum = 0
# Size the audience will see (used for converting to images):
self.displaywidth = 1024
self.displayheight = 768
# Size of the window we'll actually display:
if show_notes:
self.fullwidth = 1366
else:
self.fullwidth = 1024
self.fullheight = 768
if zoom != 1.0 :
self.displaywidth = int(self.displaywidth * zoom)
self.displayheight = int(self.displayheight * zoom)
self.fullwidth = int(self.fullwidth * zoom)
self.fullheight = int(self.fullheight * zoom)
print "Display size: %d x %d" % (self.displaywidth,
self.displayheight)
print "Full size: %d x %d" % (self.fullwidth,
self.fullheight)
gtk.Window.__init__(self)
# set the window to the right size:
self.set_default_size(self.fullwidth, self.fullheight)
# If the physical display is bigger than displaywidth,
# then it's better not to run fullscreen.
if fullscreen or \
(gtk.gdk.screen_height() <= self.displayheight and zoom == 1.0):
self.fullscreen()
self.browser = webkit.WebView()
self.add(self.browser)
self.connect('destroy', gtk.main_quit)
self.browser.connect("title-changed", self.title_changed)
self.browser.connect("key-press-event", self.key_press_event)
self.browser.open(url) # throw err if url isn't defined
self.show_all()
# Try to prevent the window from resizing if content unexpectedly grows:
self.resize(self.fullwidth, self.fullheight)
self.set_size_request(self.fullwidth, self.fullheight)
# An attempt to print to PDF. None of this works.
# Nobody seems to know how to do it from Python, though
# there's a C++ webkit printing example that's said to work:
# https://github.com/antialize/wkhtmltopdf
# There's also this, a standalone app, but it seems to render
# to a bitmap then convert that to PDF.
# https://github.com/marianoguerra/webshot/blob/master/webshot.py
# And there's commandlineprint, which uses firefox, not webkit:
# http://sites.google.com/site/torisugari/commandlineprint2
# Other links:
# http://eegg.wordpress.com/2010/01/17/html-to-pdf/
# http://notes.alexdong.com/xhtml-to-pdf-using-pyqt4-webkit-and-headless
#
# def pdf(self) :
# webframe = self.browser.get_main_frame()
# # Even the simple print dialog solution they use from the
# # python-webkit example doesn't work. It doesn't work in
# # the example, either.
# webframe.print_full(gtk.PrintOperation(),
# gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG);
# op = gtk.PrintOperation()
# #op.set_n_pages(1)
# filename = "/tmp/screenshot%03d.pdf" % self.imgnum
# op.set_export_filename(filename)
# #op.connect("begin-print", self.begin_print_cb)
# #op.connect("draw-page", self.draw_page_cb)
# #op.run(gtk.PRINT_OPERATION_ACTION_EXPORT, self)
# print 'Trying to export to', filename
# webframe.print_full(op, gtk.PRINT_OPERATION_ACTION_EXPORT)
# print 'exported to', filename, ', maybe'
def screenshot(self) :
'''Save a screenshot to a jpg file'''
pixmap = self.get_snapshot(gtk.gdk.Rectangle(0, 0,
self.displaywidth,
self.displayheight))
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8,
self.displaywidth, self.displayheight)
pixbuf.get_from_drawable(self.window, self.get_colormap(),
0, 0, 0, 0,
self.displaywidth, self.displayheight)
pixbuf.save("screenshot%03d.jpg" % self.imgnum, "jpeg")
def key_press_event(self, widget, event) :
if event.keyval == gtk.keysyms.q and \
event.state == gtk.gdk.CONTROL_MASK :
print "Bye!"
gtk.main_quit()
elif event.string == ' ' :
self.imgnum += 1
# If we're in screen-shooting mode, save the current slide:
if self.make_screenshots :
self.screenshot()
return False
elif event.keyval == gtk.keysyms.r :
if event.state == gtk.gdk.SHIFT_MASK :
self.browser.reload_bypass_cache()
else :
self.browser.reload()
return True
return False # False means we haven't handled, pass event on
def title_changed(self, webview, frame, title):
self.set_title(title)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="A simple HTML presentation tool")
parser.add_argument('-f', "--fullscreen", dest="fullscreen", default=False,
action="store_true",
help="Run fullscreen with no window decorations")
parser.add_argument('-n', "--show-notes", dest="show_notes", default=True,
action="store_true",
help="Make the screen 1366 pixels wide "
"instead of 1024, to show presenter notes")
parser.add_argument('-z', action="store", default=1, dest="zoom", type=int,
help='Zoom level')
parser.add_argument('url', help='The URL to open')
args = parser.parse_args(sys.argv[1:])
if ':' in args.url:
url = args.url
else:
if args.url[0] == '/' :
url = 'file://' + args.url
else :
url = 'file://' + os.getcwd() + '/' + args.url
gobject.threads_init()
webbrowser = PresoViewer(url, zoom=args.zoom, fullscreen=args.fullscreen,
show_notes=args.show_notes)
gtk.main()