forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreso
executable file
·132 lines (108 loc) · 4.75 KB
/
preso
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
#!/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):
# 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:
self.fullwidth = 1366
self.fullheight = 768
gtk.Window.__init__(self)
# set the size to the size of my laptop:
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 gtk.gdk.screen_height() <= self.displayheight :
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()
# 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__":
if len(sys.argv) <= 1 :
print "Usage:", sys.argv[0], "url"
sys.exit(0)
# Figure out if it's a filename or a url
url = sys.argv[1]
if url.find(':') < 0 :
if url[0] == '/' :
url = 'file://' + url
else :
url = 'file://' + os.getcwd() + '/' + url
gobject.threads_init()
webbrowser = PresoViewer(url)
gtk.main()