Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, with Ghostscript, you <em>can</em> extract text from PDFs. But no, it is not the best tool for the job. And no, you cannot do it in "portions" (parts of single pages). What you can do: extract the text of a certain range of pages only.</p> <h2>First: <a href="http://www.ghostscript.com/releases/" rel="noreferrer">Ghostscript's</a> <code>txtwrite</code> output device (not so good)</h2> <pre><code> gs \ -dBATCH \ -dNOPAUSE \ -sDEVICE=txtwrite \ -dFirstPage=3 \ -dLastPage=5 \ -sOutputFile=- \ /path/to/your/pdf </code></pre> <p>This will output all text contained on pages 3-5 to stdout. If you want output to a text file, use <code>-sOutputFile=textfilename.txt</code>.</p> <hr> <p><strong><code>gs</code> Update:</strong></p> <p>Recent versions of Ghostscript have seen major improvements in the <code>txtwrite</code> device and bug fixes. See <a href="http://git.ghostscript.com/?p=ghostpdl.git;a=blob_plain;f=gs/doc/History9.htm;hb=HEAD" rel="noreferrer">recent Ghostscript changelogs</a> (search for <em>txtwrite</em> on that page) for details.</p> <hr> <h2>Second: Ghostscript's <a href="http://git.ghostscript.com/?p=ghostpdl.git;a=blob_plain;f=gs/lib/ps2ascii.ps;hb=HEAD" rel="noreferrer"><code>ps2ascii.ps</code> PostScript utility</a> (better)</h2> <p>This one requires you to download the latest version of the file <em>ps2ascii.ps</em> from the <a href="http://git.ghostscript.com/?p=ghostpdl.git;a=tree;f=gs/lib" rel="noreferrer">Ghostscript Git source code repository</a>. You'd have to convert your PDF to PostScript, then run this command on the PS file:</p> <pre><code>gs \ -q \ -dNODISPLAY \ -P- \ -dSAFER \ -dDELAYBIND \ -dWRITESYSTEMDICT \ -dSIMPLE \ /path/to/ps2ascii.ps \ input.ps \ -c quit </code></pre> <p>If the <code>-dSIMPLE</code> parameter is not defined, each output line contains some additional info beyond the pure text content about fonts and fontsize used. </p> <p>If you replace that parameter by <code>-dCOMPLEX</code>, you'll get additional infos about colors and images used.</p> <p>Read the comments inside the <em>ps2ascii.ps</em> to learn more about this utility. It's not comfortable to use, but for me it worked in most cases I needed it....</p> <h2>Third: <a href="http://www.foolabs.com/xpdf/download.html" rel="noreferrer">XPDF's</a> <code>pdftotext</code> CLI utility (more comfortable than Ghostscript)</h2> <p>A more comfortable way to do text extraction: use <code>pdftotext</code> (available for Windows as well as Linux/Unix or Mac OS X). This utility is based either on Poppler or on XPDF. This is a command you could try:</p> <pre><code> pdftotext \ -f 13 \ -l 17 \ -layout \ -opw supersecret \ -upw secret \ -eol unix \ -nopgbrk \ /path/to/your/pdf - |less </code></pre> <p>This will display the page range 13 (<b>f</b>irst page) to 17 (<b>l</b>ast page), preserve the layout of a double-password protected named PDF file (using user and owner passwords <em>secret</em> and <em>supersecret</em>), with Unix EOL convention, but without inserting pagebreaks between PDF pages, piped through less...</p> <p><code>pdftotext -h</code> displays all available commandline options. </p> <p>Of course, both tools only work for the text parts of PDFs (if they have any). Oh, and mathematical formula also won't work too well... ;-)</p> <hr> <p><strong><em><code>pdftotext</code> Update:</em></strong></p> <p>Recent versions of Poppler's <code>pdftotext</code> have now options to extract <em>"a portion (using coordinates) of PDF"</em> pages, like the OP asked for. The parameters are:</p> <ul> <li><strong><code>-x &lt;int&gt;</code></strong> : top left corner's x-coordinate of crop area</li> <li><strong><code>-y &lt;int&gt;</code></strong> : top left corner's y-coordinate of crop area</li> <li><strong><code>-W &lt;int&gt;</code></strong> : crop area's width in pixels (defaults to 0)</li> <li><strong><code>-H &lt;int&gt;</code></strong> : crop area's height in pixels (defaults to 0)</li> </ul> <p>Best, if used with the <code>-layout</code> parameter.</p> <hr> <h2>Fourth: MuPDF's <code>mutool draw</code> command can also extract text</h2> <p>The cross-platform, open source <a href="http://mupdf.com/" rel="noreferrer">MuPDF</a> application (made by the same company that also develops Ghostscript) has bundled a command line tool, <code>mutool</code>. To extract text from a PDF with this tool, use:</p> <pre><code>mutool draw -F txt the.pdf </code></pre> <p>will emit the extracted text to <code>&lt;stdout&gt;</code>. Use <code>-o filename.txt</code> to write it into a file.</p> <h2>Fifth: PDFLib's Text Extraction Toolkit (TET) (best of all... but it is PayWare)</h2> <p><a href="http://www.pdflib.com/products/tet/" rel="noreferrer"><strong>TET</strong></a>, the Text Extraction Toolkit from the <a href="http://www.pdflib.com/" rel="noreferrer">pdflib</a> family of products can find the x-y-coordinate of text content in a PDF file (and much more). TET has a commandline interface, and it's the most powerful of all text extraction tools I'm aware of. (It can even handle ligatures...) Quote from their website:</p> <blockquote> <p><strong><em>Geometry</em></strong><br> <em>TET provides precise metrics for the text, such as the position on the page, glyph widths, and text direction. Specific areas on the page can be excluded or included in the text extraction, e.g. to ignore headers and footers or margins.</em></p> </blockquote> <p>In my experience, while it's does not sport the most straight-forward CLI interface you can imagine: after you got used to it, it will do what it promises to do, for most PDFs you throw towards it...</p> <hr> <p>And there are even more options:</p> <ol> <li><a href="http://podofo.sf.net/" rel="noreferrer"><code>podofotxtextract</code></a> (CLI tool) from the PoDoFo project (Open Source)</li> <li><a href="http://calibre-ebook.com/download/" rel="noreferrer"><code>calibre</code></a> (normally a GUI program to handle eBooks, Open Source) has a commandline option that can extract text from PDFs</li> <li><a href="http://www.abisource.com/download/" rel="noreferrer"><code>AbiWord</code></a> (a GUI word processor, Open Source) can import PDFs and save its files as .txt: <code>abiword --to=txt --to-name=output.txt input.pdf</code></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload