Skip to content

Instantly share code, notes, and snippets.

@felipeborges
Created December 16, 2015 11:13
Show Gist options
  • Save felipeborges/79b437fe8d8aff0f5d5d to your computer and use it in GitHub Desktop.
Save felipeborges/79b437fe8d8aff0f5d5d to your computer and use it in GitHub Desktop.
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const Poppler = imports.gi.Poppler;
const FILE_NAME = 'file:///home/fborges/Documents/test.pdf';
const PrintOperationExample = new Lang.Class({
Name: 'PrintOperationExample',
Extends: Gtk.Application,
_init: function() {
this.parent({ application_id: 'com.example.PrintOperationExample' });
},
vfunc_startup: function() {
this.parent();
this.operation = new Gtk.PrintOperation();
this.operation.connect('begin-print', this.begin_print.bind(this));
this.operation.connect('draw-page', this.draw_page.bind(this));
this.doc = Poppler.Document.new_from_file(FILE_NAME, '');
},
begin_print: function(operation, context, data) {
operation.set_n_pages(this.doc.get_n_pages());
},
draw_page: function(operation, context, page_num, data) {
let cr = context.get_cairo_context()
let page = this.doc.get_page(page_num);
page.render(cr);
},
run_print_operation: function() {
let result = this.operation.run(Gtk.PrintOperationAction.PRINT_DIALOG, null);
if (result == Gtk.PrintOperationResult.ERROR) {
log(self.operation.get_error());
}
},
vfunc_activate: function() {
this._window = new Gtk.ApplicationWindow({
application: this,
title: "Print Operation example"
});
let button = new Gtk.Button({
label: "Print",
});
this._window.add(button);
button.connect('clicked', this.run_print_operation.bind(this));
this._window.show_all();
},
});
let app = new PrintOperationExample();
app.run(ARGV);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment