Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. from gi.repository import GLib, Gtk, Poppler
  5.  
  6.  
  7. class PrintingApp:
  8. def __init__(self, file_uri):
  9. self.operation = Gtk.PrintOperation()
  10.  
  11. self.operation.connect('begin-print', self.begin_print, None)
  12. self.operation.connect('draw-page', self.draw_page, None)
  13.  
  14. self.doc = Poppler.Document.new_from_file(file_uri)
  15.  
  16. def begin_print(self, operation, print_ctx, print_data):
  17. operation.set_n_pages(self.doc.get_n_pages())
  18.  
  19. def draw_page(self, operation, print_ctx, page_num, print_data):
  20. cr = print_ctx.get_cairo_context()
  21. page = self.doc.get_page(page_num)
  22. page.render(cr)
  23.  
  24. def run(self, parent=None):
  25. result = self.operation.run(Gtk.PrintOperationAction.PRINT_DIALOG,
  26. parent)
  27.  
  28. if result == Gtk.PrintOperationResult.ERROR:
  29. message = self.operation.get_error()
  30.  
  31. dialog = Gtk.MessageDialog(parent,
  32. 0,
  33. Gtk.MessageType.ERROR,
  34. Gtk.ButtonsType.CLOSE,
  35. message)
  36.  
  37. dialog.run()
  38. dialog.destroy()
  39.  
  40. Gtk.main_quit()
  41.  
  42.  
  43. def main():
  44. if len(sys.argv) != 2:
  45. print "%s FILE" % sys.argv[0]
  46. sys.exit(1)
  47.  
  48. file_uri = GLib.filename_to_uri(os.path.abspath(sys.argv[1]))
  49.  
  50. main_window = Gtk.OffscreenWindow()
  51. app = PrintingApp(file_uri)
  52. GLib.idle_add(app.run, main_window)
  53. Gtk.main()
  54.  
  55. if __name__ == '__main__':
  56. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement