weave: if we don't have textonly_pdf, delete instruction to draw image

This commit is contained in:
James R. Barlow
2018-05-17 16:49:20 -07:00
parent f83ca5d8ac
commit 4b98e9ff08
+11
View File
@@ -21,6 +21,7 @@ from itertools import groupby
import pikepdf
from .helpers import flatten_groups, page_number
from .exec import tesseract
def _update_page_resources(*, page, font, font_key, procset):
@@ -54,6 +55,16 @@ def _weave_layers_graft(
pdf_text = pikepdf.open(text)
pdf_text_contents = pdf_text.pages[0].Contents.read_bytes()
if not tesseract.has_textonly_pdf():
# If we don't have textonly_pdf, edit the stream to delete the
# instruction to draw the image Tesseract generated, which we do not
# use.
stream = bytearray(pdf_text_contents)
pattern = b'/Im1 Do'
idx = stream.find(pattern)
stream[idx:(idx + len(pattern))] = b' ' * len(pattern)
pdf_text_contents = bytes(stream)
base_page = pdf_base.pages.p(page_num)
# The text page always will be oriented up by this stage but the original