Command Line Tools

Command Line Tools

The command line tools are all gathered into one umbrella command: mutool.

For rendering and converting documents there are two commands available:

mutool draw
This is the more customizable tool, but also has a more difficult set of command line options. It is primarily used for rendering a document to image files.
mutool convert
This tool is used for converting documents into other formats and is easier to use.

There are also several tools specifically for working with PDF files.

mutool show
A tool for displaying the internal objects in a PDF file.
mutool extract
Extract images and embedded font resources.
mutool clean
Rewrite PDF file. Used to fix broken files, or to make a PDF file human editable.
mutool merge
Merge pages from multiple input files into a new PDF.
mutool create
Create a new PDF file from a text file with graphics commands.
mutool portfolio
Manipulate PDF portfolios.

And finally, there is a tool for doing anything you can imagine:

mutool run
A tool for running Javascript programs with access to the MuPDF library functions.


 
 

mutool draw

The draw command will render a document to image files, convert to another vector format, or extract the text content.

The supported input document formats are: pdf, xps, cbz, and epub.

The supported output image formats are: pbm, pgm, ppm, pam, png, tga, pwg, pcl and ps. The supported output vector formats are: svg, pdf, and debug trace (as xml). The supported output text formats are: plain text, html, and structured text (as xml).

Options:

-p password
Use the specified password if the file is encrypted.

-o output
The output format is inferred from the output filename. Embed %d in the name to indicate the page number (for example: “page%d.png”). Printf modifiers are supported, for example “%03d”. If no output is specified, the output will go to stdout.

-F format
Enforce a specific output format. Only necessary when outputting to stdout since normally the output filename is used to infer the output format.

-R angle
Rotate clockwise by given number of degrees.

-r resolution
Render the page at the specified resolution. The default resolution is 72 dpi.

-w width
Render the page at the specified width (or, if the -r flag is used, render with a maximum width).

-h height
Render the page at the specified height (or, if the -r flag is used, render with a maximum height).

-f
Fit exactly; ignore the aspect ratio when matching specified width/heights.

-B bandheight
Render in banded mode with each band no taller than the given height. This uses less memory during rendering. Only compatible with pam, pgm, ppm, pnm and png output formats. Banded rendering and md5 checksumming may not be used at the same time.

-W width
Page width in points for EPUB layout.

-H height
Page height in points for EPUB layout.

-S size
Font size in points for EPUB layout.

-U filename
User CSS stylesheet for EPUB layout.

-c colorspace
Render in the specified colorspace. Supported colorspaces are: mono, gray, grayalpha, rgb, rgbalpha, cmyk, cmykalpha. Some abbreviations are allowed: m, g, ga, rgba, cmyka. The default is chosen based on the output format.

-G gamma
Apply gamma correction. Some typical values are 0.7 or 1.4 to thin or darken text rendering.

-I
Invert colors.

-s [mft5]
Show various bits of information: m for glyph cache and total memory usage, f for page features such as whether the page is grayscale or color, t for per page rendering times as well statistics, and 5 for md5 checksums of rendered images that can be used to check if rendering has changed.

-A bits
Specify how many bits of anti-aliasing to use, the default is 8.

-D
Disable use of display lists. May cause slowdown but should reduce the amount of memory used.

-i
Ignore errors.

-L
Low memory mode (avoid caching objects by clearing cache after each page).

-P
Run interpretation and rendering at the same time.

Pages
Comma separated list of page numbers and ranges (for example: 1,5,10-15). If no pages are specified, then all pages will be rendered.


 
 

mutool convert

The ‘mutool convert’ command converts an input file into another format.

The command line options are:

input
Input file name. The input can be any of the document formats supported by MuPDF: PDF, XPS, CBZ, unprotected EPUB, FB2, etc.

[pages]
Comma-separated list of page ranges. The first page is “1”, and the last page is “N”. The default is “1-N”.

-p password
Password to use for password protected PDF documents.

-o filename
The output file name. The output file name may have a “%d” in the path, which will be replaced with the page number. If there is no %d, the page number will be appended at the end of the file name for single page formats.

-F format
The output format. If missing, it is inferred from the output file name extension. See below for which formats are supported.

-O options
Comma-separated list of output options. The set of available options varies depending on the output format. See below for details.

-A bits
Number of bits of antialiasing to use (0 to 8) when rendering to image-based formats. The default is 8.

-W width
Page width in points for EPUB layout.

-H height
Page height in points for EPUB layout.

-S font-size
Font size in points for EPUB layout.

-U stylesheet.css
Filename of user style sheet for EPUB layout.

-X
Disable document styles for EPUB layout.

Image output

CBZ (comic book zip) is a multi-page image format.

The following single page image formats are also supported: PNG, PNM, PAM, TGA, PBM, PKM. Each page is written to a separate file.

The output options (-O flag) for image formats are:

rotate=N
Rotate rendered pages N degrees counter-clockwise.

resolution=N
Set both X and Y resolution in pixels per inch.

x-resolution=N
Set X resolution in pixels per inch.

y-resolution=N
Set Y resolution in pixels per inch.

width=N
Render pages to fit N pixels wide (ignore resolution options).

