Skip to content

How To Convert to PDF

Jorj X. McKie edited this page May 11, 2018 · 9 revisions

Convert arbitrary documents to PDF

A new feature introduced with PyMuPDF v1.13.3 and extended in v1.13.4, is conversion of arbitrary documents to PDF.

This works using method Document.convertToPDF(from_page=0, to_page=-1, rotation=0). The parameters control the input page range and sequence, and page rotation. If from_page > to_page page sequence is reversed. Default parameters will convert the complete document.

The result is a Python bytes object. Open it as a new PyMuPDF PDF document, or save it to disk using the ".pdf" file extension.

How well does it work?

  • Image files (like PNG, JPEG, BMP, etc.) are fully supported in excellent quality when opened as documents.
  • XPS, EPUB, CBZ files work very well, too. Other types like FB2 have not been tested yet. The resulting PDF will not contain the original's metadata, bookmarks and links. However, most of this information can be recovered by using PyMuPDF's features for the resulting PDF. See demo script xps-converter.py.
  • SVG images do also work, but as per today there are gaps and inconsistencies for some files.

Typical use cases

  • MuPDF does not support modifications of non-PDF documents. This method allows to circumvent that restriction.
  • See above demo script
  • Convert images (e.g. in a pictures folder) to pages in a PDF file. While this has been possible with other PyMuPDF features for a long time, the new method is much easier to use, faster and avoids pixmaps. Output page sizes automatically equal the image dimensions.
Clone this wiki locally