有没有人能够在python中使用poppler new_from_data? [英] Has anyone been able to use poppler new_from_data in python?
问题描述
使用Python3和Poppler,我可以毫无问题地使用new_from_file加载文件,但是new_from_data有问题.这里的代码显然是一个简单的测试,因为先读取文件然后再使用new_from_data没有意义,因为new_from_file可以正常工作,但是我无法在此处发布生成pdf文件的完整代码.
Using Python3, and Poppler, I can load files with new_from_file without problem, but new_from_data is problematic. Here is the code which is obviously a simple test, because it does not make sense to read from file and then use new_from_data, since new_from_file works perfectly, but I could not post here the full code generating the pdf file.
from gi.repository import Poppler, Gtk
def draw(widget, cr):
# set background.
cr.set_source_rgb(0.7, 0.6, 0.5)
cr.paint()
# set page background
cr.set_source_rgb(1, 1, 1)
cr.rectangle(0,0,800,400)
cr.fill()
page.render(cr)
filepath = "d:/Mes Documents/A5.pdf"
f11 = open(filepath, "r", encoding = "cp850")
data1 = f11.read()
f11.close()
document = Poppler.Document.new_from_data(data1, len(data1), None)
page = document.get_page(0)
print (document.get_n_pages())
window = Gtk.Window(title="Hello World")
window.connect("delete-event", Gtk.main_quit)
window.connect("draw", draw)
window.set_app_paintable(True)
window.show_all()
Gtk.main()
可能会发生四种不同的情况:
Four different situations may happen :
- 使用非常简单的pdf(Pdf参考13中的"Hello world"示例),它可以工作.
- 对于普通文件,可能没有错误,但是get_n_pages返回0,而get_page(0)返回None
- 或者我可能会收到错误消息:GLib.Error:poppler-quark:PDF文档已损坏(4)
- 否则程序崩溃
我想知道问题是否出在编码参数上,但是我尝试了所有我认为没有结果的事情. 我尝试使用"rb",然后使用:
I wonder if the problem may be with the encoding parameter, but I tried everything I thought of without result. I tried with "rb" and then converting bytes array to string with :
data1 = "".join(map(data1))
没有结果.
在Google上进行搜索从未返回有效的示例
Search on Google never returned a working example
推荐答案
我遇到了同样的问题,使用Gio.MemoryInputStream解决了该问题. 并不是很优雅,但是可以用...
I ran into the same problem, solved it using Gio.MemoryInputStream. Not really elegant but it works...
from gi.repository import Poppler, Gtk, Gio
def draw(widget, cr):
# set background.
cr.set_source_rgb(0.7, 0.6, 0.5)
cr.paint()
# set page background
cr.set_source_rgb(1, 1, 1)
cr.rectangle(0,0,800,400)
cr.fill()
page.render(cr)
filepath = "d:/Mes Documents/A5.pdf"
with open(filepath, "rb") as f11:
input_stream = Gio.MemoryInputStream.new_from_data(f11.read())
# Take care that you need to call .close() on the Gio.MemoryInputStream once you're done with your pdf document.
document = Poppler.Document.new_from_stream(input_stream, -1, None, None)
page = document.get_page(0)
print (document.get_n_pages())
window = Gtk.Window(title="Hello World")
window.connect("delete-event", Gtk.main_quit)
window.connect("draw", draw)
window.set_app_paintable(True)
window.show_all()
Gtk.main()
这篇关于有没有人能够在python中使用poppler new_from_data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!