Opened 5 years ago

Last modified 3 years ago

#942 new defect

New pdftohtml with Xpdf tools - works with newer PDFs too

Reported by: ak19 Owned by: ak19
Priority: moderate Milestone: 3.11 Release
Component: Collection Building Severity: major
Keywords: xpdf Cc:


Kathy found that users on the mailing list wanted more HTML output options with PDFPlugin. PDFBox's pagedimg output option was modified to produce img+text, but Kathy was hoping there were more possibilities for actual PDF to HTML support out there.

Dr Bainbridge first found PDFtoDOM which was based on PDFBox. But this produced unsatisfactory HTML (sometimes fonts weren't extracted, often fonts made the display hard to read due to overlapping characters, a <div> element around every word rather than every line).

Then Dr Bainbridge found XPdf Tools, which contained a new pdftohtml, which produced results we liked. Its pdftohtml tool outputs screenshots of each PDF page's background + the text overlaid, all as HTML. One html doc per page was produced, and we'd manipulate these into a single sectionalised HTML doc.

To get Xpdf tools to work with GS in this way:

  1. Downloaded Xpdf tools binaries for Lin/Win/Mac, eventually to be compiled up for Lin & Mac
  1. To manipulate the HTML DOM produced, Dr Bainbridge found the perl module Mojo::DOM, which he compiled up.
  1. Then the code was modified to make use of these. The list of commit revisions so far follow below.
  1. This led to thinking that PDFPlugin needed to be restructured as its configuration options were already complicated and filled with mutually contradictory options since pdfbox_conversion was included, and now to become more complicated and contradictory with the inclusion of XPDF tools.

The commit revisions thus far that make use of Xpdf Tools' pdftohtml and its pdftotext to finally support PDF to text conversion on Windows are as follows. None of these commits concern restructuring the PDFPlugin as yet. -,, -

Note that the Xpdf tools binaries for mac have been committed to an svn ignored folder and that they're not yet automatically checked out. Either we get Xpdf tools to compile from src (if we can get past the fact that Xpdf tools use CMake to configure and build rather than autotools' configure script that we're used to) or we find a better SVN location to put the Mac binaries of Xpdf tools.

Change History (14)

comment:1 by ak19, 5 years ago


Xpdf's last mod date is in 2017 and it includes its own pdftohtml utility tool, whereas the old "pdftohtml" tool that GS used was last updated 2013 (and itself made use of Xpdf, possible older versions).


As per the Readme file found in the linux binary of Xpdf Tools, the Xpdf Viewer requires the qt toolkit, but not the Xpdf Tools. Have not read the Install file to confirm whether the same is the case for when compiling the command line tools. (But in that case, can't we just include the tools binary available for all 3 OS, instead of compiling on each platform)

Using Xpdf's pdftohtml tool:

greenstone@machine:~/Downloads/xpdf-tools-linux-4.00/bin64$./pdftohtml -z 1.5 ~/Downloads/ApacheLicence.pdf licence

where licence is a folder

(Configuration flags you can put into ~/.xpdfrc to use as defaults when running xpdf tool commands)

  1. We're using Xpdf Tools version: xpdf-tools-linux-4.00

comment:2 by ak19, 5 years ago

Mojo::DOM (Perl)

  1. Before Dr Bainbridge found Mojo::DOM, he looked at
  1. Main links for Mojo::DOM


Once Mojo::DOM's src has been downloaded, follow Dr Bainbridge's sequence of commands for building the Mojo::DOM CPAN module of perl below.
We're using this module to parse the HTML output by XPDF tool pdftohtml.

2019 mkdir cpan
2020 tar xvzf Mojolicious-7.84.tar.gz
2021 cd Mojolicious-7.84/
2028 perl ./Makefile.PL PREFIX=pwd/installed
2030 make
2031 make install
2033 cp -r installed/share/perl/5.18.2 ../cpan
2043 cd ..
2044 export PERL5LIB=pwd/cpan

2053 emacs -nw

Add the shebang and an import statement:

#!/usr/bin/perl -w
add in 'use v5.10;'

Save the file and testrun the program included with the Mojo package:

2054 chmod a+x
2055 ./

  1. The relevant folders of the compiled up Mojo module were then committed to SVN to become part of perllib/cpan:

comment:3 by ak19, 5 years ago

PDF2DOM: tried it out, but wasn't what we wanted

At present, Greenstone does not make use of PDFtoDOM.
We had earlier tried out the PDFBox-based PDFtoDOM for converting PDF to HTML, but settled with Xpdf-tools instead. In case we ever want to proceed with PDFtoDOM, here are some instructions.

Using PDFBox to convert a PDF to full HTML, where both images and text are produced and placed correctly with respect to each other, is tricky, see
(Google: pdfbox to convert pdf to html with images)

PDF2DOM tool (based on PDFBox) can be used to convert PDF to HTML with images


java -jar PDFToHTML.jar <infile> [<outfile>]

greenstone@machine-name:~/Downloads$ java -jar PDFToHTML.jar SampleDoc1.pdf -im=SAVE_TO_DIR -idir=/home/greenstone/Downloads/tmp1 -fm=SAVE_TO_DIR -fdir=/home/greenstone/Downloads/tmp2

It will output the page, but you'll see the following output indicating that the logger is not displaying anything:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See for further details.


To see error output download SLF4J simple jar, run as follows:

greenstone@machine-name:~/Downloads$ java -classpath slf4j-simple-1.7.25.jar:PDFToHTML.jar ApacheLicencePDFA.pdf -im=SAVE_TO_DIR -idir=/home/greenstone/Downloads/tmp1 -fm=SAVE_TO_DIR -fdir=/home/greenstone/Downloads/tmp2

The above is a MS Word produced PDF (archive format) and works fine: font folder generated containing the extracted fonts.

The following is a PDF produced from the same doc file by the latest libreoffice installed on Windows:


But running the same command on it produces the following font errors:

greenstone@machine-name:~/Downloads$ java -classpath slf4j-simple-1.7.25.jar:PDFToHTML.jar ApacheLicencePDFA_FromODT.pdf -im=SAVE_TO_DIR -idir=/home/greenstone/Downloads/tmp1 -fm=SAVE_TO_DIR -fdir=/home/greenstone/Downloads/tmp2

[main] INFO org.reflections.Reflections - Reflections took 163 ms to scan 1 urls, producing 36 keys and 222 values
[main] WARN - Error loading font 'BAAAAA+Georgia' Message: FontVerter could not detect the input font's type. class
[main] WARN - Error loading font 'CAAAAA+Georgia-Bold' Message: FontVerter could not detect the input font's type. class
[main] WARN - Error loading font 'BAAAAA+Georgia' Message: FontVerter could not detect the input font's type. class
[main] WARN - Error loading font 'CAAAAA+Georgia-Bold' Message: FontVerter could not detect the input font's type. class

So: Fonts get extracted if the source PDF was generated by MS Word's doc to PDF conversion. Fonts didn't get extracted from PDF upon conversion to HTML when libreoffice was used to convert a .doc to the source PDF.

Further, the HTML file produced contains <div> elements around every word rather than around every line or paragraph, and this is a problem when we build the HTML in Greenstone: on preview, each word is obviously on its own line as the styling information in the <head> sections of the HTML was not maintained. Also, the HTML files generated by PDFtoDOM are unnecessarily large because of the <div>s around every word, each of which has long sections of duplicated inline styling.

comment:4 by ak19, 5 years ago

The next step was to compile up xpdf-tools from source on both linux and mac to create static executables of pdftohtml, pdftotext and other tools. Then generate the tarballs that we will be extracting (or maybe commit each tool in the linux/bin and mac/bin folder as before).

The changesets to do with the above are from to inclusive.

comment:5 by ak19, 5 years ago

Further commits related to compiling up static xpdf-tools binaries and getting them into the correct location on trac: to

comment:6 by ak19, 5 years ago

Some further minor changes related to compilling up xpdftools statically on Linux/Mac: and

comment:7 by ak19, 5 years ago

Restructuring and refactoring PDFPlugin (old pdftohtml + pdfbox) into

  • PDFv1Plugin: uses only the old pdftohtml
  • PDFv1Plugin: uses pdfbox and xpdftools (from xpdftools, just pdftohtml and pdftotext at present).

Commits related to the refactoring: -, -

Note that "PDFPlugin", which is being deprecated, is for now still present for backwards compatibility with those migrating from an earlier version of GS to the newer one.

IMPORTANT: PDFv2Plugin assumes the pdf-box extension is installed and everything is setup for it like perl. So it will work out of the box for GS3, but not for GS2 where the pdf-box extension has to be manually downloaded into the right folder.

comment:10 by ak19, 5 years ago

To get all the files modified by this ticket (so far):

  • gs2build/setup.bat

  • gs2build/setup.bash

  • gs2build/collect/modelcol/etc/collectionConfig.xml

  • gs2build/bin/script/

  • gs2build/bin/script/

  • gs2build/perllib/

  • gs2build/perllib/plugins/

  • gs2build/perllib/plugins/

  • gs2build/perllib/plugins/

  • gs2build/perllib/plugins/

  • gs2build/perllib/plugins/

  • gs2build/perllib/

PDF-Box tarball:

One of:

  • gs2build/perllib/cpan/Mojo

  • gs2build/perllib/cpan/Mojolicious

comment:11 by ak19, 5 years ago

Also related to restructuring and refactoring PDFPlugin:

comment:14 by kjdon, 3 years ago

Milestone: 3.10 Release3.11 Release

Ticket retargeted after milestone closed

Note: See TracTickets for help on using tickets.