Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've been playing around with Linux OCR software, and I really like Tesseract, especially in conjunction with gsan2pdf. Tesseract v3 or greater supports outputting in the hocr format, and gscan2pdf is able to make use of that in order to create searchable pdf's of scanned documents.

Sometimes, however, I would also like to get the plain text version as well. pdftotext on the searchable pdf generated by gscan2pdf as described above is not so great for that as even with the -raw option, the layout of the output doesn't copy the original physical layout well. I can set up a user-defined command in gscan2pdf that will call tesseract on the original scanned image without the hocr option so that only plain text is generated, however ocr happens to be quite time-consuming to do it twice for each page. Isn't there a working way to convert from hocr to plain text (with the same layout as the one generated by tesseract when invoked without the hocr option) or a way to make tesseract output both plain text and hocr at the same time?

https://github.com/jbrinley/HocrConverter looks promising, but it doesn't work for me.

share|improve this question

1 Answer 1

<?php 
/**
 * Cli process that gets as 1st argument the output of tesseract ... hocr and dumps 
 * its text nodes
 * Usage: script.php in.tif.html out.txt
 */
$inFile = $argv[1];
$outFile = $argv[2];
$stream = file_get_contents($inFile);
$dom = DOMDocument::loadHTML($stream);
$out = array();
foreach ($dom->getElementsByTagName('p') as $tag) {
    $out[] = $tag->nodeValue;
}

file_put_contents($outFile, implode("\n", $out));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.