Source code

Download source archive ↓

src/pypdf/papersizes.py

1"""Helper to get paper sizes."""
2
3from typing import NamedTuple
4
5
6class Dimensions(NamedTuple):
7    width: int
8    height: int
9
10
11class PaperSize:
12    """(width, height) of the paper in portrait mode in pixels at 72 ppi."""
13
14    # Notes of how to calculate it:
15    # 1. Get the size of the paper in millimeters
16    # 2. Convert it to inches (25.4 millimeters is equal to 1 inch)
17    # 3. Convert it to pixels at 72dpi (1 inch is equal to 72 pixels)
18
19    # All Din-A paper sizes follow this pattern:
20    # 2 x A(n - 1) = A(n)
21    # So the height of the next bigger one is the width of the smaller one
22    # The ratio is always approximately 1:2**0.5
23    # Additionally, A0 is defined to have an area of 1 m**2
24    # https://en.wikipedia.org/wiki/ISO_216
25    # Be aware of rounding issues!
26    A0 = Dimensions(2384, 3370)  # 841mm x 1189mm
27    A1 = Dimensions(1684, 2384)
28    A2 = Dimensions(1191, 1684)
29    A3 = Dimensions(842, 1191)
30    A4 = Dimensions(
31        595, 842
32    )  # Printer paper, documents - this is by far the most common
33    A5 = Dimensions(420, 595)  # Paperback books
34    A6 = Dimensions(298, 420)  # Postcards
35    A7 = Dimensions(210, 298)
36    A8 = Dimensions(147, 210)
37
38    # Envelopes
39    C4 = Dimensions(649, 918)
40
41
42_din_a = (
43    PaperSize.A0,
44    PaperSize.A1,
45    PaperSize.A2,
46    PaperSize.A3,
47    PaperSize.A4,
48    PaperSize.A5,
49    PaperSize.A6,
50    PaperSize.A7,
51    PaperSize.A8,
52)