查看 PDF 时出现问题

发布于 2024-10-14 08:23:39 字数 241 浏览 8 评论 0原文

平台: Linux、GTK+

工具: Python、PyGTK 和 Glade。

问题:

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

问题:

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

感谢您的任何建议!

Platform: Linux, GTK+

Tools: Python, PyGTK and Glade.

Problem:

  • I'd like to write a program that is capable of displaying a PDF file.

Question:

  • Which widget(s) and Python module(s) do I need?

Thanks for any suggestions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦等七年七年为一梦 2024-10-21 08:23:39

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

look into the python poppler bindings.

I render pdf files in a simple dirty way. I copied the method used in the example for the python poppler gtk bindings

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

Its not the best or prettiest but it works. I still have to add how to make it scrollable or center in the drawing area and stuff like that but there is a start.

you could also look into the evince python bindings, I do believe they have a widget you can use for to make pdf rendering easier. I'm on developing on windows so I haven't used it if there is one.

洋洋洒洒 2024-10-21 08:23:39

不是 GTK,而是 wxPython:

此示例显示了一个 PDF 查看器类,它处理缩放和滚动等操作。它需要 python-poppler 和 wxPython >= 2.8.9。

Not GTK but wxPython:

This example shows a PDF Viewer class, which handles things like Zoom and Scrolling. It requires python-poppler and wxPython >= 2.8.9.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文