I want to display pages from a PDF file into a GtkIconView. To achieve that, I need a column of type GDK_TYPE_PIXBUF in my GtkTreeModel. How can I render my PDF page into a Pixbuf?

Having page, a PopplerPage, I tried this:

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

However, I get an error about assertion ((*&(&cr->ref_count)->ref_count) > 0) when it executes the render command.

有帮助吗?

解决方案

Finally, I figured out. I have to render to a cairo Context, like this:

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())

其他提示

try this

 # 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

Reference

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top