<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>x + 3 &#187; PDF</title>
	<atom:link href="http://xplus3.net/tag/pdf/feed/" rel="self" type="application/rss+xml" />
	<link>http://xplus3.net</link>
	<description></description>
	<lastBuildDate>Fri, 19 Aug 2011 01:05:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Convert hOCR to PDF</title>
		<link>http://xplus3.net/2009/04/02/convert-hocr-to-pdf/</link>
		<comments>http://xplus3.net/2009/04/02/convert-hocr-to-pdf/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 20:05:46 +0000</pubDate>
		<dc:creator>Jonathan Brinley</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[hOCR]]></category>
		<category><![CDATA[HocrConverter]]></category>
		<category><![CDATA[OCR]]></category>
		<category><![CDATA[OCRopus]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://xplus3.net/?p=207</guid>
		<description><![CDATA[As I mentioned recently, OCRopus OCR software output an hOCR file. What is hOCR? hOCR is an open standard for representing OCR results in an HTML document (not to be confused with HOCR). It is basically a microformat using div and span tags&#8217; class and title attributes to convey information from the OCR process, as you can see from this &#8230; <a href="http://xplus3.net/2009/04/02/convert-hocr-to-pdf/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As <a href="http://xplus3.net/2009/03/31/ocr-with-ocropus-and-tesseract/">I mentioned recently</a>, OCRopus OCR software output an hOCR file. What is hOCR? hOCR is an open standard for representing OCR results in an HTML document (not to be confused with <a href="http://en.wikipedia.org/wiki/HOCR_(software)"><abbr title="Hebrew Optical Character Recoginition">HOCR</abbr></a>). It is basically a microformat using <code>div</code> and <code>span</code> tags&#8217; <code>class</code> and <code>title</code> attributes to convey information from the OCR process, as you can see from this example of a basic hOCR document:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="xml"><pre class="de1"><span class="sc0">&lt;!DOCTYPE html</span>
<span class="sc0">  PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
<span class="sc0">  &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span class="sc3"><span class="re1">&lt;html</span> <span class="re0">xmlns</span>=<span class="st0">&quot;http://www.w3.org/1999/xhtml&quot;</span><span class="re2">&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;head<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;meta</span> <span class="re0">content</span>=<span class="st0">&quot;ocr_line ocr_page&quot;</span> <span class="re0">name</span>=<span class="st0">&quot;ocr-capabilities&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;meta</span> <span class="re0">content</span>=<span class="st0">&quot;en&quot;</span> <span class="re0">name</span>=<span class="st0">&quot;ocr-langs&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;meta</span> <span class="re0">content</span>=<span class="st0">&quot;Latn&quot;</span> <span class="re0">name</span>=<span class="st0">&quot;ocr-scripts&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;meta</span> <span class="re0">content</span>=<span class="st0">&quot;&quot;</span> <span class="re0">name</span>=<span class="st0">&quot;ocr-microformats&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;title<span class="re2">&gt;</span></span></span>OCR Output<span class="sc3"><span class="re1">&lt;/title<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/head<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;body<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;div</span> <span class="re0">class</span>=<span class="st0">&quot;ocr_page&quot;</span> <span class="re0">title</span>=<span class="st0">&quot;bbox 0 0 2548 3300; image /path/to/scanned/image.png&quot;</span><span class="re2">&gt;</span></span>
      <span class="sc3"><span class="re1">&lt;span</span> <span class="re0">class</span>=<span class="st0">&quot;ocr_line&quot;</span> <span class="re0">title</span>=<span class="st0">&quot;bbox 659 143 863 177&quot;</span><span class="re2">&gt;</span></span>Some Text<span class="sc3"><span class="re1">&lt;/span<span class="re2">&gt;</span></span></span>
      <span class="sc3"><span class="re1">&lt;span</span> <span class="re0">class</span>=<span class="st0">&quot;ocr_line&quot;</span> <span class="re0">title</span>=<span class="st0">&quot;bbox 723 275 916 324&quot;</span><span class="re2">&gt;</span></span>More Text<span class="sc3"><span class="re1">&lt;/span<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;/div<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/body<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/html<span class="re2">&gt;</span></span></span></pre></div></div></div></div></div></div></div>


<p>For the more details, see <a href="http://docs.google.com/View?docid=dfxcv4vc_67g844kf">Thomas Breuel&#8217;s complete hOCR specification draft</a>. The example, though, shows all that I need to know. From the <code>div[@class='ocr_page']</code>, I can learn the dimensions (in pixels) of the image that was OCRed (as well as the path to that image on my machine). From a <code>span[@class='ocr_line']</code>, I can learn the location and dimensions of the bounding box around a line of recognized text, as well as the content of that line.</p>
<p>That information, along with a copy of the original image (or a sufficiently similar image) is enough to create a PDF of the image with selectable text.<br />
<span id="more-207"></span></p>
<h3>Creating a PDF</h3>
<p>A while back, Florian Hackenberger  created a <a href="http://groups.google.com/group/ocropus/browse_thread/thread/3cf464bda5807952">basic hOCR to PDF converter in Java</a>. It does its job reasonably well, but it&#8217;s somewhat rough around the edges. Since I&#8217;m more familiar with Python than Java, it seemed like a good idea to rewrite the code in Python, so I could more comfortably hack it.</p>
<p>And so I present to you, dear reader, the <a href="http://github.com/jbrinley/HocrConverter">hOCR Converter Python script</a> (note: this script depends on the <a href="http://www.reportlab.org/downloads.html#reportlab">ReportLab PDF Library</a>, which, in turn, depends on the <a href="http://www.freetype.org/">FreeType 2 Font Engine</a>). Right now, it has two primary functions:</p>
<ol>
<li>Given an hOCR file, create a text-only (<em>i.e.</em>, no HTML) document</li>
<li>Given an hOCR file and an image, create a PDF</li>
</ol>
<p>Usage is pretty simple:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="python"><pre class="de1"><span class="kw1">from</span> HocrConverter <span class="kw1">import</span> HocrConverter
hocr <span class="sy0">=</span> HocrConverter<span class="br0">&#40;</span><span class="st0">&quot;myHocrFile.html&quot;</span><span class="br0">&#41;</span>
hocr.<span class="me1">to_text</span><span class="br0">&#40;</span><span class="st0">&quot;output.txt&quot;</span><span class="br0">&#41;</span>
hocr.<span class="me1">to_pdf</span><span class="br0">&#40;</span><span class="st0">&quot;myImageFile.png&quot;</span><span class="sy0">,</span> <span class="st0">&quot;output.pdf&quot;</span><span class="br0">&#41;</span></pre></div></div></div></div></div></div></div>


<p>You end up with a text file at <code>output.txt</code> containing the contents of the <code>body</code> of the hOCR document and a PDF file at <code>output.pdf</code> containing your image superimposed on top of the text.</p>
<p>Note that the image you use to create the PDF need not be the same image used for the OCR. If, for example, you used a 300 or 400 DPI image for the OCR, but you want a smaller file for the PDF, you can create a 72 DPI version of the image and feed that through the script instead of the original.</p>
<p>With a simple script that iterates through a directory of images, calling OCRopus and this script for each image (maybe with some ImageMagick and <a href="http://xplus3.net/2008/10/09/command-line-pdf-editing/">pdftk</a> thrown in for good measure), you can quickly OCR a large batch of images and convert them to searchable PDF documents.</p>
<h3>Future Development</h3>
<p>I&#8217;d like to add the capability to convert the output of various OCR programs into hOCR format. For example, OmniPage, which is much better than OCRopus when it comes to layout analysis for complex images, can output documents in its proprietary XML schema. I should be able to transform that into an hOCR document and then use this script to create a PDF from there.</p>
<p>I would welcome any other suggestions or contributions.</p>
<p><strong><a href="http://github.com/jbrinley/HocrConverter">Download the HocrConverter script from github</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://xplus3.net/2009/04/02/convert-hocr-to-pdf/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Command Line PDF Editing</title>
		<link>http://xplus3.net/2008/10/09/command-line-pdf-editing/</link>
		<comments>http://xplus3.net/2008/10/09/command-line-pdf-editing/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 14:17:35 +0000</pubDate>
		<dc:creator>Jonathan Brinley</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Acrobat]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[pdftk]]></category>

		<guid isPermaLink="false">http://xplus3.net/?p=83</guid>
		<description><![CDATA[As I&#8217;ve mentioned before, Acrobat&#8217;s JavaScript API lags far behind other Adobe applications. Its limitations turned a seemingly simple project I was working on into an exercise in futility. Overview I have a collection of a little over 5,000 PDF files, the output of an OCR job. Each file contains one page of a newspaper. Four pages put together would &#8230; <a href="http://xplus3.net/2008/10/09/command-line-pdf-editing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As <a href="http://xplus3.net/2007/08/27/fun-with-acrobat/">I&#8217;ve mentioned before</a>, Acrobat&#8217;s JavaScript API lags far behind other Adobe applications. Its limitations turned a seemingly simple project I was working on into an exercise in futility.</p>
<h3>Overview</h3>
<p>I have a collection of a little over 5,000 PDF files, the output of an OCR job. Each file contains one page of a newspaper. Four pages put together would make one issue. My goal, then, is to take four PDF files (<em>e.g.</em>, <code>1896-09-24_001.pdf</code>, <code>1896-09-24_002.pdf</code>, <code>1896-09-24_003.pdf</code>, and <code>1896-09-24_004.pdf</code>) and merge them together into one file (<code>1896-09-24.pdf</code>). Then repeat 1,300 times or so to get the rest of the issues.</p>
<p>Sounds like an ideal job for a small script. Unfortunately, Acrobat only gives JavaScript access to the file system for opening and saving files. It has no way to read a directory for a list of files, which is rather fundamental to the task at hand.</p>
<h3>PyPdf Almost Works</h3>
<p>At <a href="http://www.f00die.com/">Jay Luker</a>&#8216;s suggestion, I tried out <a href="http://pybrary.net/pyPdf/">PyPdf</a>. It seems to do everything I needed. And indeed it would, except it can&#8217;t read my PDF files. It does its job just fine with other files, but not these that OmniPage created. It seems the files are missing an attribute that PyPdf looks for, so I end up with a KeyError.</p>
<h3>pdftk to the Rescue</h3>
<p>So, after much gnashing of teeth, <a href="http://ronallo.wordpress.com/">Jason Ronallo</a> suggested I try <a href="http://www.accesspdf.com/pdftk/">pdftk</a>, a simple command line tool that can merge and split PDFs, among other capabilities. To merge the issue noted above takes just one line:</p>
<pre>
pdftk 1896-09-24_001.pdf 1896-09-24_002.pdf 1896-09-24_003.pdf 1896-09-24_004.pdf cat output 1896-09-24.pdf
&nbsp;
</pre>
<p>A simple Python script can just call pdftk repeatedly to take care of the whole collection.</p>
<p>Many thanks, pdftk and <a href="http://code4lib.org/irc">#code4lib</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://xplus3.net/2008/10/09/command-line-pdf-editing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

