Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 150 lines (123 sloc) 5.57 KB
#!/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, 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 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__":
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, zoom=1, show_notes=True)
gtk.main()