Re: GtkSource.PrintCompositor: How to possible printing plain text documents without lost blank lines and indentations?




 
Hi Attila,

You have a print dialog and click on "Print to File" to save the contents to a PDF file and that file is not 
being formatted correctly and isn't the same as what is in the print preview? 

I tried a small test program. It prints well to PDF with the extra lines. Does this program work OK or does 
it cause the same problem.

Eric

#!/usr/bin/env python3

#Needed for cairo context: sudo apt-get install python-gi-cairo
#Tested on Ubuntu16.04 with GTK3.18 and Python3.

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GtkSource', '3.0')
gi.require_version('PangoCairo', '1.0')
from gi.repository import Gtk, GtkSource, PangoCairo, Pango
import math

class TextBox(GtkSource.View):
    def __init__(self, win):
        GtkSource.View.__init__(self)
        self.parent_win = win
        self.page_width = 0
        self.page_height = 0
        self.set_wrap_mode(1)
        self.set_cursor_visible(True)
        self.set_vexpand(True);
        self.set_hexpand(True);
        self.textbuffer = self.get_buffer() 
        self.textbuffer.set_text("  alma\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n                  
             1")

    def print_dialog(self):
        operation = Gtk.PrintOperation()
        #Just print 1 page.
        operation.set_n_pages(1)
        operation.connect("begin_print", self.begin_print)
        operation.connect("draw_page", self.draw_page)
        result = operation.run(Gtk.PrintOperationAction.PRINT_DIALOG, self.parent_win)

    def begin_print(self, operation, gtk_context):
        self.page_width = gtk_context.get_width()
        self.page_height = gtk_context.get_height()
        pango_context = self.get_pango_context()
        description = pango_context.get_font_description()
        self.pango_layout = gtk_context.create_pango_layout()
        self.pango_layout.set_font_description(description)
        self.pango_layout.set_width(int(self.page_width*Pango.SCALE));
        self.pango_layout.set_wrap(Pango.WrapMode.CHAR)

    def draw_page(self, operation, gtk_context, page_number):
        cr = gtk_context.get_cairo_context()

        #Draw a rectangle.
        cr.set_source_rgb(1.0, 0.0, 1.0)
        cr.set_line_width(5)
        cr.rectangle(40, 40, self.page_width-80, self.page_height-500)
        cr.stroke()

        #Show Text.
        cr.set_source_rgb(0.0, 0.0, 0.0)
        start = self.textbuffer.get_start_iter()
        end = self.textbuffer.get_end_iter()
        string = self.textbuffer.get_text(start, end, False)
        self.pango_layout.set_markup(string)
        PangoCairo.show_layout(cr, self.pango_layout)

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_title("Print")
        self.set_default_size(300, 700)
        self.TextBox1 = TextBox(self)
        self.scrolledwindow = Gtk.ScrolledWindow()
        self.scrolledwindow.add(self.TextBox1)
        self.button1 = Gtk.Button("Print Dialog")
        self.button1.connect("clicked", self.print_dialog)
        self.grid = Gtk.Grid()
        self.grid.attach(self.scrolledwindow, 0, 0, 4, 4)
        self.grid.attach(self.button1, 0, 5, 4, 1)
        self.add(self.grid)

    def print_dialog(self, button1):
        self.TextBox1.print_dialog()

win = MainWindow()
win.connect("delete-event", Gtk.main_quit) 
win.show_all()
Gtk.main()



 




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]