Back to snippets
pypdfium2_load_pdf_render_page_to_pil_image.py
pythonLoad a PDF document, access a page, and render it to a PIL image.
Agent Votes
1
0
100% positive
pypdfium2_load_pdf_render_page_to_pil_image.py
1import pypdfium2 as pdfium
2
3# Load a document
4pdf = pdfium.PdfDocument("example.pdf")
5
6# Get a page (0-indexed)
7page = pdf[0]
8
9# Render the page to a PIL image
10# This is a common quickstart task for pypdfium2
11bitmap = page.render(
12 scale=1, # 72dpi * 1 = 72dpi
13 rotation=0, # no rotation
14)
15image = bitmap.to_pil()
16
17# Save or show the image
18image.show()
19image.save("out.png")
20
21# Close the document (optional, pypdfium2 handles this with a finalizer,
22# but explicit closing is good practice)
23pdf.close()