From b267494e4a38e694178f8f49bba7d0a760a8a847 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Fri, 8 Jan 2021 15:10:43 -0800 Subject: [PATCH] Create raster PDF pages to match input page size Previously we produced a raster image, then multiplied image width by DPI to get the page size. However if there is rounding the page size may not match exactly. In this modified approach we constrain the page size to match. --- src/ocrmypdf/_pipeline.py | 9 +++++++-- tests/test_tesseract.py | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ocrmypdf/_pipeline.py b/src/ocrmypdf/_pipeline.py index ab38a76d..97d5b718 100644 --- a/src/ocrmypdf/_pipeline.py +++ b/src/ocrmypdf/_pipeline.py @@ -588,12 +588,17 @@ def create_pdf_page_from_image(image: Path, page_context: PageContext): # except that the hocr renderer does not understand non-square DPI. The # sandwich renderer would be fine. output_file = page_context.get_path('visible.pdf') - dpi = get_page_square_dpi(page_context.pageinfo, page_context.options) - layout_fun = img2pdf.get_fixed_dpi_layout_fun(dpi) + + pageinfo = page_context.pageinfo + pagesize = 72.0 * float(pageinfo.width_inches), 72.0 * float(pageinfo.height_inches) + if pageinfo.rotation % 180 == 90: + pagesize = pagesize[1], pagesize[0] # This create a single page PDF with open(image, 'rb') as imfile, open(output_file, 'wb') as pdf: log.debug('convert') + + layout_fun = img2pdf.get_layout_fun(pagesize) img2pdf.convert( imfile, with_pdfrw=False, layout_fun=layout_fun, outputstream=pdf ) diff --git a/tests/test_tesseract.py b/tests/test_tesseract.py index 57ece9af..f7331e48 100644 --- a/tests/test_tesseract.py +++ b/tests/test_tesseract.py @@ -45,7 +45,8 @@ def test_skip_pages_does_not_replicate(resources, basename, outdir): assert len(page.images) == 1, "skipped page was replicated" for n, info_out_n in enumerate(info): - assert info_out_n.width_inches == info_in[n].width_inches + assert info_out_n.width_inches == info_in[n].width_inches, "output resized" + assert info_out_n.height_inches == info_in[n].height_inches, "output resized" def test_content_preservation(resources, outpdf):