Skip to content

Instantly share code, notes, and snippets.

@mara004
Last active July 11, 2024 20:35
Show Gist options
  • Save mara004/012b4d216ec285ddd766b6e1a034d045 to your computer and use it in GitHub Desktop.
Save mara004/012b4d216ec285ddd766b6e1a034d045 to your computer and use it in GitHub Desktop.
PDF rendering with python-poppler
# SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: MPL-2.0
# Note that Poppler is GPL-licensed, so this code is altogether affected by copyleft
import PIL.Image
import poppler # python-poppler
from poppler.cpp.page_renderer import render_hint
def _translate_rotation(rotation):
# Beware: this enum contained two typos with python-poppler <= 0.2.2
# https://github.com/cbrunet/python-poppler/issues/42
return {
0: poppler.Rotation.rotate_0,
90: poppler.Rotation.rotate_90,
180: poppler.Rotation.rotate_180,
270: poppler.Rotation.rotate_270,
}[rotation]
def invoke_poppler_py(filepath, index, scale=4, rotation=0, password=None):
# Note, as of this writing, poppler does not take /UserUnit into account.
if password is None:
pdf = poppler.load_from_file(filepath)
else:
pdf = poppler.load_from_file(
filepath,
owner_password = password,
user_password = password,
)
page = pdf.create_page(index)
dpi = scale * 72
# output looks poor without general anti-aliasing
renderer = poppler.PageRenderer()
renderer.set_render_hint(hint=render_hint.antialiasing, on=True) # -> 1
renderer.set_render_hint(hint=render_hint.text_antialiasing, on=True) # -> 2
image = renderer.render_page(
page, xres=dpi, yres=dpi,
rotate = _translate_rotation(rotation),
)
pil_image = PIL.Image.frombytes("RGBA", (image.width, image.height), image.data, "raw", str(image.format))
return pil_image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment