Package: archive-pdf-tools
Authors:Merlijn Wajer <merlijn@archive.org>Date:2021-11-14 18:00This repository contains a library to perform MRC (Mixed Raster Content)
compression on images[*], which offers lossy high compression of images, in
particular images with text.Additionally, the library can generate MRC-compressed PDF files with hOCR[†]text layers mixed into to the PDF, which makes searching and copy-pasting of the
PDF possible. PDFs generated bybin/recode_pdfshould bePDF/A 3bandPDF/UAcompatible.Some of the tooling also supports specific Internet Archive file formats (such
as the “scandata.xml” files, but the tooling should work fine without those
files, too.While the code is already being used internally to create PDFs at the Internet
Archive, the code still needs more documentation and cleaning up, so don’t
expect this to be super well documented just yet.FeaturesReliable: has produced over 6 million PDFs in 2021 alone (each with many
hundreds of pages)Fast and robust compression: Competes directly with the proprietary software
offerings when it comes to speed and compressibility (often outperforming in
both)MRC compression of images, leading to anywhere from 3-15x compression ratios,
depending on the quality setting provided.Creates PDF from a directory of imagesImproved compression based on OCR results (hOCR files)Hidden text layer insertion based on hOCR files, which makes a PDF searchable
and the text copy-pasteable.PDF/A 3b compatible.Basic PDF/UA support (accessibility features)Creation of 1 bit (black and white) PDFsDependenciesPython 3.xPython packages (also seerequirements.txt):PyMuPDFlxmlscikit-imagePillowromanarchive-hocr-toolsOne-of:Kakadu JPEG2000 binariesOpen source OpenJPEG2000 tools (opj_compress and opj_decompress)Grok(grk_compress and grk_decompress)jpegoptim(when using JPEG instead of JPEG2000)For JBIG2 compression:jbig2encfor JBIG2 compression (and PyMuPDF 1.19.0 or higher)InstallationFirst install dependencies. For example, in Ubuntu:sudo apt install libleptonica-dev libopenjp2-tools libxml2-dev libxslt-dev python3-dev python3-pip

sudo apt install automake libtool
git clone https://github.com/agl/jbig2enc
cd jbig2enc
./autogen.sh
./configure && make
sudo make installBecausearchive-pdf-toolsis on thePython Package Index(PyPI), you can usepip(the Python 3 version is often calledpip3) to install the latest version:# Latest version
pip3 install archive-pdf-tools

# Specific version
pip3 install archive-pdf-tools==1.4.14Alternatively, if you want a specific commit or unreleased version, check out the master branch or atagged releaseand usepipto install:git clone https://github.com/internetarchive/archive-pdf-tools.git
cd archive-pdf-tools
pip3 install .Finally, if you’ve downloaded a wheel to test a specific commit, you can also install it usingpip:pip3 install --force-reinstall -U --no-deps ./archive_pdf_tools-${version}.whlTo see ifarchive-pdf-toolsis installed correctly for your user, run:recode_pdf --versionNot well tested features“Recoding” an existing PDF, extracting the images and creating a new PDF with
the images from the existing PDF is not well tested. This works OK if every
PDF page just has a single image.Known issuesUsing--image-mode0and--image-mode1is currently broken, so only
MRC or no images is supported.It is not possible to recode/compress a PDF without hOCR files. This will be
addressed in the future, since it should not be a problem to generate a PDF
lacking hOCR data.Planned featuresAddition of a second set of fonts in the PDFs, so that hidden selected text
also renders the original glyphs.Better background generation (text shade removal from the background)Better compression parameter selection, I have not toyed around that much with
kakadu and grok/openjpeg2000 parameters.MRCThe goal of Mixed Raster Content compression is to decompose the image into a
background, foreground and mask. The background should contain components that
are not of particular interest, whereas the foreground would contain all
glyphs/text on a page, as well as the lines and edges of various drawings or
images. The mask is a 1-bit image which has the value ‘1’ when a pixel is part
of the foreground.This decomposition can then be used to compress the different components
individually, applying much higher compression to specific components, usually
the background, which can be downscaled as well. The foreground can be quite
compressed as well, since it mostly just needs to contain the approximate
colours of the text and other lines - any artifacts introduced during the
foreground compression (e.g. ugly artifact around text borders) are removed by
overlaying the mask component of the image, which is losslessly compressed
(typically using either JBIG2 or CCITT).In a PDF, this usually means the background image is inserted into a page,
followed by the foreground image, which uses the mask as its alpha layer.UsageCreating a PDF from a set of images is pretty straightforward:recode_pdf --from-imagestack 'sim_english-illustrated-magazine_1884-12_2_15_jp2/*' \
    --hocr-file sim_english-illustrated-magazine_1884-12_2_15_hocr.html \
    --dpi 400 --bg-downsample 3 \
    -m 2 -t 10 --mask-compression jbig2 \
    -o /tmp/example.pdf
[...]
Processed 9 pages at 1.16 seconds/page
Compression ratio: 7.144962Or, to scan a document, OCR it with Tesseract and save the result as a compressed PDF
(JPEG2000 compression with OpenJPEG, background downsampled three times), with
text layer:scanimage --resolution 300 --mode Color --format tiff | tee /tmp/scan.tiff | tesseract - - hocr > /tmp/scan.hocr ; recode_pdf -v -J openjpeg --bg-downsample 3 --from-imagestack /tmp/scan.tiff --hocr-file /tmp/scan.hocr -o /tmp/scan.pdf
[...]
Processed 1 pages at 11.40 seconds/page
Compression ratio: 249.876613Examining the resultsmrcview(tools/mrcview) is shipped with the package and can be used to turn a
MRC-compressed PDF into a PDF with each layer on a separate page, this is the
easiest way to inspect the resulting compression. Run it like so:mrcview /tmp/compressed.pdf /tmp/mrc.pdfThere is alsomaskview, which just renders the masks of a PDF to another PDF.Alternatively, one could usepdfimagesto extract the image layers of a
specific page and then view them with your favourite image viewer:pageno=0; pdfimages -f $pageno -l $pageno -png path_to_pdf extracted_image_base
feh extracted_image_base*.pngtools/pdfimagesmrccan be used to check how the size of the PDF
is broken down into the foreground, background, masks and text layer.LicenseLicense for all code (minusinternetarchive/pdfrenderer.py) is AGPL 3.0.internetarchive/pdfrenderer.pyis Apache 2.0, which matches the Tesseract
license for that file.[*]https://en.wikipedia.org/wiki/Mixed_raster_content[†]http://kba.cloud/hocr-spec/1.2/