【问题标题】:Issues Viewing PDF's查看 PDF 的问题
【发布时间】:2011-01-24 01:50:32
【问题描述】:

平台:Linux、GTK+

工具: Python、PyGTK 和 Glade。

问题:

  • 我想编写一个能够显示 PDF 文件的程序。

问题:

  • 我需要哪些小部件和 Python 模块?

感谢您的任何建议!

【问题讨论】:

  • 你想在这里做什么?在屏幕上显示,还是转换成别的东西来显示?
  • 只需将其显示在屏幕上,无需启动现有的 pdf 查看器。

标签: python interface gtk pygtk glade


【解决方案1】:

查看 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上开发,所以如果有的话我还没有使用它。

【讨论】:

  • 你有使用python查看PDF文件的完整代码吗?介意分享吗?
【解决方案2】:

不是 GTK,而是 wxPython:

这个例子展示了一个 PDF 查看器类,它处理像缩放和滚动这样的事情。它需要 python-poppler 和 wxPython >= 2.8.9。

【讨论】:

    【解决方案3】:

    【讨论】:

    • 如果链接失效,我认为谷歌搜索“python ghostscript”不会超出任何人的能力。
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    相关资源
    最近更新 更多