2

我想将 PDF 文件中的页面显示到 GtkIconView 中。为此,我需要在我的 GtkTreeModel 中有一个 GDK_TYPE_PIXBUF 类型的列。如何将我的 PDF 页面呈现为 Pixbuf?

page, a PopplerPage,我试过这个:

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 320, 240)
page.render(surface)
img = Gdk.pixbuf_get_from_surface(surface, 0, 0, 320, 240)

((*&(&cr->ref_count)->ref_count) > 0)但是,当它执行render命令时,我得到一个关于断言的错误。

4

2 回答 2

2

最后,我想通了。我必须渲染到 cairo 上下文,如下所示:

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
ctx = cairo.Context(surface)
page.render(ctx)
img = Gdk.pixbuf_get_from_surface(ctx.get_target(), 0, 0,
        ctx.get_target().get_width(),
        ctx.get_target().get_height())
于 2013-01-29T03:02:33.030 回答
0

试试这个

 # get pdf
 pdf = mkpdf(ds,sample=True)

# render pdf onto pixbuf
pixbuf = Pixbuf(COLORSPACE_RGB,False,8,286,225)
doc = poppler.document_new_from_data(pdf,len(pdf),password='')
page0=doc.get_page(0)
page0.render_to_pixbuf(0,0,8,11,1,0,pixbuf)

# save pixbuf as png
# There has to be a better way to get the image?
lst=[]
pixbuf.save_to_callback(lambda b,l: l.append(b), 'png', user_data=lst)
png=''.join(lst)

return png

参考

于 2013-01-28T19:50:18.807 回答