diff --git a/ocrmypdf/__main__.py b/ocrmypdf/__main__.py index 16b7f662..6af7af48 100755 --- a/ocrmypdf/__main__.py +++ b/ocrmypdf/__main__.py @@ -304,6 +304,9 @@ advanced.add_argument( advanced.add_argument( '--user-patterns', metavar='FILE', help="Specify the location of the Tesseract user patterns file.") +advanced.add_argument( + '--interword-spaces', action='store_true', + help="Add spaces between words with HOCR transformation.") debugging = parser.add_argument_group( "Debugging", @@ -463,7 +466,11 @@ def check_options_advanced(options, log): "--pdfa-image-compression argument has no effect when " "--output-type is not 'pdfa', 'pdfa-1', or 'pdfa-2'" ) - + if options.interword_spaces and options.pdf_renderer != 'hocr': + log.warning( + "--interword-spaces argument has no effect when " + "--pdf-renderer is not 'hocr'" + ) def check_options_metadata(options, log): import unicodedata diff --git a/ocrmypdf/hocrtransform.py b/ocrmypdf/hocrtransform.py index 75faf8d3..244eec0e 100755 --- a/ocrmypdf/hocrtransform.py +++ b/ocrmypdf/hocrtransform.py @@ -137,7 +137,7 @@ class HocrTransform(): return s def to_pdf(self, outFileName, imageFileName=None, showBoundingboxes=False, - fontname="Helvetica", invisibleText=False): + fontname="Helvetica", invisibleText=False, interwordSpaces=False): """ Creates a PDF file with an image superimposed on top of the text. Text is positioned according to the bounding box of the lines in @@ -180,7 +180,7 @@ class HocrTransform(): ".//%sspan[@class='ocrx_word']" % (self.xmlns)) is not None: elemclass = "ocrx_word" - # itterate all text elements + # iterate all text elements # light green for bounding box of word/line pdf.setStrokeColorRGB(1, 0, 0) pdf.setLineWidth(0.5) # bounding box line width @@ -196,6 +196,12 @@ class HocrTransform(): if len(elemtxt) == 0: continue + # if the advanced option `--interword-spaces` is true, append a space + # to the end of each text element to allow simpler PDF viewers such + # as PDF.js to better recognize words in search and copy and paste + if interwordSpaces: + elemtxt += ' ' + pxl_coords = self.element_coordinates(elem) pt = self.pt_from_pixel(pxl_coords) @@ -242,10 +248,12 @@ if __name__ == "__main__": help='Resolution of the image that was OCRed') parser.add_argument('-i', '--image', default=None, help='Path to the image to be placed above the text') + parser.add_argument('--interword-spaces', action='store_true', + default=False, help='Add spaces between words') parser.add_argument('hocrfile', help='Path to the hocr file to be parsed') parser.add_argument( 'outputfile', help='Path to the PDF file to be generated') args = parser.parse_args() hocr = HocrTransform(args.hocrfile, args.resolution) - hocr.to_pdf(args.outputfile, args.image, args.boundingboxes) + hocr.to_pdf(args.outputfile, args.image, args.boundingboxes, interwordSpaces=args.interword_spaces) diff --git a/ocrmypdf/pipeline.py b/ocrmypdf/pipeline.py index 79f129f5..35326b88 100644 --- a/ocrmypdf/pipeline.py +++ b/ocrmypdf/pipeline.py @@ -639,8 +639,8 @@ def render_hocr_page( hocrtransform = HocrTransform(hocr, dpi) hocrtransform.to_pdf(output_file, imageFileName=None, - showBoundingboxes=False, invisibleText=True) - + showBoundingboxes=False, invisibleText=True, + interwordSpaces=options.interword_spaces) def flatten_groups(groups): for obj in groups: @@ -664,8 +664,8 @@ def render_hocr_debug_page( hocrtransform = HocrTransform(hocr, dpi) hocrtransform.to_pdf(output_file, imageFileName=None, - showBoundingboxes=True, invisibleText=False) - + showBoundingboxes=True, invisibleText=False, + interwordSpaces=options.interword_spaces) def combine_layers( infiles,