【问题标题】:How to reduce size of modified PDF using pymupdf如何使用 pymupdf 减小修改后 PDF 的大小
【发布时间】:2021-05-27 09:12:02
【问题描述】:

我正在通过编辑某些单词并在 pymupdf 的编辑区域顶部添加不同的单词来编辑 pdf。

代码可以成功运行,但是它会生成一个非常大的单页 pdf (9MB)。我认为这是因为绘制了许多形状和修订,但我似乎无法重构。

我知道from this post 我不应该多次应用page.apply_redactions(),但如果我不这样做,文本将无法正确显示在编辑方块的顶部,或者它会引发ValueError: fill rect must be finite and not empty

非常感谢任何有关重构较小输出 pdf 的帮助。

    doc = fitz.open(self.path) 
    # get pdf background colour
    col = fitz.utils.getColor("py_color")
    # iterating through pages 
    for page in doc: 

        page.wrap_contents()
        # geting the rect boxes which consists the matching regex 
        sensitive = self.get_sensitive_data(page.getText("text") 
                                            .split('\n')) 
        for data in sensitive: 
            areas = page.searchFor(data) 
            for area in areas:
                text_page = page.get_textpage(clip=area)
                text_page = text_page.extractDICT(area)
                # text_page = area
                max_length = fitz.getTextlength(str(max(column, key=len)), fontsize=fontsize)+14
                area = format_border(page, area, data, fontsize, align=align, max_length=max_length)
                area.y1 = add_yrect_line(column, area.y1, area.y1-area.y0)
                col = fitz.utils.getColor("white")
                redaction = page.addRedactAnnot(new_area, fill=col, text=" ") #flags not available
                page.apply_redactions()  # page.apply_redations(images=fitz.PDF_REDACT_IMAGE_NONE) to circumvent transparent image issues
                writer = fitz.TextWriter(page.rect, color=color)
                # align to top of box if align right:
                writer.fill_textbox(new_area, variable, fontsize=fontsize, warn=True, align=align, font=font)
                writer.write_text(page)
                # To show what happened, draw the rectangles, etc.
                shape = page.newShape()
                shape.drawRect(new_area)  # the rect within which we had to stay
                shape.finish(stroke_opacity=0)  # show in red color
                shape.commit()

                shape = page.newShape()
                shape.drawRect(writer.text_rect)  # the generated TextWriter rectangle
                shape.drawCircle(writer.last_point, 2)  # coordinates of end of text
                shape.finish(stroke_opacity=0)  # show with blue color
                shape.commit()
                writer = fitz.TextWriter(area, color=color)

【问题讨论】:

    标签: python pymupdf


    【解决方案1】:

    如果不了解您正在处理的 PDF 页面的更多详细信息,这有点难以分辨。 然而,插入文本或绘图不会增加大量数据。所以我认为应用编辑可能会导致问题: 如果您的页面包含与您的任何编辑矩形重叠的图像,apply_redactions()(不带参数!)将修改重叠的图像部分并将它们空白......这将发生在每个图像及其每个重叠处!结果是每个图像的未压缩新 PNG 版本。 因此,您应该尝试以下方法之一:

    • 请勿触摸任何图像:使用page.apply_redactions(images=fitz.PDF_REDACT_IMAGE_NONE)
    • 删除每张至少有一个重叠的图像(可能不受欢迎):page.apply_redactions(images=fitz.PDF_REDACT_IMAGE_REMOVE)
    • 或者,至少在保存文件时使用garbage=3, deflate=True 以压缩修改后的图像。

    实际上你应该总是在这些类型的操作之后使用垃圾收集和压缩。

    【讨论】:

    • 感谢您的帮助,不触摸图像有帮助 - 我应该更具体地说明我的问题:应用编辑然后在顶部渲染文本的过程是“覆盖文本”的最有效方式吗', 或者,还有更好的方法?我尝试了其他几种组合,但大多数都在新文本之上呈现了矩形(包括在最后应用编辑)。
    • @polymath - 我也邀请您参加我的存储库的“讨论”。您可以指定直接用注释替换文本。没看到你的理由,为什么用text = " "
    猜你喜欢
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    相关资源
    最近更新 更多