Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting PDF to PNG with Python (without pdf2image)

I want to convert a PDF (one page) into a PNG file. I installed pdf2image and got this error:

popler is not installed in windows.

According to this question: Poppler in path for pdf2image, Poppler should be installed and PATH modified.

I cannot do any of those (I don't have the necessary permissions in the system I am working with).

I had a look at OpenCV and PIL and none seems to offer the possibility to make this transformation: PIL (see here https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html?highlight=pdf#pdf) does not offer the possibility to read PDFs, only to save images as PDFs. The same goes for OpenCV.

Any suggestion on how to make the PDF to PNG transformation? I can install any Python library but I can not touch the Windows installation.

like image 366
JFerro Avatar asked Dec 03 '25 09:12

JFerro


1 Answers

PyMuPDF supports pdf to image rasterization without requiring any external dependencies.

Sample code to do a basic pdf to png transformation:

import fitz  # PyMuPDF, imported as fitz for backward compatibility reasons
file_path = "my_file.pdf"
doc = fitz.open(file_path)  # open document
for i, page in enumerate(doc):
    pix = page.get_pixmap()  # render page to an image
    pix.save(f"page_{i}.png")
like image 164
Seon Avatar answered Dec 05 '25 22:12

Seon