在gtk3/python中创建打印作业
我有一些信息(活动参与者名单),我想很容易打印出来。不需要花哨的布局,只需要一个有几列的表格,如果可能的话,在文本行之间画一条线以提高可读性。将需要打印横向以使其完全适合(可通过GtkPageSetup完成) 我使用的是Python,我使用的是Linux,因此必须使用GtkPrintUnixDialog接口。我一直在互联网上搜索,但找不到任何关于如何实现这一目标的例子 为了简化问题:这是我自己使用的,所以已知的纸张大小(A4) 我遇到的问题基本上有两个方面:1)创建一个适合打印的格式正确的文本,2)将其发送到打印机在gtk3/python中创建打印作业,python,printing,gtk3,Python,Printing,Gtk3,我有一些信息(活动参与者名单),我想很容易打印出来。不需要花哨的布局,只需要一个有几列的表格,如果可能的话,在文本行之间画一条线以提高可读性。将需要打印横向以使其完全适合(可通过GtkPageSetup完成) 我使用的是Python,我使用的是Linux,因此必须使用GtkPrintUnixDialog接口。我一直在互联网上搜索,但找不到任何关于如何实现这一目标的例子 为了简化问题:这是我自己使用的,所以已知的纸张大小(A4) 我遇到的问题基本上有两个方面:1)创建一个适合打印的格式正确的文本,
关于从哪里开始有什么建议吗?或者更好,完整的示例?我搜索我以前的打印示例,但首先: 您可以写入pdf surface,打印pdf,或 将绘图代码置于on_打印功能上。请注意,它不会打印您看到的内容,而是打印您在打印表面上绘制的内容。像常规cairo上下文一样绘制上下文,但有几个方法不可用(不适合打印上下文),而其他方法(如newpage)则被添加。如果我找到了这个例子,我会给出一个更加不言自明的答案。 编辑:查找ex:
def on_button_clicked(self, widget):
ps = Gtk.PaperSize.new_custom("cc", "cc", 210, 297, Gtk.Unit.MM)
st = Gtk.PrintSettings()
s = Gtk.PageSetup()
s.set_paper_size(ps)
s.set_bottom_margin(4.3, Gtk.Unit.MM)
s.set_left_margin(4.3, Gtk.Unit.MM)
s.set_right_margin(4.3, Gtk.Unit.MM)
s.set_top_margin(4.3, Gtk.Unit.MM)
s.set_orientation(Gtk.PageOrientation.LANDSCAPE)
# ret = Gtk.print_run_page_setup_dialog(self, s, st)
pd = Gtk.PrintOperation()
pd.set_n_pages(1)
pd.set_default_page_setup(s)
pd.connect("begin_print", self.bg)
pd.connect("draw_page", self.draw_page)
# print(ret, s, st)
pd.set_export_filename("test.pdf")
result = pd.run(Gtk.PrintOperationAction.EXPORT, None) #play with action, but for test export first; if it's ok, then action.PRINT
print (result) # handle errors etc.
# Gtk.PaperSize.free(ps) - not needed in py
以上可能是按钮按下或其他方式
def draw_page (self, operation, context, page_number):
end = self.layout.get_line_count()
cr = context.get_cairo_context()
cr.set_source_rgb(0, 0, 0)
i = 0
start = 0
start_pos = 0
iter = self.layout.get_iter()
while 1:
if i >= start:
line = iter.get_line()
print(line)
_, logical_rect = iter.get_line_extents()
# x_bearing, y_bearing, lwidth, lheight = logical_rect
baseline = iter.get_baseline()
if i == start:
start_pos = 12000 / 1024.0 # 1024.0 is float(pango.SCALE)
cr.move_to(0 / 1024.0, baseline / 1024.0 - start_pos)
PangoCairo.show_layout_line(cr, line)
i += 1
if not (i < end and iter.next_line()):
break
复制/粘贴功能示例:
#!/usr/bin/python
from gi.repository import Gtk, Pango, PangoCairo
import cairo
text = '''
Text.
I have some information (a list of participants to an event) which I
want to print out easily.
No need for fancy layout,
just a table with several columns,
and if possible a drawn line in between the lines of
text for better readability.
'''
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World Printing")
self.button = Gtk.Button(label="Print A Rectangle")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
ps = Gtk.PaperSize.new_custom("cc", "cc", 210, 297, Gtk.Unit.MM)
st = Gtk.PrintSettings()
s = Gtk.PageSetup()
s.set_paper_size(ps)
s.set_bottom_margin(4.3, Gtk.Unit.MM)
s.set_left_margin(4.3, Gtk.Unit.MM)
s.set_right_margin(4.3, Gtk.Unit.MM)
s.set_top_margin(4.3, Gtk.Unit.MM)
s.set_orientation(Gtk.PageOrientation.LANDSCAPE)
# ret = Gtk.print_run_page_setup_dialog(self, s, st)
pd = Gtk.PrintOperation()
pd.set_n_pages(1)
pd.set_default_page_setup(s)
pd.connect("begin_print", self.bg)
pd.connect("draw_page", self.draw_page)
# print(ret, s, st)
pd.set_export_filename("test.pdf")
result = pd.run(Gtk.PrintOperationAction.EXPORT, None)
print (result) # handle errors etc.
# Gtk.PaperSize.free(ps)
def bg(self, op, cx):
w = cx.get_width()
h = cx.get_height()
self.layout = cx.create_pango_layout()
self.layout.set_width(int(w / 4 * Pango.SCALE))
self.layout.set_text(text, len(text))
num_lines = self.layout.get_line_count()
page_height = 0
self.layout.set_font_description(Pango.FontDescription("Georgia Bold 12"))
k = 0
for line in range(num_lines):
if k == 4:
self.layout.set_font_description(Pango.FontDescription("Georgia 10"))
layout_line = self.layout.get_line(line)
ink_rect, logical_rect = layout_line.get_extents()
# print(logical_rect, ink_rect)
# x_bearing, y_bearing, lwidth, lheight = logical_rect
lheight = 1200
line_height = lheight / 1024.0 # 1024.0 is float(pango.SCALE)
page_height += line_height
# page_height is the current location on a page.
# It adds the the line height on each pass through the loop
# Once it is greater then the height supplied by context.get_height
# it marks the line and sets the current page height back to 0
k += 1
print ("page_height ", page_height)
def box(self, w, h, x, y, cx):
w, h = int(w), int(h)
cx.set_font_size(100)
cx.set_source_rgb(0, 0, 0)
cx.rectangle(x, y, w, h)
cx.stroke()
yy = 120
cx.select_font_face("Times", 0, 1)
ex = cx.text_extents("TEGOLA ROMÂNIA SRL")[2]
cx.move_to(w / 2 - ex / 2 + x, 105 + y)
cx.show_text("TEGOLA ROMÂNIA SRL")
ex = cx.text_extents("Str. Plevnei, nr. 5, Buzău")[2]
cx.move_to(w / 2 - ex / 2 + x, 210 + y)
cx.show_text("Str. Plevnei, nr. 5, Buzău")
ex = cx.text_extents("Tel.: 0238/710.280")[2]
cx.move_to(w / 2 - ex / 2 + x, 320 + y)
cx.show_text("Tel.: 0238/710.280")
ex = cx.text_extents("Fax : 0238/710021")[2]
cx.move_to(w / 2 - ex / 2 + x, 415 + y)
cx.show_text("Fax : 0238/710021")
cx.set_font_size(90)
cx.move_to(x + 120, 520 + y)
ex = cx.text_extents("Compoziție:")[2]
cx.show_text("Compoziție:")
cx.select_font_face("Times", 0, 0)
cx.move_to(x + 125 + ex, 520 + y)
cx.show_text("Polimer bituminos, liant și")
cx.move_to(x + 5, 620 + y)
cx.show_text("material de umplutură de înaltă calitate.")
cx.move_to(x + 5, 720 + y)
cx.show_text("Nu conține gudron.")
cx.move_to(x + 5, 800 + y)
cx.select_font_face("Times", 0, 1)
ex = cx.text_extents("Instrucțiuni de utilizare:")[2]
cx.show_text("Instrucțiuni de utilizare:")
cx.select_font_face("Times", 0, 0)
cx.move_to(x + 10 + ex, 800 + y)
cx.show_text("Suprafețele se")
def draw_page1(self, operation, context, page_nr=None):
ctx = context.get_cairo_context()
w = context.get_width()
h = context.get_height()
ww, hh = int(w / 4), int(h / 2)
self.k = 0
for x in range(2):
for y in range(4):
self.box(ww, hh, y * ww, x * hh, ctx)
self.k += 1
print(ctx.get_font_matrix())
def draw_page (self, operation, context, page_number):
end = self.layout.get_line_count()
cr = context.get_cairo_context()
cr.set_source_rgb(0, 0, 0)
i = 0
start = 0
start_pos = 0
iter = self.layout.get_iter()
while 1:
if i >= start:
line = iter.get_line()
print(line)
_, logical_rect = iter.get_line_extents()
# x_bearing, y_bearing, lwidth, lheight = logical_rect
baseline = iter.get_baseline()
if i == start:
start_pos = 12000 / 1024.0 # 1024.0 is float(pango.SCALE)
cr.move_to(0 / 1024.0, baseline / 1024.0 - start_pos)
PangoCairo.show_layout_line(cr, line)
i += 1
if not (i < end and iter.next_line()):
break
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
#/usr/bin/python
从gi.repository导入Gtk、Pango、PangoCairo
进口开罗
文本='''
文本。
我有一些信息(参加活动的人员名单),我
希望打印出来容易。
不需要花哨的布局,
只是一个有几列的表,
如果可能的话,在两条线之间画一条线
文本以提高可读性。
'''
类MyWindow(Gtk.Window):
定义初始化(自):
Gtk.Window.\uuuu init\uuuuu(self,title=“Hello World Printing”)
self.button=Gtk.button(label=“打印矩形”)
self.button.connect(“单击”,self.on_按钮已单击)
self.add(self.button)
单击按钮上的def(自我,小部件):
ps=Gtk.纸张尺寸.新定制(“cc”,“cc”,210297,Gtk.单位.MM)
st=Gtk.PrintSettings()
s=Gtk.PageSetup()
s、 设置纸张大小(ps)
s、 设置底部边缘(4.3,Gtk.单位MM)
s、 设置左边缘(4.3,Gtk.单位为毫米)
s、 设置右边缘(4.3,Gtk.单位为毫米)
s、 设置顶部边缘(4.3,Gtk.单位为毫米)
s、 设置方向(Gtk.PageOrientation.横向)
#ret=Gtk.打印运行页面设置对话框(self、s、st)
pd=Gtk.PrintOperation()
pd.设置页面(1)
pd.设置\默认\页面\设置
pd.connect(“开始打印”,self.bg)
pd.connect(“绘图页”,自绘图页)
#打印(ret、s、st)
pd.set\u export\u文件名(“test.pdf”)
结果=pd.run(Gtk.PrintOperationAction.EXPORT,无)
打印(结果)#处理错误等。
#Gtk.免费纸张尺寸(ps)
def bg(自身、op、cx):
w=cx.get_width()
h=cx.get_height()
self.layout=cx.create\u pango\u layout()
自布局设置宽度(整数(带4*Pango比例))
self.layout.set_text(text,len(text))
num\u lines=self.layout.get\u line\u count()
页面高度=0
self.layout.set_font_description(Pango.fontdription(“Georgia Bold 12”))
k=0
对于范围内的行(num_行):
如果k==4:
self.layout.set_font_description(Pango.fontsdescription(“格鲁吉亚10”))
layout\u line=self.layout.get\u line(行)
ink\u rect,logical\u rect=layout\u line.get\u extents()
#打印(逻辑、墨水)
#x_轴承,y_轴承,L宽度,L高度=逻辑矩形
高度=1200
线高度=lheight/1024.0#1024.0为浮动(盘古刻度)
页高+=行高
#页面高度是页面上的当前位置。
#它会在每次通过循环时添加线高度
#一旦大于context.get\u height提供的高度
#它标记行并将当前页面高度设置回0
k+=1
打印(“页高”,页高)
def箱(自身、w、h、x、y、cx):
w、 h=整数(w),整数(h)
cx.设置字体大小(100)
cx.set\u source\u rgb(0,0,0)
cx.矩形(x,y,w,h)
cx.stroke()
yy=120
cx.选择字体面(“时间”,0,1)
ex=cx.text_区段(“TEGOLA ROM NIA SRL”)[2]
cx.移动到(w/2-ex/2+x,105+y)
cx.显示文本(“TEGOLA ROM NIA SRL”)
ex=cx.textău区段(“Str.Plevnei,编号5,Buzău”)[2]
cx.移动到(w/2-ex/2+x,210+y)
cx.显示文字(“Plevnei街,5号,Buzău”)
ex=cx.text_区段(“电话:0238/710.280”)[2]
cx.移动到(w/2-ex/2+x,320+y)
cx.显示文字(“电话:0238/710.280”)
ex=cx.text_区段(“传真:0238/710021”)[2]
cx.移动到(w/2-ex/2+x,415+y)
cx.显示文本(“传真:0238/710021”)
cx.设置字体大小(90)
cx.移动到(x+120520+y)
ex=cx.text_区段(“Compoziție:”)[2]
cx.显示文字(“组合文字:”)
cx.选择字体面(“时间”,0,0)
cx.移动到(x+125+ex,520+y)
cx.显示文字(“Polimer bituminos,liantși”)
cx.移动到(x+5620+y)
cx.显示文字(“校准材料”)
cx.移动到(x+5720+y)
cx.显示文字(“Nu conține gudron.”)
cx.移动到(x+5800+y)
cx.选择字体面(“时间”,0,1)
ex=cx.text_区段(“实用说明:”)[2]
cx.显示文字(“使用说明:”)
cx.选择字体面(“时间”,0,0)
cx.移动到(x+10+ex,800+y)
cx.显示文字(“上文”)
def draw_page1(自身、操作、上下文、页面编号=无):
ctx=context.get\u cairo\u context()
w=上下文。获取宽度()
h=context.g
#!/usr/bin/python
from gi.repository import Gtk, Pango, PangoCairo
import cairo
text = '''
Text.
I have some information (a list of participants to an event) which I
want to print out easily.
No need for fancy layout,
just a table with several columns,
and if possible a drawn line in between the lines of
text for better readability.
'''
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World Printing")
self.button = Gtk.Button(label="Print A Rectangle")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
ps = Gtk.PaperSize.new_custom("cc", "cc", 210, 297, Gtk.Unit.MM)
st = Gtk.PrintSettings()
s = Gtk.PageSetup()
s.set_paper_size(ps)
s.set_bottom_margin(4.3, Gtk.Unit.MM)
s.set_left_margin(4.3, Gtk.Unit.MM)
s.set_right_margin(4.3, Gtk.Unit.MM)
s.set_top_margin(4.3, Gtk.Unit.MM)
s.set_orientation(Gtk.PageOrientation.LANDSCAPE)
# ret = Gtk.print_run_page_setup_dialog(self, s, st)
pd = Gtk.PrintOperation()
pd.set_n_pages(1)
pd.set_default_page_setup(s)
pd.connect("begin_print", self.bg)
pd.connect("draw_page", self.draw_page)
# print(ret, s, st)
pd.set_export_filename("test.pdf")
result = pd.run(Gtk.PrintOperationAction.EXPORT, None)
print (result) # handle errors etc.
# Gtk.PaperSize.free(ps)
def bg(self, op, cx):
w = cx.get_width()
h = cx.get_height()
self.layout = cx.create_pango_layout()
self.layout.set_width(int(w / 4 * Pango.SCALE))
self.layout.set_text(text, len(text))
num_lines = self.layout.get_line_count()
page_height = 0
self.layout.set_font_description(Pango.FontDescription("Georgia Bold 12"))
k = 0
for line in range(num_lines):
if k == 4:
self.layout.set_font_description(Pango.FontDescription("Georgia 10"))
layout_line = self.layout.get_line(line)
ink_rect, logical_rect = layout_line.get_extents()
# print(logical_rect, ink_rect)
# x_bearing, y_bearing, lwidth, lheight = logical_rect
lheight = 1200
line_height = lheight / 1024.0 # 1024.0 is float(pango.SCALE)
page_height += line_height
# page_height is the current location on a page.
# It adds the the line height on each pass through the loop
# Once it is greater then the height supplied by context.get_height
# it marks the line and sets the current page height back to 0
k += 1
print ("page_height ", page_height)
def box(self, w, h, x, y, cx):
w, h = int(w), int(h)
cx.set_font_size(100)
cx.set_source_rgb(0, 0, 0)
cx.rectangle(x, y, w, h)
cx.stroke()
yy = 120
cx.select_font_face("Times", 0, 1)
ex = cx.text_extents("TEGOLA ROMÂNIA SRL")[2]
cx.move_to(w / 2 - ex / 2 + x, 105 + y)
cx.show_text("TEGOLA ROMÂNIA SRL")
ex = cx.text_extents("Str. Plevnei, nr. 5, Buzău")[2]
cx.move_to(w / 2 - ex / 2 + x, 210 + y)
cx.show_text("Str. Plevnei, nr. 5, Buzău")
ex = cx.text_extents("Tel.: 0238/710.280")[2]
cx.move_to(w / 2 - ex / 2 + x, 320 + y)
cx.show_text("Tel.: 0238/710.280")
ex = cx.text_extents("Fax : 0238/710021")[2]
cx.move_to(w / 2 - ex / 2 + x, 415 + y)
cx.show_text("Fax : 0238/710021")
cx.set_font_size(90)
cx.move_to(x + 120, 520 + y)
ex = cx.text_extents("Compoziție:")[2]
cx.show_text("Compoziție:")
cx.select_font_face("Times", 0, 0)
cx.move_to(x + 125 + ex, 520 + y)
cx.show_text("Polimer bituminos, liant și")
cx.move_to(x + 5, 620 + y)
cx.show_text("material de umplutură de înaltă calitate.")
cx.move_to(x + 5, 720 + y)
cx.show_text("Nu conține gudron.")
cx.move_to(x + 5, 800 + y)
cx.select_font_face("Times", 0, 1)
ex = cx.text_extents("Instrucțiuni de utilizare:")[2]
cx.show_text("Instrucțiuni de utilizare:")
cx.select_font_face("Times", 0, 0)
cx.move_to(x + 10 + ex, 800 + y)
cx.show_text("Suprafețele se")
def draw_page1(self, operation, context, page_nr=None):
ctx = context.get_cairo_context()
w = context.get_width()
h = context.get_height()
ww, hh = int(w / 4), int(h / 2)
self.k = 0
for x in range(2):
for y in range(4):
self.box(ww, hh, y * ww, x * hh, ctx)
self.k += 1
print(ctx.get_font_matrix())
def draw_page (self, operation, context, page_number):
end = self.layout.get_line_count()
cr = context.get_cairo_context()
cr.set_source_rgb(0, 0, 0)
i = 0
start = 0
start_pos = 0
iter = self.layout.get_iter()
while 1:
if i >= start:
line = iter.get_line()
print(line)
_, logical_rect = iter.get_line_extents()
# x_bearing, y_bearing, lwidth, lheight = logical_rect
baseline = iter.get_baseline()
if i == start:
start_pos = 12000 / 1024.0 # 1024.0 is float(pango.SCALE)
cr.move_to(0 / 1024.0, baseline / 1024.0 - start_pos)
PangoCairo.show_layout_line(cr, line)
i += 1
if not (i < end and iter.next_line()):
break
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()