def __begin_print_cb(self, operation, context, data=None): settings = operation.get_print_settings() pixbuf = self._get_view_image() rect = self.output.get_allocation() paper_size = gtk.paper_size_new_custom("custom", "custom", pixel_to_mm(rect.width), pixel_to_mm(rect.height), gtk.UNIT_MM) settings.set_paper_size(paper_size) page_setup = gtk.PageSetup() page_setup.set_top_margin(0, gtk.UNIT_POINTS) page_setup.set_bottom_margin(0, gtk.UNIT_POINTS) page_setup.set_left_margin(0, gtk.UNIT_POINTS) page_setup.set_right_margin(0, gtk.UNIT_POINTS) operation.set_default_page_setup(page_setup) settings.set_orientation operation.set_n_pages(1)
def set_document(self, filename, operation, context): if not filename.startswith('file'): filename = 'file://' + os.path.realpath(filename) self.d = poppler.document_new_from_file(filename, None) operation.set_n_pages(self.d.get_n_pages()) # Assume all pages are same page = self.d.get_page(0) w, h = page.get_size() if w > h: w, h = h, w ori = gtk.PAGE_ORIENTATION_LANDSCAPE else: ori = gtk.PAGE_ORIENTATION_PORTRAIT page_setup = gtk.PageSetup() page_setup.set_orientation(ori) size = int(round(w)), int(round(h)) gtk_size = rl2gtk_papersizes.get(size, None) if gtk_size: ps = gtk.PaperSize(gtk_size) else: ps = gtk.paper_size_new_custom('', '', w, h, gtk.UNIT_POINTS) page_setup.set_paper_size(ps) operation.set_default_page_setup(page_setup)
def set_document (self, filename, operation,context): if not filename.startswith('file'): filename = 'file://' + os.path.realpath(filename) self.d = poppler.document_new_from_file(filename,None) operation.set_n_pages(self.d.get_n_pages()) # Assume all pages are same page = self.d.get_page(0) w,h = page.get_size() if w > h: w,h = h,w ori = gtk.PAGE_ORIENTATION_LANDSCAPE else: ori = gtk.PAGE_ORIENTATION_PORTRAIT page_setup = gtk.PageSetup() page_setup.set_orientation(ori) size = int(round(w)),int(round(h)) gtk_size = rl2gtk_papersizes.get(size,None) if gtk_size: ps = gtk.PaperSize(gtk_size) else: ps = gtk.paper_size_new_custom('','',w,h,gtk.UNIT_POINTS) page_setup.set_paper_size(ps) operation.set_default_page_setup(page_setup)
def paperstyle_to_pagesetup(paper_style): """Convert a PaperStyle instance into a gtk.PageSetup instance. @param paper_style: Gramps paper style object to convert @param type: PaperStyle @return: page_setup @rtype: gtk.PageSetup """ # paper size names according to 'PWG Candidate Standard 5101.1-2002' # ftp://ftp.pwg.org/pub/pwg/candidates/cs-pwgmsn10-20020226-5101.1.pdf gramps_to_gtk = { "Letter": "na_letter", "Legal": "na_legal", "A0": "iso_a0", "A1": "iso_a1", "A2": "iso_a2", "A3": "iso_a3", "A4": "iso_a4", "A5": "iso_a5", "B0": "iso_b0", "B1": "iso_b1", "B2": "iso_b2", "B3": "iso_b3", "B4": "iso_b4", "B5": "iso_b5", "B6": "iso_b6", "B": "na_ledger", "C": "na_c", "D": "na_d", "E": "na_e", } # First set the paper size gramps_paper_size = paper_style.get_size() gramps_paper_name = gramps_paper_size.get_name() # All sizes not included in the translation table (even if a standard size) # are handled as custom format, because we are not intelligent enough. if gramps_paper_name in gramps_to_gtk: paper_size = gtk.PaperSize(gramps_to_gtk[gramps_paper_name]) log.debug("Selected paper size: %s" % gramps_to_gtk[gramps_paper_name]) else: if paper_style.get_orientation() == PAPER_PORTRAIT: paper_width = gramps_paper_size.get_width() * 10 paper_height = gramps_paper_size.get_height() * 10 else: paper_width = gramps_paper_size.get_height() * 10 paper_height = gramps_paper_size.get_width() * 10 paper_size = gtk.paper_size_new_custom("custom", "Custom Size", paper_width, paper_height, gtk.UNIT_MM) log.debug("Selected paper size: (%f,%f)" % (paper_width, paper_height)) page_setup = gtk.PageSetup() page_setup.set_paper_size(paper_size) # Set paper orientation if paper_style.get_orientation() == PAPER_PORTRAIT: page_setup.set_orientation(gtk.PAGE_ORIENTATION_PORTRAIT) else: page_setup.set_orientation(gtk.PAGE_ORIENTATION_LANDSCAPE) # Set paper margins page_setup.set_top_margin(paper_style.get_top_margin() * 10, gtk.UNIT_MM) page_setup.set_bottom_margin(paper_style.get_bottom_margin() * 10, gtk.UNIT_MM) page_setup.set_left_margin(paper_style.get_left_margin() * 10, gtk.UNIT_MM) page_setup.set_right_margin(paper_style.get_right_margin() * 10, gtk.UNIT_MM) return page_setup