Draw a page as an image¶
ozaru.render_page draws a page of a PDF as a PNG image:
import struct
import ozaru
# Draw the first page of the PDF at twice the size of a point: 144 dots per inch.
png = ozaru.render_page("report.pdf", 1, scale=2)
with open("page-1.png", "wb") as file:
file.write(png)
width, height = struct.unpack(">II", png[16:24])
print(f"page-1.png is {width} x {height} pixels")
Output:
The arguments are the PDF and the number of the page, counted from 1, as
page_info numbers them. A page the PDF doesn't have raises an IndexError.
The result is the bytes of the PNG file: write them to a file, as above, or send
them somewhere without saving them.
The size of the image¶
scale is how many pixels each point of the page takes. A point is 1/72 of an
inch, so the default, 1, draws 72 dots per inch, and 2 draws 144. The size in
pixels is rounded up: an A4 page, 595.28 by 841.89 points, is 596 by 842 pixels
at scale 1 and 1191 by 1684 at scale 2.
The image is of the page as a viewer shows it: its crop box, turned by the
rotation the page has. A scale that would make an image of more than 256 MB
raises a ValueError: use a smaller one.
What is drawn¶
Text, lines and shapes with their colors, transparency and clipping, and images, including those with a mask. Transparency includes opacity, groups of objects painted as a whole, blend modes such as Multiply, and soft masks. Patterns that repeat and gradients (axial, radial and function-based) are drawn too. The page is drawn on white paper, as the writer of the PDF saw it.
Annotations are drawn with their appearance: notes, highlights, shapes, and the
widgets of form fields with what they hold. Pass annotations=False to leave them
out. See Read the annotations.
The fonts are the ones embedded in the PDF: TrueType, CFF, Type 1 and Type 3 fonts, simple and composite. A font that isn't embedded, such as Helvetica, is drawn with a font of similar shape and the same measures.
A few things are left out for now, and listed in Limitations: mesh gradients and JPEG 2000 images.
Protected and damaged PDFs¶
Pass password for a protected PDF, as with every function. A damaged page is
drawn empty, and a PageWarning tells you its number, as in
Handle errors.