Skip to content

page.extract_text(layout=True) incorrectly reproducing text #1380

Description

@acb764se

Open attached .pdf, and look at the end of the page for this line:
Against Celsus (Origen), 327 , 328 *, 329 *

page.extract_text produces two lines instead:

                                         *    *                                   
                  Against Celsus (Origen), 327 , 328 , 329                         

Have you tried repairing the PDF?

Yes:

$ gs -o repaired.pdf -sDEVICE=pdfwrite page826.pdf
GPL Ghostscript 9.55.0 (2021-09-27)
Copyright (C) 2021 Artifex Software, Inc.  All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
   **** Warning: considering '0000000000 XXXXX n' as a free entry.
   **** Warning: considering '0000000000 XXXXX n' as a free entry.
Processing pages 1 through 1.
Page 1

   **** This file had errors that were repaired or ignored.
   **** The file was produced by: 
   **** >>>> Mac OS X 10.13.6 Quartz PDFContext <<<<
   **** Please notify the author of the software that produced this
   **** file that it does not conform to Adobe's published PDF
   **** specification.

Running the script on it, no change

Please try running your code with pdfplumber.open(..., repair=True) before submitting a bug report.
The script already does this

Trying to run thru pdf2txt from pdfminer.six gives this, which looks right, or at least better:

Against Celsus (Origen), 327 , 328 *, 329 * 

Code to reproduce the problem

import pdb
import pdfplumber

if __name__ == '__main__':

    with pdfplumber.open("page826.pdf", repair=True) as pdf:

        page = pdf.pages[0]

        one_string = page.extract_text(layout=True)
        lines = one_string.split('\n')

        print("\nasterisks on the top line should appear after 328, 329; but are shifted left")
        print('\n'.join(lines[53:55]),'\n')

        print("still, coordinates look right:\n")
        words = page.extract_words(return_chars=True)

        print("left '*' starts at x0=297.0, and '328' below ends at 294.0 -- OK")
        print("line 53:",words[236]['text'],words[236]['x0'],words[236]['x1'])
        print("line 54:",words[243]['text'],words[243]['x0'],words[243]['x1'],'\n')

        print("rite '*' starts at x0=330.0, and '329' below ends at 327.0 -- OK")
        print("line 53:",words[237]['text'],words[237]['x0'],words[237]['x1'])
        print("line 54:",words[245]['text'],words[245]['x0'],words[245]['x1'],'\n')

PDF file

page826.pdf, attached

Expected behavior

What did you expect the result should have been?
One line, not two; or, at the least, correct x-positioning of the two asterisks

Actual behavior

They are shifted left, towards lower x0 and x1, and (unless I am wrong) by different amounts

Screenshots

If applicable, add screenshots to help explain your problem.

page826.pdf

Environment

  • pdfplumber version: 0.11.9
  • Python version: 3.10.12
  • Ubuntu 22.04

Additional context

Add any other context/notes about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions