Using the mutool command (with examples)
The mutool command is a powerful tool that allows you to convert PDF files, query information, and extract data. It is a command-line tool that can be used in various scenarios. In this article, we will explore different use cases of the mutool command and provide code examples for each use case.
Convert pages of a PDF into PNG images
mutool convert -o path/to/output%nd.png path/to/input.pdf 1-10
Motivation: Sometimes we may need to convert specific pages of a PDF into images, for example, to use them in a presentation or to upload them to a website. The mutool command makes it easy to convert PDF pages into PNG images.
Explanation: The above command uses the convert
subcommand of mutool to convert pages 1 to 10 of the input PDF file into PNG images. The -o
flag specifies the output path and file name with a placeholder %nd
representing the page number. It should be replaced with a print modifier like %d
or %2d
to generate sequential file names.
Example output: The command will generate 10 PNG images named output1.png
, output2.png
, …, output10.png
, containing the respective pages from the input PDF.
Convert specific pages of a PDF into text
mutool draw -F txt path/to/input.pdf 2,3,5
Motivation: Extracting specific pages from a PDF as plain text can be useful for further processing or analysis. The mutool command provides the capability to convert selected pages of a PDF into text format.
Explanation: The above command uses the draw
subcommand of mutool to convert pages 2, 3, and 5 of the input PDF file into text. The -F txt
flag specifies the output format as plain text.
Example output: The command will display the text content of pages 2, 3, and 5 of the input PDF directly in the terminal.
Concatenate multiple PDF files
mutool merge -o path/to/output.pdf path/to/input1.pdf path/to/input2.pdf ...
Motivation: Often, there is a need to combine multiple PDF files into a single document, such as when merging multiple reports or presentations. The mutool command provides an easy way to concatenate PDF files.
Explanation: The above command uses the merge
subcommand of mutool to concatenate the input PDF files (input1.pdf
, input2.pdf
, …) into a single output PDF file (output.pdf
). The -o
flag specifies the output file path.
Example output: The command will generate a single PDF file named output.pdf
, which contains the content of all the input PDF files combined in the order they were provided.
Query information about a PDF file
mutool info path/to/input.pdf
Motivation: Sometimes it is necessary to obtain detailed information about a PDF file, such as the number of pages, the fonts used, or the document metadata. The mutool command allows querying and retrieving such information easily.
Explanation: The above command uses the info
subcommand of mutool to retrieve information about the input PDF file. The file path is specified as an argument.
Example output: The command will display detailed information about the input PDF file, including the number of pages, the document metadata, page dimensions, fonts used, and more.
Extract embedded resources from a PDF
mutool extract path/to/input.pdf
Motivation: PDF files can contain embedded images, fonts, and other resources. Extracting these resources can be necessary for further analysis or editing. The mutool command provides a way to extract embedded resources from a PDF.
Explanation: The above command uses the extract
subcommand of mutool to extract all embedded images, fonts, and resources from the input PDF file. The resources are extracted into the current directory.
Example output: The command will extract all embedded images, fonts, and resources from the input PDF file and save them in the current directory.
Print the outline (table of contents) of a PDF
mutool show path/to/input.pdf outline
Motivation: PDF documents can have an outline or table of contents that provides a structured overview of the document’s sections and sub-sections. Printing the outline can help in understanding the structure of the PDF and navigating through its content.
Explanation: The above command uses the show
subcommand of mutool to print the outline or table of contents of the input PDF file. The file path is specified as an argument, followed by the keyword “outline”.
Example output: The command will display the outline or table of contents of the input PDF, including section titles and page numbers.
In this article, we have explored different use cases of the mutool command with code examples for each use case. The mutool command is a versatile tool that can be used for converting, querying, and extracting data from PDF files. Whether you need to convert PDF pages into images, extract text, concatenate multiple files, or obtain information about a PDF, the mutool command has got you covered.