What it is
MuPDF is a lightweight, high-performance PDF, XPS, and EPUB rendering library written in C. It allows text extraction, page rendering, and manipulation of PDF documents with minimal memory usage.
MuPDF provides APIs to render PDF pages to bitmaps, extract text, access metadata, and annotate documents. It supports interactive viewing, searching, and converting pages to images or other formats.
Installation
sudo apt install mupdf mupdf-tools libmupdf-devGetting started
The smallest useful thing you can do with it, and what each part means.
# Command-line example
mutool draw -o output.png input.pdf 1# Command-line example
mutool extract input.pdfAdvanced usage
Where the library earns its place over a simpler alternative.
#include "mupdf/fitz.h"
int main() {
fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
fz_document *doc = fz_open_document(ctx, "input.pdf");
int page_count = fz_count_pages(ctx, doc);
printf("Number of pages: %d\n", page_count);
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
return 0;
}// Use fz_new_pixmap_from_page to render pages and save as PNG or display in GUI applications.// Use fz_new_text_page and fz_text_page_from_page to extract structured text from a PDF page.Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Failed to open document
- Ensure the file path is correct and the PDF is not corrupted.
- Memory allocation errors
- Manage contexts, documents, and pixmaps carefully to avoid leaks.
- Text extraction returns empty
- Some PDFs may store text as images; OCR may be required.
Best practices
- Always create and drop `fz_context` to manage memory safely.
- Check return values for all MuPDF API calls to handle errors gracefully.
- Use MuPDF for lightweight PDF rendering where memory efficiency is important.
- Release resources such as documents and pixmaps after use.
- Combine MuPDF with GUI libraries for building custom PDF viewers.
Background
Why it exists, and what it was reacting to.
MuPDF was developed by Artifex Software to provide a fast and compact alternative to larger PDF libraries. It is commonly used in mobile applications, desktop software, and embedded systems where performance and low resource usage are critical.