height=N
Render pages to fit N pixels tall (ignore resolution options).

colorspace=gray/rgb/cmyk
Render using specified colorspace (if output format supports it).

alpha
Render pages with an alpha channel and transparent background (if output format supports it).

PDF output

With PDF output, we will create a new PDF file that matches the visual appearance. The PDF output is still a work in progress, so some features may not work.

If the input is PDF, the output will have nothing in common except the visual appearance. All bookmarks, annotations, forms, etc, will be thrown away. If you want to do a PDF to PDF conversion, ‘mutool clean’ is a better tool to use.

The PDF output options are:

decompress
Decompress all streams (except compress-fonts/images).

compress
Compress all streams.

compress-fonts
Compress embedded fonts.

compress-images
Compress images.

ascii
ASCII hex encodes binary streams.

pretty
Pretty-print objects with indentation.

linearize
Optimize for web browsers.

sanitize
Clean up graphics commands in content streams.

garbage
Garbage collect unused objects.

garbage=compact
… and compact cross-reference table.

garbage=deduplicate
… and remove duplicate objects.

SVG output

SVG output is a single page format, so we will write one SVG file for each input page.

The SVG output options are:

text=text
Emit text as elements (inaccurate fonts).

text=path
Emit text as elements (accurate fonts).

no-reuse-images
Do not reuse images using definitions.


 
 

mutool show

The show command will print the specified objects and streams to stdout. Streams are decoded and non-printable characters are represented with a period by default.

Options:

-p password
Use the specified password if the file is encrypted.

-o file
Write output to file instead of stdout.

-b
Print streams as binary data and omit the object header.

-e
Print streams in their original encoded (or compressed) form.

Specify objects by number, or use one of the following special names:

‘xref’ or ‘x’
Print the cross-reference table.

‘trailer’ or ‘t’
Print the trailer dictionary.

‘encrypt’ or ‘e’
Print the encryption dictionary.

‘pagetree’ or ‘p’
List the object numbers for every page.

‘grep’ or ‘g’
Print all the objects in the file in a compact one-line format suitable for piping to grep.

‘outline’ or ‘o’
Print the outline (table of contents).


 
 

mutool extract

The extract command can be used to extract images and font files from a PDF file.

Options:

-p password
Use the specified password if the file is encrypted.

-r
Convert images to RGB when extracting them.

If no object numbers are given on the command line, all images and fonts will be extracted.

 
 

mutool clean

The clean command pretty prints and rewrites the syntax of a PDF file. It can be used to repair broken files, expand compressed streams, filter out a range of pages, etc.

If no output file is specified, it will write the cleaned PDF to “out.pdf” in the current directory.

-p password
Use the specified password if the file is encrypted.

-g
Garbage collect objects that have no references from other objects. Give the option twice to renumber all objects and compact the cross-reference table. Give it three times to merge and reuse duplicate objects.

-s
Rewrite content streams.

-d
Decompress streams. This will make the output file larger, but provides easy access for reading and editing the contents with a text editor.

-l
Linearize output. Create a “Web Optimized” output file.

-i
Toggle decompression of image streams. Use in conjunction with -d to leave images compressed.

-f
Toggle decompression of font streams. Use in conjunction with -d to leave fonts compressed.

-a
ASCII Hex encodes binary streams. Use in conjunction with -d and -i or -f to ensure that although the images and/or fonts are compressed, the resulting file can still be viewed and edited with a text editor.

-z
Deflate uncompressed streams. If combined with -d, any decompressed streams will be recompressed. If combined with -a, the streams will also be hex encoded after compression.pages

pages
Comma-separated list of page numbers and ranges to include.


 
 

mutool merge

The merge command is used to pick out pages from two or more files and merge them in order into a new output file.

Options:

-o output
The output filename.

-O options
See mutool create for details on this option.


 
 

mutool create

The create command creates a new PDF file with the contents created from one or more input files containing graphics commands.

If no output file is specified, it will write the created PDF to “out.pdf” in the current directory.

The -O argument is a comma-separated list of options for writing the PDF file:

decompress
Decompress all object streams.

compress
Compress all object streams.

compress-fonts
Compress object streams for embedded fonts.

compress-images
Compress object streams for images.

ascii
Encode object streams using ASCII hex encoding.

pretty
Pretty-print objects with indentation.

linearize
Optimize document for progressive loading in viewers.

sanitize
Clean up graphics command in content streams.

garbage[=compact|deduplicate]
Garbage collect unused objects. With compact, the cross-reference table will also be compacted. With deduplicate, duplicate objects will also be recombined.

A page is created for each input file, with the contents of the file copied into the content stream. Special comments in the input files are parsed to define the page dimensions and font and image resources:

An example input file:


 
 

mutool porfolio

The portfolio tool can list files in, extract files from, and add files to PDF portfolios.

Options are:

-p password
password

-o filename
output (defaults to input file)

-O options
PDF output options (see mutool create)

Actions are:

t
Display a table listing the contents of the portfolio.

N file
Extract Nth entry to file.

file name
Add contents of file as an entry named name.

For safety, only use ASCII characters in entry names for now.