查看PDF的问题

2 投票
3 回答
2453 浏览
提问于 2025-04-16 10:29

平台:Linux,GTK+

工具:Python,PyGTK和Glade。

问题:

  • 我想写一个程序,可以显示PDF文件。

提问:

  • 我需要哪些控件和Python模块呢?

谢谢大家的建议!

3 个回答

-1

这是一个链接,指向一个叫做Ghostscript的东西的页面。Ghostscript是一个用于处理和转换PDF文件和其他图形文件的工具。你可以在这个链接里找到更多关于Ghostscript的信息,比如怎么安装和使用它。

0

不是GTK,而是wxPython:

这个例子展示了一个PDF查看器类,它可以处理缩放和滚动等功能。使用这个类需要安装python-poppler和wxPython版本大于等于2.8.9。

2

可以看看Python的poppler绑定。

我用一种简单粗暴的方法来渲染PDF文件。我复制了在Python poppler gtk绑定示例中使用的方法。

def load_pdf(self):
    self.doc = poppler.document_new_from_file (uri, None)
    # the number of pages in the pdf
    self.n_pgs = self.document.get_n_pgs()
    # the current page of the pdf
    self.curr_pg = 0
    # the current page being displayed
    self.curr_pg_disp = self.document.get_page(self.curr_pg)
    # the scale of the page
    self.scale = 1
    # the document width and height
    self.doc_width, self.doc_height = self.curr_pg_disp.get_size()


def render_pdf(self):
    cr = self.pdfda.window.cairo_create()
    cr.set_source_rgb(1, 1, 1)
    if self.scale != 1:
        cr.scale(self.scale, self.scale)
    cr.rectangle(0, 0, self.doc_width, self.doc_height)
    cr.fill()
    self.curr_pg_disp.render(cr)

def on_next_btn_clicked(self, widget, data=None):
    if self.curr_pg < self.n_pgs:
        self.curr_pg = self.curr_pg + 1
        self.curr_pg_disp = self.doc.get_page(self.curr_pg)
        self.render_page()

def on_prev_btn_clicked(self, widget, data=None):
    if self.curr_pg > 0:
        self.curr_pg = self.curr_pg - 1
        self.curr_pg_disp = self.doc.get_page(self.curr_pg)
        self.render_page()

虽然这不是最好的或最漂亮的方式,但它确实能工作。我还需要添加一些功能,比如如何让它可以滚动,或者在绘图区域中居中之类的,但这算是一个开始。

你也可以看看evince的Python绑定,我相信他们有一个小部件可以让PDF渲染变得更简单。我是在Windows上开发的,所以如果有的话我还没用过。

撰写回答