-
Notifications
You must be signed in to change notification settings - Fork 567
How To Convert 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 >= 0
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.
- You can open any image file as a document and convert it to a 1-page PDF with this method. Except for transparency, PNG, JPEG, BMP, etc. are fully supported in excellent quality. If you need a transparent image (e.g. as a watermark), you should still use
Page.insertImage()
. - 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.
- 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.
HOWTO Button annots with JavaScript
HOWTO work with PDF embedded files
HOWTO extract text from inside rectangles
HOWTO extract text in natural reading order
HOWTO create or extract graphics
HOWTO create your own PDF Drawing
Rectangle inclusion & intersection
Metadata & bookmark maintenance