1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
|
"""
This module contains the serialization logic that produces a PDF document from a FPDF instance.
Most of the code in this module is used when FPDF.output() is called.
The contents of this module are internal to fpdf2, and not part of the public API.
They may change at any time without prior warning or any deprecation period,
in non-backward-compatible ways.
"""
# pyright: reportAttributeAccessIssue=false, reportUnknownMemberType=false, reportPrivateUsage=false
import logging
import re
# pylint: disable=protected-access
from abc import ABC, abstractmethod
from collections import OrderedDict, defaultdict
from contextlib import contextmanager
from datetime import datetime, timezone
from html import escape as _html_escape
from io import BytesIO
from fontTools import subset as ftsubset
from .annotations import AnnotationDict, PDFAnnotation
from .drawing import ImageSoftMask, PaintSoftMask
from .drawing_primitives import Transform
from .enums import OutputIntentSubType, PageLabelStyle, PDFResourceType, SignatureFlag
from .errors import FPDFException
from .font_type_3 import Type3Font
from .fonts import CORE_FONTS, CoreFont, TTFFont
from .image_datastructures import RasterImageInfo
from .line_break import TotalPagesSubstitutionFragment
from .outline import OutlineDictionary, OutlineItemDictionary, build_outline_objs
from .pattern import Gradient, MeshShading, Pattern, Shading
from .sign import Signature, sign_content
from .syntax import (
DestinationXYZ,
Name,
PDFArray,
PDFContentStream,
PDFDate,
PDFObject,
PDFString,
Raw,
build_obj_dict,
create_dictionary_string as pdf_dict,
create_list_string as pdf_list,
iobj_ref as pdf_ref,
)
from .util import int2roman, int_to_letters
try:
from endesive import signer
except ImportError:
signer = None
from typing import (
TYPE_CHECKING,
Any,
ItemsView,
Iterator,
Literal,
Optional,
Sequence,
Union,
cast,
)
if TYPE_CHECKING:
from .drawing import BlendGroup, GraphicsStyle
from .encryption import EncryptionDictionary, StandardSecurityHandler
from .enums import PageLayout, PageMode
from .fonts import PDFFontDescriptor
from .fpdf import FPDF
from .prefs import ViewerPreferences
from .transitions import Transition
LOGGER = logging.getLogger(__name__)
ZOOM_CONFIGS = { # cf. section 8.2.1 "Destinations" of the 2006 PDF spec 1.7:
"fullpage": ("/Fit",),
"fullwidth": ("/FitH", "null"),
"real": ("/XYZ", "null", "null", "1"),
}
class ContentWithoutID(ABC):
@abstractmethod
def serialize(
self, _security_handler: Optional["StandardSecurityHandler"] = None
) -> str:
raise NotImplementedError
class PDFHeader(ContentWithoutID):
"""
Emit the PDF file header as required by ISO 32000-1, §7.5.2 “File header”.
The header consists of:
1) A line starting with the literal "%PDF-" followed by the file version
2) If the file contains binary data an immediate second line that is a comment
starting with "%" and containing at least four bytes with values ≥ 128 (non-ASCII).
This helps file-transfer tools treat the content as binary rather than text.
"""
def __init__(self, pdf_version: str) -> None:
self.pdf_version = pdf_version
def serialize(
self, _security_handler: Optional["StandardSecurityHandler"] = None
) -> str:
return f"%PDF-{self.pdf_version}\n%éëñ¿"
class PDFFont(PDFObject):
def __init__(
self,
subtype: str,
base_font: str,
encoding: Optional[str] = None,
d_w: Optional[float] = None,
w: Optional[str] = None,
) -> None:
super().__init__()
self.type = Name("Font")
self.subtype = Name(subtype)
self.base_font = Name(base_font)
self.encoding = Name(encoding) if encoding else None
self.d_w = d_w # default glyph width
self.w = w # widths list
self.descendant_fonts: Optional[PDFArray] = None
self.to_unicode: Optional[PDFContentStream] = None
self.c_i_d_system_info: Optional[CIDSystemInfo] = None
self.font_descriptor: Optional["PDFFontDescriptor"] = None
self.c_i_d_to_g_i_d_map: Optional[PDFContentStream] = None
class CIDSystemInfo(PDFObject):
def __init__(self) -> None:
super().__init__()
self.registry = PDFString("Adobe", encrypt=True)
self.ordering = PDFString("UCS", encrypt=True)
self.supplement = 0
class PDFType3Font(PDFObject):
def __init__(self, font3: "Type3Font") -> None:
super().__init__()
self._font3 = font3
self.type = Name("Font")
self.name = Name(f"MPDFAA+{font3.base_font.name}")
self.subtype = Name("Type3")
self.font_b_box = (
f"[{self._font3.base_font.ttfont['head'].xMin * self._font3.scale:.0f}"
f" {self._font3.base_font.ttfont['head'].yMin * self._font3.scale:.0f}"
f" {self._font3.base_font.ttfont['head'].xMax * self._font3.scale:.0f}"
f" {self._font3.base_font.ttfont['head'].yMax * self._font3.scale:.0f}]"
)
self.font_matrix = "[0.001 0 0 0.001 0 0]"
self.first_char = min(g.unicode for g in font3.glyphs)
self.last_char = max(g.unicode for g in font3.glyphs)
self.resources: Optional[str] = None
self.to_unicode: Optional[str] = None
@property
def char_procs(self) -> str:
return pdf_dict(
{f"/{g.glyph_name}": f"{g.obj_id} 0 R" for g in self._font3.glyphs}
)
@property
def encoding(self) -> str:
return pdf_dict(
{
Name("/Type"): Name("/Encoding"),
Name("/Differences"): self.differences_table(),
}
)
@property
def widths(self) -> str:
sorted_glyphs = sorted(self._font3.glyphs, key=lambda glyph: glyph.unicode)
# Find the range of unicode values
min_unicode = sorted_glyphs[0].unicode
max_unicode = sorted_glyphs[-1].unicode
# Initialize widths array with zeros
widths = [0] * (max_unicode + 1 - min_unicode)
# Populate the widths array
for glyph in sorted_glyphs:
widths[glyph.unicode - min_unicode] = round(
glyph.glyph_width * self._font3.scale + 0.001
)
return pdf_list([str(glyph_width) for glyph_width in widths])
def generate_resources(
self,
img_objs_per_index: dict[int, "PDFXObject"],
gfxstate_objs_per_name: dict[str, "PDFExtGState"],
pattern_objs_per_name: dict[str, "Pattern"],
) -> None:
resources = "<<"
objects = " ".join(
f"/I{img} {img_objs_per_index[img].id} 0 R"
for img in self._font3.images_used
)
resources += f"/XObject <<{objects}>>" if len(objects) > 0 else ""
ext_g_state = " ".join(
f"/{name} {gfxstate_obj.id} 0 R"
for name, gfxstate_obj in gfxstate_objs_per_name.items()
if name in self._font3.graphics_style_used
)
resources += f"/ExtGState <<{ext_g_state}>>" if len(ext_g_state) > 0 else ""
pattern = " ".join(
f"/{name} {pattern.id} 0 R"
for name, pattern in pattern_objs_per_name.items()
if name in self._font3.patterns_used
)
resources += f"/Pattern <<{pattern}>>" if len(pattern) > 0 else ""
resources += ">>"
self.resources = resources
def differences_table(self) -> str:
sorted_glyphs = sorted(self._font3.glyphs, key=lambda glyph: glyph.unicode)
return (
"["
+ "\n".join(
f"{glyph.unicode} /{glyph.glyph_name}" for glyph in sorted_glyphs
)
+ "]"
)
class PDFInfo(PDFObject):
def __init__(
self,
title: Optional[str],
subject: Optional[str],
author: Optional[str],
keywords: Optional[str],
creator: Optional[str],
producer: Optional[str],
creation_date: PDFDate,
) -> None:
super().__init__()
self.title = PDFString(title, encrypt=True) if title else None
self.subject = PDFString(subject, encrypt=True) if subject else None
if author and isinstance(author, (list, tuple, set)):
author = "; ".join(str(a) for a in author)
self.author = PDFString(author, encrypt=True) if author else None
if keywords and isinstance(keywords, (list, tuple, set)):
keywords = ", ".join(str(keyword) for keyword in keywords)
self.keywords = PDFString(keywords, encrypt=True) if keywords else None
self.creator = PDFString(creator, encrypt=True) if creator else None
self.producer = PDFString(producer, encrypt=True) if producer else None
self.creation_date = creation_date
class AcroForm:
def __init__(self, fields: PDFArray, sig_flags: int):
self.fields = fields
self.sig_flags = sig_flags
def serialize(
self,
_security_handler: Optional["StandardSecurityHandler"] = None,
_obj_id: Optional[int] = None,
) -> str:
obj_dict = build_obj_dict(
{key: getattr(self, key) for key in dir(self)},
_security_handler=_security_handler,
_obj_id=_obj_id,
)
return pdf_dict(obj_dict, field_join=" ")
class PDFCatalog(PDFObject):
def __init__(
self,
lang: Optional[str] = None,
page_layout: Optional["PageLayout"] = None,
page_mode: Optional["PageMode"] = None,
viewer_preferences: Optional["ViewerPreferences"] = None,
) -> None:
super().__init__()
self.type = Name("Catalog")
self.lang = PDFString(lang) if lang else None
self.page_layout = page_layout
self.page_mode = page_mode
self.viewer_preferences = viewer_preferences
self.pages: Optional[PDFPagesRoot] = (
None # Required; shall be an indirect reference
)
self.acro_form: Optional[AcroForm] = None
self.open_action: Optional[str] = None
self.mark_info: Optional[str] = None
self.metadata: Optional[PDFXmpMetadata] = None
self.names: Optional[str] = None
self.outlines: Optional[OutlineDictionary] = None
self.output_intents: Optional[PDFArray] = None
self.struct_tree_root: Optional[PDFObject] = None
self.a_f: Optional[str] = None
self.page_labels: Optional[str] = None
class PDFResources(PDFObject):
def __init__(
self,
proc_set: Optional[str],
font: Optional[str],
x_object: Optional[str],
ext_g_state: Optional[str],
shading: Optional[str],
pattern: Optional[str],
) -> None:
super().__init__()
self.proc_set = proc_set
self.font = font
self.x_object = x_object
self.ext_g_state = ext_g_state
self.shading = shading
self.pattern = pattern
class PDFFontStream(PDFContentStream):
def __init__(self, contents: bytes) -> None:
super().__init__(contents=contents, compress=True)
self.length1 = len(contents)
class PDFXmpMetadata(PDFContentStream):
def __init__(self, contents: str) -> None:
super().__init__(contents=contents.encode("utf-8"))
self.type = Name("Metadata")
self.subtype = Name("XML")
class PDFXObject(PDFContentStream):
__slots__ = ( # RAM usage optimization
"_id",
"_contents",
"filter",
"length",
"type",
"subtype",
"width",
"height",
"color_space",
"bits_per_component",
"decode",
"decode_parms",
"s_mask",
"image_mask",
)
def __init__(
self,
contents: bytes,
subtype: str,
width: float,
height: float,
color_space: PDFArray | Name | None,
bits_per_component: int,
img_filter: Optional[str] = None,
decode: Optional[str] = None,
decode_parms: Optional[str] = None,
image_mask: bool = False,
) -> None:
super().__init__(contents=contents)
self.type = Name("XObject")
self.subtype = Name(subtype)
self.width = width
self.height = height
self.color_space = color_space
self.bits_per_component = bits_per_component
self.filter = Name(img_filter)
self.decode = decode
self.decode_parms = decode_parms
self.s_mask: Optional[PDFXObject] = None
self.image_mask = True if image_mask else None
class PDFICCProfile(PDFContentStream):
"""
Holds values for ICC Profile Stream
Args:
contents (str): stream content
n (int): [1|3|4], # the numbers for colors 1=Gray, 3=RGB, 4=CMYK
alternate (str): ['DeviceGray'|'DeviceRGB'|'DeviceCMYK']
"""
__slots__ = ( # RAM usage optimization
"_id",
"_contents",
"filter",
"length",
"n",
"alternate",
)
def __init__(
self,
contents: bytes,
n: int,
alternate: str,
):
super().__init__(contents=contents, compress=True)
self.n = n
self.alternate = Name(alternate)
class PDFPageLabel:
"""
This will be displayed by some PDF readers to identify pages.
"""
__slots__ = ("_style", "_prefix", "st") # RAM usage optimization
def __init__(
self,
label_style: Optional[PageLabelStyle],
label_prefix: Optional[str],
label_start: Optional[int],
) -> None:
self._style: Optional[PageLabelStyle] = label_style
self._prefix: Optional[str] = label_prefix
self.st: Optional[int] = label_start
@property
def s(self) -> Optional[Name]:
return Name(self._style.value) if self._style else None
@property
def p(self) -> Optional[PDFString]:
return PDFString(self._prefix) if self._prefix else None
def __repr__(self) -> str:
return f"PDFPageLabel({self._style}, {self._prefix}, {self.st})"
def __str__(self) -> str:
ret = self._prefix if self._prefix else ""
if self._style:
if self._style == PageLabelStyle.NUMBER:
ret += str(self.st)
elif self._style == PageLabelStyle.UPPER_ROMAN:
ret += int2roman(self.st or 1)
elif self._style == PageLabelStyle.LOWER_ROMAN:
ret += int2roman(self.st or 1).lower()
elif self._style == PageLabelStyle.UPPER_LETTER:
start = (self.st or 1) - 1
ret += int_to_letters(start)
elif self._style == PageLabelStyle.LOWER_LETTER:
start = (self.st or 1) - 1
ret += int_to_letters(start).lower()
return ret
def serialize(self) -> dict[str, Any]:
return build_obj_dict({key: getattr(self, key) for key in dir(self)})
def get_style(self) -> Optional[PageLabelStyle]:
return self._style
def get_prefix(self) -> str:
return self._prefix or ""
def get_start(self) -> int:
return self.st or 1
class PDFPage(PDFObject):
__slots__ = ( # RAM usage optimization
"_id",
"type",
"contents",
"dur",
"trans",
"annots",
"group",
"media_box",
"struct_parents",
"resources",
"parent",
"_index",
"_width_pt",
"_height_pt",
"_page_label",
"_text_substitution_fragments",
)
def __init__(
self,
duration: Optional[float],
transition: Optional["Transition"],
contents: bytearray | PDFContentStream,
index: int,
):
super().__init__()
self.type = Name("Page")
self.contents = contents
self.dur = duration if duration else None
self.trans = transition
self.annots: Optional[PDFArray] = PDFArray() # list of PDFAnnotation
self.group: Optional[str] = None
self.media_box: Optional[str] = None
self.struct_parents: Optional[int] = None
self.resources: Optional[PDFResources] = (
None # must always be set before calling .serialize()
)
self.parent: Optional[PDFPagesRoot] = (
None # must always be set before calling .serialize()
)
# Useful properties that will not be serialized in the final PDF document:
self._index = index
self._width_pt: Optional[float] = None
self._height_pt: Optional[float] = None
self._page_label: Optional[PDFPageLabel] = None
self._text_substitution_fragments: list[TotalPagesSubstitutionFragment] = []
def index(self) -> int:
return self._index
def set_index(self, i: int) -> None:
self._index = i
def dimensions(self) -> tuple[float, float]:
"Return a pair (width, height) in the unit specified to FPDF constructor"
if self._width_pt is None or self._height_pt is None:
raise ValueError("Page dimensions are null")
return self._width_pt, self._height_pt
def set_dimensions(self, width_pt: float, height_pt: float) -> None:
"Accepts a pair (width, height) in the unit specified to FPDF constructor"
self._width_pt, self._height_pt = width_pt, height_pt
def set_page_label(
self,
previous_page_label: Optional[PDFPageLabel],
page_label: Optional[PDFPageLabel],
) -> None:
if (
previous_page_label
and page_label
and page_label.get_style() == previous_page_label.get_style()
and page_label.get_prefix() == previous_page_label.get_prefix()
and not page_label.st
):
page_label.st = previous_page_label.get_start() + 1
if page_label:
if page_label.st is None or page_label.st == 0:
page_label.st = 1
if previous_page_label and not page_label:
page_label = PDFPageLabel(
previous_page_label.get_style(),
previous_page_label.get_prefix(),
previous_page_label.get_start() + 1,
)
self._page_label = page_label
def get_page_label(self) -> Optional[PDFPageLabel]:
return self._page_label
def get_label(self) -> str:
return str(self.index()) if not self._page_label else str(self._page_label)
def get_text_substitutions(self) -> Sequence[TotalPagesSubstitutionFragment]:
return self._text_substitution_fragments
def add_text_substitution(self, fragment: TotalPagesSubstitutionFragment) -> None:
self._text_substitution_fragments.append(fragment)
def add_annotation(self, annotation: AnnotationDict | PDFAnnotation) -> None:
if self.annots is None:
self.annots = PDFArray()
self.annots.append(annotation)
class PDFPagesRoot(PDFObject):
def __init__(self, count: int, media_box: str) -> None:
super().__init__()
self.type = Name("Pages")
self.count = count
self.media_box = media_box
self.kids: Optional[PDFArray] = (
None # must always be set before calling .serialize()
)
class PDFExtGState(PDFObject):
def __init__(self, dict_as_str: str) -> None:
super().__init__()
self._dict_as_str = dict_as_str
# method override
def serialize(
self,
obj_dict: Optional[dict[str, Any]] = None,
_security_handler: Optional["StandardSecurityHandler"] = None,
) -> str:
return f"{self.id} 0 obj\n{self._dict_as_str}\nendobj"
class PDFXrefAndTrailer(ContentWithoutID):
"Cross-reference table & file trailer"
def __init__(self, output_builder: "OutputProducer") -> None:
self.output_builder = output_builder
self.count = output_builder.obj_id + 1
# Must be set before the call to serialize():
self.catalog_obj: Optional[PDFCatalog] = None
self.info_obj: Optional[PDFInfo] = None
self.encryption_obj: Optional["EncryptionDictionary"] = None
def serialize(
self, _security_handler: Optional["StandardSecurityHandler"] = None
) -> str:
if self.catalog_obj is None:
raise FPDFException("Invalid state for XREF production.")
builder = self.output_builder
startxref = str(len(builder.buffer))
out: list[str] = []
out.append("xref")
out.append(f"0 {self.count}")
out.append("0000000000 65535 f ")
for obj_id in range(1, self.count):
out.append(f"{builder.offsets[obj_id]:010} 00000 n ")
out.append("trailer")
out.append("<<")
out.append(f"/Size {self.count}")
out.append(f"/Root {pdf_ref(self.catalog_obj.id)}")
if self.info_obj:
out.append(f"/Info {pdf_ref(self.info_obj.id)}")
fpdf = builder.fpdf
if self.encryption_obj:
out.append(f"/Encrypt {pdf_ref(self.encryption_obj.id)}")
assert fpdf._security_handler is not None
file_id: Optional[str | Literal[-1]] = fpdf._security_handler.file_id
else:
file_id = fpdf.file_id()
if file_id == -1:
file_id = fpdf._default_file_id(builder.buffer)
if file_id is not None:
out.append(f"/ID [{file_id}]")
out.append(">>")
out.append("startxref")
out.append(startxref)
out.append("%%EOF")
return "\n".join(out)
class OutputIntentDictionary:
"""
The optional OutputIntents (PDF 1.4) entry in the document
catalog dictionary holds an array of output intent dictionaries,
each describing the colour reproduction characteristics of a possible
output device.
Args:
subtype (OutputIntentSubType, required): PDFA, PDFX or ISOPDF
output_condition_identifier (str, required): see the Name in
https://www.color.org/registry.xalter
output_condition (str, optional): see the Definition in
https://www.color.org/registry.xalter
registry_name (str, optional): "https://www.color.org"
dest_output_profile (PDFICCProfile, required/optional):
PDFICCProfile | None # (required if
output_condition_identifier does not specify a standard
production condition; optional otherwise)
info (str, required/optional see dest_output_profile): human
readable description of profile
"""
__slots__ = ( # RAM usage optimization
"type",
"s",
"output_condition_identifier",
"output_condition",
"registry_name",
"dest_output_profile",
"info",
)
def __init__(
self,
subtype: "OutputIntentSubType | str",
output_condition_identifier: Optional[str],
output_condition: Optional[str] = None,
registry_name: Optional[str] = None,
dest_output_profile: Optional[PDFICCProfile] = None,
info: Optional[str] = None,
) -> None:
self.type = Name("OutputIntent")
self.s = Name(OutputIntentSubType.coerce(subtype).value)
self.output_condition_identifier = (
PDFString(output_condition_identifier)
if output_condition_identifier
else None
)
self.output_condition = (
PDFString(output_condition) if output_condition else None
)
self.registry_name = PDFString(registry_name) if registry_name else None
self.dest_output_profile = (
dest_output_profile
if dest_output_profile and isinstance(dest_output_profile, PDFICCProfile)
else None
)
self.info = PDFString(info) if info else None
def serialize(
self,
_security_handler: Optional["StandardSecurityHandler"] = None,
_obj_id: Optional[int] = None,
) -> str:
obj_dict = build_obj_dict(
{key: getattr(self, key) for key in dir(self)},
_security_handler=_security_handler,
_obj_id=_obj_id,
)
return pdf_dict(obj_dict)
ResourceTypes = Union[str, int, Name, "Gradient", "Pattern", "Shading", "MeshShading"]
class ResourceCatalog:
"Manage the indexing of resources and association to the pages they are used"
GS_REGEX = re.compile(r"/(GS\d+) gs")
IMG_REGEX = re.compile(r"/I(\d+) Do")
PATTERN_FILL_REGEX = re.compile(r"/(P\d+)\s+scn")
PATTERN_STROKE_REGEX = re.compile(r"/(P\d+)\s+SCN")
FONT_REGEX = re.compile(r"/F(\d+)\s+[-+]?\d+(?:\.\d+)?\s+Tf")
def __init__(self) -> None:
self.resources: dict[PDFResourceType, dict[ResourceTypes, str]] = defaultdict(
dict
)
self.resources_per_page: dict[
tuple[int, PDFResourceType], set[ResourceTypes]
] = defaultdict(set)
self.graphics_styles: dict[str, Name] = OrderedDict()
self.soft_mask_xobjects: list[PDFContentStream] = []
self.form_xobjects: list[tuple[int, PDFContentStream]] = []
self.last_reserved_object_id: int = 0
self.font_registry: dict[str, CoreFont | TTFFont] = {}
self.next_xobject_index: int = 1
def add(
self,
resource_type: PDFResourceType,
resource: ResourceTypes,
page_number: Optional[int],
) -> Optional[str]:
if resource_type in (PDFResourceType.PATTERN, PDFResourceType.SHADING):
registry = self.resources[resource_type]
prefix = self._get_prefix(resource_type)
if resource not in registry:
registry[resource] = f"{prefix}{len(registry) + 1}"
if page_number is not None:
self.resources_per_page[(page_number, resource_type)].add(
registry[resource]
)
return str(registry[resource])
if (
resource_type == PDFResourceType.X_OBJECT
and isinstance(resource, int)
and resource >= self.next_xobject_index
):
self.next_xobject_index = resource + 1
if TYPE_CHECKING:
assert page_number is not None
self.resources_per_page[(page_number, resource_type)].add(resource)
return None
def register_graphics_style(self, style: "GraphicsStyle") -> Optional[Name]:
"""
Graphics style can be added without associating to a page number right away,
like when rendering a svg image.
The method that adds image to the page will call the add method for the page association.
"""
style_dict: Optional[Raw] = style.serialize()
if style_dict is None: # empty style does not need an entry
return None
style_str = str(style_dict)
if style_str not in self.graphics_styles:
name = Name(
f"{self._get_prefix(PDFResourceType.EXT_G_STATE)}{len(self.graphics_styles)}"
)
self.graphics_styles[style_str] = name
return self.graphics_styles[style_str]
def register_soft_mask(self, soft_mask: PaintSoftMask | ImageSoftMask) -> int:
"""Register a soft mask xobject and return its object id"""
self.last_reserved_object_id += 1
xobject = soft_mask_path_to_xobject(soft_mask, self)
xobject.id = self.last_reserved_object_id
self.soft_mask_xobjects.append(xobject)
return xobject.id
def register_blend_form(self, blend_group: "BlendGroup") -> int:
"""Register a blend group Form XObject and return its resource index."""
xobject = blend_group_to_xobject(blend_group, self)
index = self.next_xobject_index
self.next_xobject_index += 1
self.form_xobjects.append((index, xobject))
return index
def scan_stream(self, rendered: str) -> set[tuple[PDFResourceType, str]]:
"""Parse a content stream and return discovered resources"""
found: set[tuple[PDFResourceType, str]] = set()
for m in self.GS_REGEX.finditer(rendered):
found.add((PDFResourceType.EXT_G_STATE, m.group(1)))
for m in self.IMG_REGEX.finditer(rendered):
found.add((PDFResourceType.X_OBJECT, m.group(1)))
for m in self.PATTERN_FILL_REGEX.finditer(rendered):
found.add((PDFResourceType.PATTERN, m.group(1)))
for m in self.PATTERN_STROKE_REGEX.finditer(rendered):
found.add((PDFResourceType.PATTERN, m.group(1)))
for m in self.FONT_REGEX.finditer(rendered):
found.add((PDFResourceType.FONT, m.group(1)))
return found
def index_stream_resources(self, rendered: str, page_number: int) -> None:
"""
Scan a rendered content stream and register resources used on the given page.
Currently indexes:
- ExtGState invocations: '/GSn gs'
- Image XObjects: '/In Do'
"""
for resource_type, resource in self.scan_stream(rendered):
if resource_type == PDFResourceType.PATTERN:
self.resources_per_page[(page_number, PDFResourceType.PATTERN)].add(
resource
)
else:
self.add(resource_type, resource, page_number)
def get_items(
self, resource_type: PDFResourceType
) -> ItemsView[ResourceTypes, str]:
return self.resources[resource_type].items()
def get_resources_per_page(
self, page_number: int, resource_type: PDFResourceType
) -> set[ResourceTypes]:
return self.resources_per_page[(page_number, resource_type)]
def get_used_resources(self, resource_type: PDFResourceType) -> set[ResourceTypes]:
unique: set[ResourceTypes] = set()
for (_, rtype), resource in self.resources_per_page.items():
if rtype == resource_type:
unique.update(resource)
return unique
@classmethod
def _get_prefix(cls, resource_type: PDFResourceType) -> str:
if resource_type == PDFResourceType.EXT_G_STATE:
return "GS"
if resource_type == PDFResourceType.PATTERN:
return "P"
if resource_type == PDFResourceType.SHADING:
return "Sh"
raise ValueError(f"No prefix for resource type {resource_type}")
def get_font_from_family(
self, font_family: str, font_style: str = ""
) -> CoreFont | TTFFont:
"""
Resolve a family+style to a concrete font instance from the font registry.
Behavior:
- Exact match (family.lower() + style.upper()) in registry: return it
- If `family` names a core font: add CoreFont to registry (if missing) and return it
- If `family` is an alias/generic: translate to a core font, add to registry (if missing), and return it
- Otherwise: raise KeyError
Notes:
- For Symbol/ZapfDingbats, style is forced to "" (they don't support B/I).
"""
if not font_family:
raise KeyError("Empty font family")
style = "".join(sorted(font_style.upper()))
alias = {
# sans
"sans-serif": "helvetica",
"sans serif": "helvetica",
"arial": "helvetica",
"verdana": "helvetica",
"tahoma": "helvetica",
"segoe ui": "helvetica",
# serif
"serif": "times",
"times": "times",
"times new roman": "times",
"georgia": "times",
"cambria": "times",
"garamond": "times",
# mono
"monospace": "courier",
"courier": "courier",
"courier new": "courier",
"consolas": "courier",
"monaco": "courier",
# symbol
"symbol": "symbol",
"zapfdingbats": "zapfdingbats",
"zapf dingbats": "zapfdingbats",
}
for candidate in font_family.strip().strip("'\"").split(","):
family = candidate.strip().strip("'\"").lower()
# 1) Exact match
fontkey = f"{family}{style}"
if fontkey in self.font_registry:
return self.font_registry[fontkey]
# 2) Core-family direct hit?
if family in CORE_FONTS:
core_style = "" if family in {"symbol", "zapfdingbats"} else style
key = f"{family}{core_style}"
if key not in self.font_registry:
i = len(self.font_registry) + 1
self.font_registry[key] = CoreFont(i, key, core_style)
return self.font_registry[key]
# 3) Alias / generic mapping to core font
mapped = alias.get(family)
if mapped:
core_style = "" if mapped in {"symbol", "zapfdingbats"} else style
key = f"{mapped}{core_style}"
if key not in self.font_registry:
i = len(self.font_registry) + 1
self.font_registry[key] = CoreFont(i, key, core_style)
return self.font_registry[key]
# 4) Fail: do not return anything
raise KeyError(f"No suitable font for family={font_family!r}, style={style!r}")
class OutputProducer:
"Generates the final bytearray representing the PDF document, based on a FPDF instance."
def __init__(self, fpdf: "FPDF") -> None:
self.fpdf = fpdf
self.pdf_objs: list[PDFObject | ContentWithoutID] = []
self.iccp_i_to_pdf_i: dict[int, int] = {}
self.obj_id: int = (
fpdf._resource_catalog.last_reserved_object_id
) # current PDF object number
# array of PDF object offsets in self.buffer, used to build the xref table:
self.offsets: dict[int, int] = {}
self.trace_labels_per_obj_id: dict[int, str] = {}
self.sections_size_per_trace_label: dict[str, int] = defaultdict(int)
self.buffer: bytearray = bytearray() # resulting output buffer
def bufferize(self) -> bytearray:
"""
This method alters the target FPDF instance
by assigning IDs to all PDF objects,
plus a few other properties on PDFPage instances
"""
fpdf = self.fpdf
# 1. setup - Insert all PDF objects
# and assign unique consecutive numeric IDs to all of them
if fpdf._security_handler is not None:
# get the file_id and generate passwords needed to encrypt streams and strings
file_id: Optional[str | Literal[-1]] = fpdf.file_id()
if file_id == -1:
# no custom file id - use default file id so encryption passwords can be generated
file_id = fpdf._default_file_id(bytearray(0x00))
fpdf._security_handler.generate_passwords(str(file_id))
pdf_version = fpdf.pdf_version
if (
fpdf.viewer_preferences
and fpdf.viewer_preferences._min_pdf_version > pdf_version
):
pdf_version = fpdf.viewer_preferences._min_pdf_version
self.pdf_objs.append(PDFHeader(pdf_version))
pages_root_obj = self._add_pages_root()
catalog_obj = self._add_catalog()
page_objs = self._add_pages()
sig_annotation_obj = self._add_annotations_as_objects()
for embedded_file in fpdf.embedded_files:
self._add_pdf_obj(embedded_file, "embedded_files")
self._add_pdf_obj(embedded_file.file_spec(), "file_spec")
self._insert_resources(page_objs)
struct_tree_root_obj = self._add_structure_tree()
outline_dict_obj, outline_items = self._add_document_outline()
xmp_metadata_obj = self._add_xmp_metadata()
info_obj = None
if not fpdf._compliance:
info_obj = self._add_info()
encryption_obj = self._add_encryption()
xref = PDFXrefAndTrailer(self)
self.pdf_objs.append(xref)
# 2. Plumbing - Inject all PDF object references required:
pages_root_obj.kids = PDFArray(page_objs)
self._finalize_catalog(
catalog_obj,
pages_root_obj=pages_root_obj,
first_page_obj=page_objs[0],
sig_annotation_obj=sig_annotation_obj,
xmp_metadata_obj=xmp_metadata_obj,
struct_tree_root_obj=struct_tree_root_obj,
outline_dict_obj=outline_dict_obj,
)
dests: list[DestinationXYZ] = []
for page_obj in page_objs:
page_obj.parent = pages_root_obj
assert isinstance(page_obj.annots, PDFArray)
for annot in page_obj.annots:
page_dests: list[DestinationXYZ] = []
if annot.dest:
# Only add to page_dests if it's a Destination object (not a string/PDFString)
if hasattr(annot.dest, "page_number"):
page_dests.append(annot.dest)
if annot.a and hasattr(annot.a, "dest"):
# Only add to page_dests if it's a Destination object (not a string/PDFString)
if hasattr(annot.a.dest, "page_number"):
page_dests.append(annot.a.dest)
for dest in page_dests:
if dest.page_number > len(page_objs):
raise ValueError(
f"Invalid reference to non-existing page {dest.page_number} present on page {page_obj.index()}: "
)
dests.extend(page_dests)
if not page_obj.annots:
# Avoid serializing an empty PDFArray:
page_obj.annots = None
for outline_item in outline_items:
if outline_item.dest is not None:
dests.append(outline_item.dest)
# Assigning the .page_ref property of all Destination objects:
for dest in dests:
dest.page_ref = pdf_ref(
page_objs[
dest.page_number - 1
].id # pyright: ignore[reportUnknownArgumentType]
)
for struct_elem in fpdf.struct_builder.doc_struct_elem.k:
struct_elem.pg = page_objs[struct_elem.page_number() - 1]
xref.catalog_obj = catalog_obj
xref.info_obj = info_obj
xref.encryption_obj = encryption_obj
# 3. Serializing - Append all PDF objects to the buffer:
assert (
not self.buffer
), f"Nothing should have been appended to the .buffer at this stage: {self.buffer}"
assert (
not self.offsets
), f"No offset should have been set at this stage: {len(self.offsets)}"
for pdf_obj in self.pdf_objs:
if isinstance(pdf_obj, ContentWithoutID):
# top header, xref table & trailer:
trace_label = None
else:
self.offsets[pdf_obj.id] = len(self.buffer)
trace_label = self.trace_labels_per_obj_id.get(pdf_obj.id)
if trace_label:
with self._trace_size(trace_label):
self._out(
pdf_obj.serialize(_security_handler=fpdf._security_handler)
)
else:
self._out(pdf_obj.serialize(_security_handler=fpdf._security_handler))
self._log_final_sections_sizes()
if fpdf._sign_key:
self.buffer = sign_content(
signer, # pyright: ignore[reportArgumentType]
self.buffer,
fpdf._sign_key,
fpdf._sign_cert,
fpdf._sign_extra_certs,
fpdf._sign_hashalgo,
fpdf._sign_time,
)
return self.buffer
def _out(self, data: bytes | bytearray | str) -> None:
"Append data to the buffer"
if not isinstance(data, bytes):
if not isinstance(data, str):
data = str(data)
data = data.encode("latin1")
self.buffer += data + b"\n"
def _add_pdf_obj(
self, pdf_obj: PDFObject, trace_label: Optional[str] = None
) -> int:
self.obj_id += 1
pdf_obj.id = self.obj_id
self.pdf_objs.append(pdf_obj)
if trace_label:
self.trace_labels_per_obj_id[self.obj_id] = trace_label
return self.obj_id
def _add_pages_root(self) -> PDFPagesRoot:
fpdf = self.fpdf
pages_root_obj = PDFPagesRoot(
count=fpdf.pages_count,
media_box=_dimensions_to_mediabox(fpdf.default_page_dimensions),
)
self._add_pdf_obj(pages_root_obj)
return pages_root_obj
def _iter_pages_in_order(self) -> Iterator[PDFPage]:
for page_index in range(1, self.fpdf.pages_count + 1):
page_obj = self.fpdf.pages[page_index]
# Defensive check:
assert (
page_obj.index() == page_index
), f"{page_obj.index()=} != {page_index=}"
yield page_obj
def _add_pages(self, _slice: slice = slice(0, None)) -> list[PDFPage]:
fpdf = self.fpdf
page_objs: list[PDFPage] = []
for page_obj in list(self._iter_pages_in_order())[_slice]:
if fpdf.pdf_version > "1.3" and fpdf.allow_images_transparency:
page_obj.group = pdf_dict(
{"/Type": "/Group", "/S": "/Transparency", "/CS": "/DeviceRGB"},
field_join=" ",
)
if page_obj.dimensions() != fpdf.default_page_dimensions:
page_obj.media_box = _dimensions_to_mediabox(page_obj.dimensions())
self._add_pdf_obj(page_obj, "pages")
page_objs.append(page_obj)
# Extracting the page contents to insert it as a content stream:
assert isinstance(page_obj.contents, bytearray)
cs_obj = PDFContentStream(
contents=page_obj.contents, compress=fpdf.compress
)
self._add_pdf_obj(cs_obj, "pages")
page_obj.contents = cs_obj
return page_objs
def _add_annotations_as_objects(self) -> Optional[PDFAnnotation]:
sig_annotation_obj = None
for page_obj in self.fpdf.pages.values():
assert isinstance(page_obj.annots, PDFArray)
for annot_obj in page_obj.annots:
if isinstance(annot_obj, PDFAnnotation): # distinct from AnnotationDict
self._add_pdf_obj(annot_obj)
if isinstance(annot_obj.v, Signature):
assert (
sig_annotation_obj is None
), "A /Sig annotation is present on more than 1 page"
sig_annotation_obj = annot_obj
return sig_annotation_obj
def _add_fonts(
self,
image_objects_per_index: dict[int, PDFXObject],
gfxstate_objs_per_name: dict[str, PDFExtGState],
pattern_objs_per_name: dict[str, "Pattern"],
) -> dict[int, PDFFont | PDFType3Font]:
font_objs_per_index: dict[int, PDFFont | PDFType3Font] = {}
for font in sorted(self.fpdf.fonts.values(), key=lambda font: font.i):
# type 3 font
if isinstance(font, TTFFont) and font.color_font:
if font.subset._next > 0xFF:
raise FPDFException(
"Type 3 fonts with color glyphs are not supported is more than 255 glyphs are rendered. "
"set FPDF.render_color_fonts=False or use less color glyphs."
)
for color_glyph in font.color_font.glyphs:
color_glyph.obj_id = self._add_pdf_obj(
PDFContentStream(
contents=color_glyph.glyph.encode("latin-1"),
compress=self.fpdf.compress,
),
"fonts",
)
bfChar: list[str] = []
for glyph, code_mapped in font.subset.items():
if (
glyph is None
or not isinstance(glyph.unicode, tuple)
or len(glyph.unicode) == 0
):
continue
bfChar.append(
f'<{code_mapped:02X}> <{"".join(chr(code).encode("utf-16-be").hex().upper() for code in glyph.unicode)}>\n'
)
to_unicode_obj = PDFContentStream(
(
"/CIDInit /ProcSet findresource begin\n"
"12 dict begin\n"
"begincmap\n"
"/CIDSystemInfo\n"
"<</Registry (Adobe)\n"
"/Ordering (UCS)\n"
"/Supplement 0\n"
">> def\n"
"/CMapName /Adobe-Identity-UCS def\n"
"/CMapType 2 def\n"
"1 begincodespacerange\n"
"<00> <FF>\n"
"endcodespacerange\n"
f"{len(bfChar)} beginbfchar\n"
f"{''.join(bfChar)}"
"endbfchar\n"
"endcmap\n"
"CMapName currentdict /CMap defineresource pop\n"
"end\n"
"end"
).encode("latin-1")
)
self._add_pdf_obj(to_unicode_obj, "fonts")
t3_font_obj = PDFType3Font(font.color_font)
t3_font_obj.to_unicode = pdf_ref(to_unicode_obj.id)
t3_font_obj.generate_resources(
image_objects_per_index,
gfxstate_objs_per_name,
pattern_objs_per_name,
)
self._add_pdf_obj(t3_font_obj, "fonts")
font_objs_per_index[font.i] = t3_font_obj
continue
# Standard font
if isinstance(font, CoreFont):
encoding = (
"WinAnsiEncoding"
if font.name not in ("Symbol", "ZapfDingbats")
else None
)
core_font_obj = PDFFont(
subtype="Type1", base_font=font.name, encoding=encoding
)
self._add_pdf_obj(core_font_obj, "fonts")
font_objs_per_index[font.i] = core_font_obj
elif isinstance(font, TTFFont):
fontname = f"MPDFAA+{font.name}"
# 1. get all glyphs in PDF
glyph_names = font.subset.get_all_glyph_names()
if len(font.missing_glyphs) > 0:
msg = ", ".join(
f"'{chr(x)}' ({chr(x).encode('unicode-escape').decode()})"
for x in font.missing_glyphs[:10]
)
if len(font.missing_glyphs) > 10:
msg += f", ... (and {len(font.missing_glyphs) - 10} others)"
LOGGER.warning(
"Font %s is missing the following glyphs: %s", fontname, msg
)
# 2. make a subset
# notdef_outline=True means that keeps the white box for the .notdef glyph
# recommended_glyphs=True means that adds the .notdef, .null, CR, and space glyphs
options = ftsubset.Options(notdef_outline=True, recommended_glyphs=True)
# dropping some tables that currently not used:
options.drop_tables += [
"FFTM", # FontForge Timestamp table - cf. https://github.com/py-pdf/fpdf2/issues/600
"GDEF", # Glyph Definition table = various glyph properties used in OpenType layout processing
"GPOS", # Glyph Positioning table = precise control over glyph placement
# for sophisticated text layout and rendering in each script and language system
"GSUB", # Glyph Substitution table = data for substitution of glyphs for appropriate rendering of scripts
"MATH", # Mathematical typesetting table = specific information necessary for math formula layout
"hdmx", # Horizontal Device Metrics table, stores integer advance widths scaled to particular pixel sizes
# for OpenType™ fonts with TrueType outlines
"meta", # metadata table
"sbix", # Apple's SBIX table, used for color bitmap glyphs
"CBDT", # Color Bitmap Data Table
"CBLC", # Color Bitmap Location Table
"EBDT", # Embedded Bitmap Data Table
"EBLC", # Embedded Bitmap Location Table
"EBSC", # Embedded Bitmap Scaling Table
"SVG ", # SVG table
"CPAL", # Color Palette table
"COLR", # Color table
]
subsetter = ftsubset.Subsetter(options)
subsetter.populate(glyphs=glyph_names)
subsetter.subset(font.ttfont)
# 3. make codeToGlyph
# is a map Character_ID -> Glyph_ID
# it's used for associating glyphs to new codes
# this basically takes the old code of the character
# take the glyph associated with it
# and then associate to the new code the glyph associated with the old code
code_to_glyph: dict[int, int] = {
char_id: font.ttfont.getGlyphID(glyph.glyph_name)
for glyph, char_id in font.subset.items()
if glyph is not None
}
# 4. return the ttfile
output = BytesIO()
font.ttfont.save(output)
output.seek(0)
ttfontstream = output.read()
# A composite font - a font composed of other fonts,
# organized hierarchically
composite_font_obj = PDFFont(
subtype="Type0", base_font=fontname, encoding="Identity-H"
)
self._add_pdf_obj(composite_font_obj, "fonts")
font_objs_per_index[font.i] = composite_font_obj
# A CIDFont whose glyph descriptions are based on TrueType or CFF technology
is_cff_cid = font.is_cff and font.is_cid_keyed
code_to_cid: Optional[dict[int, int]] = None
cid_widths: Optional[dict[int, int]] = None
if is_cff_cid:
code_to_cid = {}
cid_widths = {}
for glyph, code_mapped in font.subset.items():
if glyph is None:
continue
if (
glyph.glyph_name.startswith("cid")
and glyph.glyph_name[3:].isdigit()
):
cid = int(glyph.glyph_name[3:])
else:
cid = glyph.glyph_id
if cid > 0xFFFF:
LOGGER.warning(
"Glyph CID %s exceeds 0xFFFF and cannot be encoded in a 2-byte CID font: %s",
cid,
font.fontkey,
)
continue
code_to_cid[code_mapped] = cid
cid_widths[cid] = glyph.glyph_width
cid_font_obj = PDFFont(
subtype="CIDFontType0" if is_cff_cid else "CIDFontType2",
base_font=fontname,
d_w=font.desc.missing_width,
w=(
_cid_font_widths(cid_widths)
if is_cff_cid and cid_widths
else _tt_font_widths(font)
),
)
self._add_pdf_obj(cid_font_obj, "fonts")
composite_font_obj.descendant_fonts = PDFArray([cid_font_obj])
# bfChar
# This table informs the PDF reader about the unicode
# character that each used 16-bit code belongs to. It
# allows searching the file and copying text from it.
bfChar = []
def format_code(unicode: int) -> str:
if unicode > 0xFFFF:
# Calculate surrogate pair
code_high = 0xD800 | (unicode - 0x10000) >> 10
code_low = 0xDC00 | (unicode & 0x3FF)
return f"{code_high:04X}{code_low:04X}"
return f"{unicode:04X}"
for glyph, code_mapped in font.subset.items():
if (
glyph is None
or not isinstance(glyph.unicode, tuple)
or len(glyph.unicode) == 0
):
continue
bfChar.append(
f'<{code_mapped:04X}> <{"".join(format_code(code) for code in glyph.unicode)}>\n'
)
to_unicode_obj = PDFContentStream(
(
"/CIDInit /ProcSet findresource begin\n"
"12 dict begin\n"
"begincmap\n"
"/CIDSystemInfo\n"
"<</Registry (Adobe)\n"
"/Ordering (UCS)\n"
"/Supplement 0\n"
">> def\n"
"/CMapName /Adobe-Identity-UCS def\n"
"/CMapType 2 def\n"
"1 begincodespacerange\n"
"<0000> <FFFF>\n"
"endcodespacerange\n"
f"{len(bfChar)} beginbfchar\n"
f"{''.join(bfChar)}"
"endbfchar\n"
"endcmap\n"
"CMapName currentdict /CMap defineresource pop\n"
"end\n"
"end"
).encode("latin-1")
)
self._add_pdf_obj(to_unicode_obj, "fonts")
composite_font_obj.to_unicode = to_unicode_obj
if is_cff_cid and code_to_cid:
registry = "Adobe"
ordering = "Identity"
supplement = 0
if font.cff_ros:
registry, ordering, supplement = font.cff_ros
cid_mapping = [
f"<{code:04X}> {cid}\n"
for code, cid in sorted(code_to_cid.items())
]
encoding_cmap_obj = PDFContentStream(
(
"/CIDInit /ProcSet findresource begin\n"
"12 dict begin\n"
"begincmap\n"
"/CIDSystemInfo\n"
f"<</Registry ({registry})\n"
f"/Ordering ({ordering})\n"
f"/Supplement {supplement}\n"
">> def\n"
f"/CMapName /{registry}-{ordering}-UCS def\n"
"/CMapType 2 def\n"
"1 begincodespacerange\n"
"<0000> <FFFF>\n"
"endcodespacerange\n"
f"{len(cid_mapping)} begincidchar\n"
f"{''.join(cid_mapping)}"
"endcidchar\n"
"endcmap\n"
"CMapName currentdict /CMap defineresource pop\n"
"end\n"
"end"
).encode("latin-1")
)
encoding_cmap_obj.type = Name("CMap") # type: ignore[attr-defined]
encoding_cmap_obj.c_map_name = Name( # type: ignore[attr-defined]
f"{registry}-{ordering}-UCS"
)
encoding_cmap_obj.c_i_d_system_info = Raw( # type: ignore[attr-defined]
f"<< /Registry ({registry}) /Ordering ({ordering}) /Supplement {supplement} >>"
)
self._add_pdf_obj(encoding_cmap_obj, "fonts")
composite_font_obj.encoding = encoding_cmap_obj # type: ignore[assignment]
cid_system_info_obj = CIDSystemInfo()
if is_cff_cid and font.cff_ros:
registry, ordering, supplement = font.cff_ros
cid_system_info_obj.registry = PDFString(registry, encrypt=True)
cid_system_info_obj.ordering = PDFString(ordering, encrypt=True)
cid_system_info_obj.supplement = supplement
self._add_pdf_obj(cid_system_info_obj, "fonts")
cid_font_obj.c_i_d_system_info = cid_system_info_obj
font_descriptor_obj = font.desc
font_descriptor_obj.font_name = Name(fontname)
self._add_pdf_obj(font_descriptor_obj, "fonts")
cid_font_obj.font_descriptor = font_descriptor_obj
if not is_cff_cid:
# Embed CIDToGIDMap
# A specification of the mapping from CIDs to glyph indices
cid_to_gid_list = ["\x00"] * 256 * 256 * 2
for cc, glyph_i in code_to_glyph.items():
cid_to_gid_list[cc * 2] = chr(glyph_i >> 8)
cid_to_gid_list[cc * 2 + 1] = chr(glyph_i & 0xFF)
cid_to_gid_map = "".join(cid_to_gid_list)
# manage binary data as latin1 until PEP461-like function is implemented
cid_to_gid_map_obj = PDFContentStream(
contents=cid_to_gid_map.encode("latin1"), compress=True
)
self._add_pdf_obj(cid_to_gid_map_obj, "fonts")
cid_font_obj.c_i_d_to_g_i_d_map = cid_to_gid_map_obj
font_file_cs_obj = PDFFontStream(contents=ttfontstream)
if is_cff_cid:
font_file_cs_obj.subtype = Name("CIDFontType0C") # type: ignore[attr-defined]
self._add_pdf_obj(font_file_cs_obj, "fonts")
if is_cff_cid:
font_descriptor_obj.font_file3 = font_file_cs_obj # type: ignore[attr-defined]
else:
font_descriptor_obj.font_file2 = font_file_cs_obj # type: ignore[attr-defined]
font.subset.pick.cache_clear()
font.subset.get_glyph.cache_clear()
font.close()
return font_objs_per_index
def _add_images(self) -> dict[int, PDFXObject]:
img_objs_per_index: dict[int, PDFXObject] = {}
for img in sorted(
self.fpdf.image_cache.images.values(), key=lambda img: cast(int, img["i"])
):
if cast(int, img["usages"]) > 0:
img_objs_per_index[cast(int, img["i"])] = self._add_image(img)
return img_objs_per_index
def _ensure_iccp(self, img_info: dict[str, object]) -> int:
"""
Returns the PDF object of the ICC profile indexed iccp_i in the FPDF object.
Adds it if not present.
"""
iccp_i = cast(int, img_info["iccp_i"])
if iccp_i in self.iccp_i_to_pdf_i:
return self.iccp_i_to_pdf_i[iccp_i]
iccp_content = None
for iccp_c, i in self.fpdf.image_cache.icc_profiles.items():
if iccp_i == i:
iccp_content = iccp_c
break
assert iccp_content is not None
# Note: n should be 4 if the profile ColorSpace is CMYK
iccp_obj = PDFICCProfile(
contents=iccp_content,
n=cast(int, img_info["dpn"]),
alternate=cast(str, img_info["cs"]),
)
iccp_pdf_i = self._add_pdf_obj(iccp_obj, "iccp")
self.iccp_i_to_pdf_i[iccp_i] = iccp_pdf_i
return iccp_pdf_i
def _add_image(self, info: dict[str, object]) -> PDFXObject:
image_mask = bool(info.get("image_mask"))
color_space: Name | PDFArray | None = None if image_mask else Name(info["cs"])
decode = None
iccp_i = None if image_mask else info.get("iccp_i")
if color_space == "Indexed":
color_space = PDFArray(
["/Indexed", "/DeviceRGB", f"{len(info['pal']) // 3 - 1}"] # type: ignore[arg-type]
)
elif iccp_i is not None:
iccp_pdf_i = self._ensure_iccp(info)
color_space = PDFArray(["/ICCBased", str(iccp_pdf_i), str("0"), "R"])
elif color_space == "DeviceCMYK":
if info["inverted"] is True:
decode = "[1 0 1 0 1 0 1 0]"
if "decode" in info:
decode = cast(str, info["decode"])
decode_parms = f"<<{info['dp']} /BitsPerComponent {info['bpc']}>>"
img_obj = PDFXObject(
subtype="Image",
contents=cast(bytes, info["data"]),
width=cast(int, info["w"]),
height=cast(int, info["h"]),
color_space=color_space,
bits_per_component=cast(int, info["bpc"]),
img_filter=cast(str, info["f"]),
decode=decode,
decode_parms=decode_parms,
image_mask=image_mask,
)
info["obj_id"] = self._add_pdf_obj(img_obj, "images")
# Soft mask
if self.fpdf.allow_images_transparency and "smask" in info and not image_mask:
dp = f"/Predictor 15 /Colors 1 /Columns {info['w']}"
img_obj.s_mask = self._add_image(
{
"w": info["w"],
"h": info["h"],
"cs": "DeviceGray",
"bpc": 8,
"f": info["f"],
"dp": dp,
"data": info["smask"],
}
)
# Palette
if isinstance(color_space, PDFArray) and "/Indexed" in color_space:
assert isinstance(img_obj.color_space, PDFArray)
pal_cs_obj = PDFContentStream(
contents=cast(bytes, info["pal"]), compress=self.fpdf.compress
)
self._add_pdf_obj(pal_cs_obj, "images")
img_obj.color_space.append(pdf_ref(pal_cs_obj.id))
return img_obj
def _add_gfxstates(self) -> dict[str, PDFExtGState]:
gfxstate_objs_per_name: dict[str, PDFExtGState] = OrderedDict()
for state_dict, name in self.fpdf._resource_catalog.graphics_styles.items():
gfxstate_obj = PDFExtGState(state_dict)
self._add_pdf_obj(gfxstate_obj, "gfxstate")
gfxstate_objs_per_name[name] = gfxstate_obj
return gfxstate_objs_per_name
def _add_soft_masks(
self,
gfxstate_objs_per_name: dict[str, PDFExtGState],
pattern_objs_per_name: dict[str, "Pattern"],
img_objs_per_index: dict[int, PDFXObject],
) -> None:
"""Append soft-mask Form XObjects after patterns exist so we can resolve /Pattern ids."""
for soft_mask in self.fpdf._resource_catalog.soft_mask_xobjects:
soft_mask.resources = soft_mask._path.get_resource_dictionary( # type: ignore[attr-defined]
gfxstate_objs_per_name, pattern_objs_per_name, img_objs_per_index
)
self.pdf_objs.append(soft_mask)
def _register_form_xobject_placeholders(
self, img_objs_per_index: dict[int, PDFXObject]
) -> None:
"""Ensure isolated blend forms are part of the XObject set before other resources rely on them."""
for index, xobject in self.fpdf._resource_catalog.form_xobjects:
if not getattr(xobject, "_registered", False):
self._add_pdf_obj(xobject, "images")
xobject._registered = True # type: ignore[attr-defined]
img_objs_per_index.setdefault(index, xobject) # type: ignore[arg-type]
def _finalize_form_xobjects(
self,
img_objs_per_index: dict[int, PDFXObject],
gfxstate_objs_per_name: dict[str, PDFExtGState],
pattern_objs_per_name: dict[str, Pattern],
shading_objs_per_name: dict[str, Shading | MeshShading],
font_objs_per_index: dict[int, PDFFont | PDFType3Font],
) -> None:
"""Populate resource dictionaries for isolated blend Form XObjects."""
for _, xobject in self.fpdf._resource_catalog.form_xobjects:
blend_group = getattr(xobject, "_blend_group", None)
if blend_group is not None:
xobject.resources = blend_group.get_resource_dictionary( # type: ignore[attr-defined]
gfxstate_objs_per_name,
pattern_objs_per_name,
shading_objs_per_name,
font_objs_per_index,
img_objs_per_index,
)
def _add_shadings(self) -> dict[str, Shading | MeshShading]:
shading_objs_per_name: dict[str, Shading | MeshShading] = OrderedDict()
for shading, name in self.fpdf._resource_catalog.get_items(
PDFResourceType.SHADING
):
assert isinstance(shading, (Gradient, Shading, MeshShading))
for function in shading.get_functions():
self._add_pdf_obj(function, "function")
shading_obj: Shading | MeshShading = shading.get_shading_object()
self._add_pdf_obj(shading_obj, "shading")
shading_objs_per_name[name] = shading_obj
return shading_objs_per_name
def _add_patterns(self) -> dict[str, Pattern]:
pattern_objs_per_name: dict[str, Pattern] = OrderedDict()
for pattern, name in self.fpdf._resource_catalog.get_items(
PDFResourceType.PATTERN
):
assert isinstance(pattern, Pattern)
self._add_pdf_obj(pattern, "pattern")
pattern_objs_per_name[name] = pattern
if pattern.get_apply_page_ctm():
pattern.set_matrix(
pattern.get_matrix()
@ Transform.translation(0, -self.fpdf.h)
.scale(x=1, y=-1)
.scale(self.fpdf.k)
)
return pattern_objs_per_name
def _insert_resources(self, page_objs: list[PDFPage]) -> None:
img_objs_per_index = self._add_images()
self._register_form_xobject_placeholders(img_objs_per_index)
gfxstate_objs_per_name = self._add_gfxstates()
pattern_objs_per_name = self._add_patterns()
font_objs_per_index = self._add_fonts(
img_objs_per_index, gfxstate_objs_per_name, pattern_objs_per_name
)
shading_objs_per_name = self._add_shadings()
self._finalize_form_xobjects(
img_objs_per_index,
gfxstate_objs_per_name,
pattern_objs_per_name,
shading_objs_per_name,
font_objs_per_index,
)
self._add_soft_masks(
gfxstate_objs_per_name, pattern_objs_per_name, img_objs_per_index
)
# Insert /Resources dicts:
if self.fpdf.single_resources_object:
resources_dict_obj = self._add_resources_dict(
font_objs_per_index,
img_objs_per_index,
gfxstate_objs_per_name,
shading_objs_per_name,
pattern_objs_per_name,
)
for page_obj in page_objs:
page_obj.resources = resources_dict_obj
else:
for page_number, page_obj in enumerate(page_objs, start=1):
page_font_objs_per_index = {
int(font_id): font_objs_per_index[int(font_id)] # type: ignore[arg-type]
for font_id in self.fpdf._resource_catalog.get_resources_per_page(
page_number, PDFResourceType.FONT
)
}
page_img_objs_per_index = {
int(img_id): img_objs_per_index[int(img_id)] # type: ignore[arg-type]
for img_id in self.fpdf._resource_catalog.get_resources_per_page(
page_number, PDFResourceType.X_OBJECT
)
}
page_gfxstate_objs_per_name = {
gfx_name: gfx_state
for (gfx_name, gfx_state) in gfxstate_objs_per_name.items()
if gfx_name
in self.fpdf._resource_catalog.get_resources_per_page(
page_number, PDFResourceType.EXT_G_STATE
)
}
page_shading_objs_per_name = {
str(shading_name): shading_objs_per_name[str(shading_name)]
for shading_name in self.fpdf._resource_catalog.get_resources_per_page(
page_number, PDFResourceType.SHADING
)
}
page_pattern_objs_per_name = {
str(pattern_name): pattern_objs_per_name[str(pattern_name)]
for pattern_name in self.fpdf._resource_catalog.get_resources_per_page(
page_number, PDFResourceType.PATTERN
)
}
page_obj.resources = self._add_resources_dict(
page_font_objs_per_index,
page_img_objs_per_index,
page_gfxstate_objs_per_name,
page_shading_objs_per_name,
page_pattern_objs_per_name,
)
def _add_resources_dict(
self,
font_objs_per_index: dict[int, PDFFont | PDFType3Font],
img_objs_per_index: dict[int, PDFXObject],
gfxstate_objs_per_name: dict[str, PDFExtGState],
shading_objs_per_name: dict[str, Shading | MeshShading],
pattern_objs_per_name: dict[str, Pattern],
) -> PDFResources:
# From section 10.1, "Procedure sets", of PDF 1.7 spec:
# > Beginning with PDF 1.4, this feature is considered obsolete.
# > For compatibility with existing consumer applications,
# > PDF producer applications should continue to specify procedure sets
# > (preferably, all of those listed in Table 10.1).
proc_set = "[/PDF /Text /ImageB /ImageC /ImageI]"
font, x_object, ext_g_state, shading, pattern = None, None, None, None, None
if font_objs_per_index:
font = pdf_dict(
{
f"/F{index}": pdf_ref(font_obj.id)
for index, font_obj in sorted(font_objs_per_index.items())
}
)
if img_objs_per_index:
x_object = pdf_dict(
{
f"/I{index}": pdf_ref(img_obj.id)
for index, img_obj in sorted(img_objs_per_index.items())
}
)
if gfxstate_objs_per_name:
ext_g_state = pdf_dict(
{
f"/{name}": pdf_ref(gfxstate_obj.id)
for name, gfxstate_obj in gfxstate_objs_per_name.items()
}
)
if shading_objs_per_name:
shading = pdf_dict(
{
f"/{name}": pdf_ref(shading_obj.id)
for name, shading_obj in sorted(shading_objs_per_name.items())
}
)
if pattern_objs_per_name:
pattern = pdf_dict(
{
f"/{name}": pdf_ref(pattern_obj.id)
for name, pattern_obj in sorted(pattern_objs_per_name.items())
}
)
resources_obj = PDFResources(
proc_set=proc_set,
font=font,
x_object=x_object,
ext_g_state=ext_g_state,
shading=shading,
pattern=pattern,
)
self._add_pdf_obj(resources_obj)
return resources_obj
def _add_structure_tree(self) -> Optional[PDFObject]:
"Builds a Structure Hierarchy, including image alternate descriptions"
if self.fpdf.struct_builder.empty():
return None
struct_tree_root_obj = None
for pdf_obj in self.fpdf.struct_builder:
if struct_tree_root_obj is None:
struct_tree_root_obj = pdf_obj
self._add_pdf_obj(pdf_obj, "structure_tree")
return struct_tree_root_obj
def _add_document_outline(
self,
) -> tuple[Optional[OutlineDictionary], Sequence[OutlineItemDictionary]]:
if not self.fpdf._outline:
return None, ()
outline_dict_obj: Optional[OutlineDictionary] = None
outline_items: list[OutlineItemDictionary] = []
for pdf_obj in build_outline_objs(self.fpdf._outline):
if isinstance(pdf_obj, OutlineDictionary):
outline_dict_obj = pdf_obj
else:
outline_items.append(pdf_obj)
self._add_pdf_obj(pdf_obj, "document_outline")
return outline_dict_obj, outline_items
def _add_xmp_metadata(self) -> Optional[PDFXmpMetadata]:
# Prefer explicitly provided XMP (user-supplied inner <x:xmpmeta/> without xpacket):
xmp_src = self.fpdf.xmp_metadata
# If not provided but a PDF/A document is being created, synthesize it:
if not xmp_src and self.fpdf._compliance:
xmp_src = self._build_xmp_from_info()
if not xmp_src:
return None
xpacket = f'<?xpacket begin="{chr(0xFEFF)}" id="W5M0MpCehiHzreSzNTczkc9d"?>\n{xmp_src}\n<?xpacket end="w"?>\n'
pdf_obj = PDFXmpMetadata(xpacket)
self._add_pdf_obj(pdf_obj)
return pdf_obj
def _build_xmp_from_info(self) -> str:
title = getattr(self.fpdf, "title", None) or ""
subject = getattr(self.fpdf, "subject", None) or ""
author = getattr(self.fpdf, "author", None) or ""
if author and isinstance(author, str):
author = [author]
keywords = getattr(self.fpdf, "keywords", None) or ""
if keywords and isinstance(keywords, str):
keywords = [keywords]
creator_tool = getattr(self.fpdf, "creator", None) or ""
producer = getattr(self.fpdf, "producer", None) or ""
cdate = getattr(self.fpdf, "creation_date", None)
creation_date_utc = None
if isinstance(cdate, datetime):
creation_date_utc = cdate if cdate.tzinfo else cdate.astimezone()
creation_date_utc = creation_date_utc.astimezone(timezone.utc)
pdfa = self.fpdf._compliance
# Escape for XML attributes/PCDATA:
def esc(s: str) -> str:
"""Return XML-escaped text suitable for XMP (attributes or text nodes)."""
value = "" if s is None else _html_escape(str(s), quote=True)
return value.replace("'", "'")
# XMP times are ISO 8601 (e.g., 2025-09-01T12:34:56+02:00):
EPOCH = datetime(1969, 12, 31, 19, 0, 0, tzinfo=timezone.utc)
now = datetime.now(timezone.utc)
if creation_date_utc == EPOCH:
xmp_create = EPOCH.isoformat(timespec="seconds")
xmp_modify = EPOCH.isoformat(timespec="seconds")
else:
create_dt = creation_date_utc or now
xmp_create = create_dt.isoformat(timespec="seconds")
xmp_modify = now.isoformat(timespec="seconds")
# Build a single Description that includes everything + pdfaid if requested:
parts = [
'<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="fpdf2">',
" <rdf:RDF",
' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"',
' xmlns:dc="http://purl.org/dc/elements/1.1/"',
' xmlns:xmp="http://ns.adobe.com/xap/1.0/"',
' xmlns:pdf="http://ns.adobe.com/pdf/1.3/"',
' xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">',
' <rdf:Description rdf:about=""',
]
# attributes block (xmp, pdf, pdfaid)
if creator_tool:
parts.append(f' xmp:CreatorTool="{esc(creator_tool)}"')
if xmp_create:
parts.append(f' xmp:CreateDate="{esc(xmp_create)}"')
parts.append(f' xmp:ModifyDate="{esc(xmp_modify)}"')
parts.append(f' xmp:MetadataDate="{esc(xmp_modify)}"')
if producer:
parts.append(f' pdf:Producer="{esc(producer)}"')
if keywords:
keyword_list = ",".join(keywords)
parts.append(f' pdf:Keywords="{esc(keyword_list)}"')
parts.append(" >")
# nested elements (Lang Alt / Seqs)
if pdfa:
parts.append(f" <pdfaid:part>{int(pdfa.part)}</pdfaid:part>")
if pdfa.conformance:
parts.append(
f" <pdfaid:conformance>{esc(pdfa.conformance)}</pdfaid:conformance>"
)
if pdfa.part == 4:
parts.append(" <pdfaid:rev>2020</pdfaid:rev>")
if title:
parts += [
" <dc:title><rdf:Alt>",
' <rdf:li xml:lang="x-default">' + esc(title) + "</rdf:li>",
" </rdf:Alt></dc:title>",
]
if subject:
parts += [
" <dc:description><rdf:Alt>",
' <rdf:li xml:lang="x-default">' + esc(subject) + "</rdf:li>",
" </rdf:Alt></dc:description>",
]
if author:
parts.append(" <dc:creator><rdf:Seq>")
for a in author:
parts.append(f" <rdf:li>{esc(a)}</rdf:li>")
parts.append(" </rdf:Seq></dc:creator>")
parts += [
" </rdf:Description>",
" </rdf:RDF>",
"</x:xmpmeta>",
]
return "\n".join(parts)
def _add_info(self) -> PDFInfo:
fpdf = self.fpdf
try:
creation_date = PDFDate(fpdf.creation_date, with_tz=True, encrypt=True)
except Exception as error:
raise FPDFException(
f"Could not format date: {fpdf.creation_date}"
) from error
info_obj = PDFInfo(
title=fpdf.title,
subject=getattr(fpdf, "subject", None),
author=getattr(fpdf, "author", None),
keywords=getattr(fpdf, "keywords", None),
creator=getattr(fpdf, "creator", None),
producer=getattr(fpdf, "producer", None),
creation_date=creation_date,
)
self._add_pdf_obj(info_obj)
return info_obj
def _add_encryption(self) -> Optional["EncryptionDictionary"]:
if self.fpdf._security_handler:
encryption_handler = self.fpdf._security_handler
pdf_obj = encryption_handler.get_encryption_obj()
self._add_pdf_obj(pdf_obj)
return pdf_obj
return None
def _add_output_intents(self) -> Optional[PDFArray]:
"""should be added in _add_catalog"""
output_intents = self.fpdf.output_intents
if not output_intents:
return None
for output_intent in output_intents:
if output_intent.dest_output_profile:
self._add_pdf_obj(output_intent.dest_output_profile)
return PDFArray(output_intents)
def _add_catalog(self) -> PDFCatalog:
fpdf = self.fpdf
catalog_obj = PDFCatalog(
lang=getattr(fpdf, "lang", None),
page_layout=fpdf.page_layout,
page_mode=fpdf.page_mode,
viewer_preferences=fpdf.viewer_preferences,
)
catalog_obj.output_intents = self._add_output_intents()
self._add_pdf_obj(catalog_obj)
return catalog_obj
def _finalize_catalog(
self,
catalog_obj: PDFCatalog,
pages_root_obj: PDFPagesRoot,
first_page_obj: PDFPage,
sig_annotation_obj: Optional[PDFAnnotation],
xmp_metadata_obj: Optional[PDFXmpMetadata],
struct_tree_root_obj: Optional[PDFObject],
outline_dict_obj: Optional[OutlineDictionary],
) -> None:
fpdf = self.fpdf
catalog_obj.pages = pages_root_obj
catalog_obj.struct_tree_root = struct_tree_root_obj
catalog_obj.outlines = outline_dict_obj
catalog_obj.metadata = xmp_metadata_obj
if sig_annotation_obj:
flags = SignatureFlag.SIGNATURES_EXIST + SignatureFlag.APPEND_ONLY
catalog_obj.acro_form = AcroForm(
fields=PDFArray([sig_annotation_obj]), sig_flags=flags
)
if fpdf.zoom_mode in ZOOM_CONFIGS:
zoom_config = [
pdf_ref(first_page_obj.id),
*ZOOM_CONFIGS[fpdf.zoom_mode], # type: ignore[index]
]
else: # zoom_mode is a number, not one of the allowed strings:
zoom_config = [
pdf_ref(first_page_obj.id),
"/XYZ",
"null",
"null",
str(cast(float, fpdf.zoom_mode) / 100),
]
catalog_obj.open_action = pdf_list(zoom_config)
if struct_tree_root_obj:
catalog_obj.mark_info = pdf_dict({"/Marked": "true"})
if fpdf.embedded_files or fpdf.named_destinations:
names_dict_entries: dict[str, str] = {}
if fpdf.embedded_files:
file_spec_names = [
f"{PDFString(embedded_file.basename()).serialize()} {embedded_file.file_spec().ref}"
for embedded_file in fpdf.embedded_files
]
names_dict_entries["/EmbeddedFiles"] = pdf_dict(
{"/Names": pdf_list(file_spec_names)}
)
global_file_specs = [
pdf_ref(ef.file_spec().id)
for ef in self.fpdf.embedded_files
if ef.globally_enclosed()
]
if global_file_specs:
catalog_obj.a_f = pdf_list(global_file_specs)
if fpdf.named_destinations:
# Create a list of name/destination pairs for the Dests name tree
dests_names: list[str] = []
for name, dest in fpdf.named_destinations.items():
# Check if this is a placeholder destination (page 0)
if dest.page_number == 0:
raise FPDFException(
f"Named destination '{name}' was referenced but never set with set_link(name=...)"
)
# Ensure the destination's page_ref is set
if not hasattr(dest, "page_ref") or not dest.page_ref:
assert dest.page_number is not None
page_index = dest.page_number - 1
if 0 <= page_index < len(fpdf.pages):
dest.page_ref = pdf_ref(fpdf.pages[dest.page_number].id)
# Add name and destination to the Dests list
dests_names.append(
f"{PDFString(name, encrypt=True).serialize(_security_handler=fpdf._security_handler, _obj_id=catalog_obj.id)} {dest.serialize()}"
)
if dests_names:
names_dict_entries["/Dests"] = pdf_dict(
{"/Names": pdf_list(sorted(dests_names))}
)
catalog_obj.names = pdf_dict(names_dict_entries)
page_labels = [
f"{i} {pdf_dict(label.serialize())}"
for i, page in enumerate(self._iter_pages_in_order())
if (label := page.get_page_label()) is not None
]
if page_labels and not fpdf.pages[1].get_page_label():
# If page labels are used, an entry for sequence 0 is mandatory
page_labels.insert(0, "0 <<>>")
if page_labels:
catalog_obj.page_labels = pdf_dict(
{"/Nums": PDFArray(page_labels).serialize()}
)
@contextmanager
def _trace_size(self, label: str) -> Iterator[None]:
prev_size = len(self.buffer)
yield
self.sections_size_per_trace_label[label] += len(self.buffer) - prev_size
def _log_final_sections_sizes(self) -> None:
LOGGER.debug("Final size summary of the biggest document sections:")
for label, section_size in self.sections_size_per_trace_label.items():
LOGGER.debug("- %s: %s", label, _sizeof_fmt(section_size))
def stream_content_for_raster_image(
info: RasterImageInfo,
x: float,
y: float,
w: float,
h: float,
keep_aspect_ratio: bool = False,
scale: float = 1,
pdf_height_to_flip: Optional[float] = None,
) -> str:
if keep_aspect_ratio:
x, y, w, h = info.scale_inside_box(x, y, w, h)
if pdf_height_to_flip:
stream_h = h
stream_y = pdf_height_to_flip - h - y
else:
stream_h = -h
stream_y = y + h
return (
f"q {w * scale:.2f} 0 0 {stream_h * scale:.2f}"
f" {x * scale:.2f} {stream_y * scale:.2f} cm"
f" /I{info['i']} Do Q"
)
def _tt_font_widths(font: TTFFont) -> str:
rangeid: int = 0
range_: dict[int, list[int]] = {}
range_interval: dict[int, bool] = {}
prevcid: int = -2
prevwidth: int = -1
interval: bool = False
# Glyphs sorted by mapped character id
glyphs = dict(sorted(font.subset.items(), key=lambda item: item[1]))
for glyph in glyphs:
assert glyph is not None
cid_mapped = glyphs[glyph]
if cid_mapped == (prevcid + 1):
if glyph.glyph_width == prevwidth:
if glyph.glyph_width == range_[rangeid][0]:
range_.setdefault(rangeid, []).append(glyph.glyph_width)
else:
range_[rangeid].pop()
# new range
rangeid = prevcid
range_[rangeid] = [prevwidth, glyph.glyph_width]
interval = True
range_interval[rangeid] = True
else:
if interval:
# new range
rangeid = cid_mapped
range_[rangeid] = [glyph.glyph_width]
else:
range_[rangeid].append(glyph.glyph_width)
interval = False
else:
rangeid = cid_mapped
range_[rangeid] = [glyph.glyph_width]
interval = False
prevcid = cid_mapped
prevwidth = glyph.glyph_width
prevk = -1
nextk = -1
prevint = False
ri = range_interval
for k, ws in sorted(range_.items()):
cws = len(ws)
if k == nextk and not prevint and (k not in ri or cws < 3):
if k in ri:
del ri[k]
range_[prevk] = range_[prevk] + range_[k]
del range_[k]
else:
prevk = k
nextk = k + cws
if k in ri:
prevint = cws > 3
del ri[k]
nextk -= 1
else:
prevint = False
w: list[str] = []
for k, ws in sorted(range_.items()):
if len(set(ws)) == 1:
w.append(f" {k} {k + len(ws) - 1} {ws[0]}")
else:
w.append(f" {k} [ {' '.join(str(int(h)) for h in ws)} ]\n")
return f"[{''.join(w)}]"
def _cid_font_widths(cid_widths: dict[int, int]) -> str:
rangeid: int = 0
range_: dict[int, list[int]] = {}
range_interval: dict[int, bool] = {}
prevcid: int = -2
prevwidth: int = -1
interval: bool = False
for cid, width in sorted(cid_widths.items()):
if cid == (prevcid + 1):
if width == prevwidth:
if width == range_[rangeid][0]:
range_.setdefault(rangeid, []).append(width)
else:
range_[rangeid].pop()
rangeid = prevcid
range_[rangeid] = [prevwidth, width]
interval = True
range_interval[rangeid] = True
else:
if interval:
rangeid = cid
range_[rangeid] = [width]
else:
range_[rangeid].append(width)
interval = False
else:
rangeid = cid
range_[rangeid] = [width]
interval = False
prevcid = cid
prevwidth = width
prevk = -1
nextk = -1
prevint = False
ri = range_interval
for k, ws in sorted(range_.items()):
cws = len(ws)
if k == nextk and not prevint and (k not in ri or cws < 3):
if k in ri:
del ri[k]
range_[prevk] = range_[prevk] + range_[k]
del range_[k]
else:
prevk = k
nextk = k + cws
if k in ri:
prevint = cws > 3
del ri[k]
nextk -= 1
else:
prevint = False
w: list[str] = []
for k, ws in sorted(range_.items()):
if len(set(ws)) == 1:
w.append(f" {k} {k + len(ws) - 1} {ws[0]}")
else:
w.append(f" {k} [ {' '.join(str(int(h)) for h in ws)} ]\n")
return f"[{''.join(w)}]"
def _dimensions_to_mediabox(dimensions: tuple[float, float]) -> str:
width_pt, height_pt = dimensions
return f"[0 0 {width_pt:.2f} {height_pt:.2f}]"
def _sizeof_fmt(num: float, suffix: str = "B") -> str:
# Recipe from: https://stackoverflow.com/a/1094933/636849
for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
if abs(num) < 1024:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024
return f"{num:.1f}Yi{suffix}"
def soft_mask_path_to_xobject(
path: PaintSoftMask | ImageSoftMask, resource_catalog: ResourceCatalog
) -> PDFContentStream:
"""Converts a PaintedSoftMask into a PDF XObject Form suitable for use as a soft mask."""
xobject = PDFContentStream(contents=path.render(resource_catalog).encode("latin-1"))
xobject._path = path # type: ignore[attr-defined]
xobject.type = Name("XObject") # type: ignore[attr-defined]
xobject.subtype = Name("Form") # type: ignore[attr-defined]
xobject.b_box = PDFArray(path.get_bounding_box()) # type: ignore[attr-defined]
xobject.group = "<</S /Transparency /CS /DeviceGray /I true /K false>>" # type: ignore[attr-defined]
return xobject
def blend_group_to_xobject(
group: "BlendGroup", resource_catalog: ResourceCatalog
) -> PDFContentStream:
"""Convert a blend group into a Form XObject with an isolated transparency group."""
stream = group.render(resource_catalog)
xobject = PDFContentStream(contents=stream.encode("latin-1"))
xobject._blend_group = group # type: ignore[attr-defined]
xobject._registered = False # type: ignore[attr-defined]
xobject.type = Name("XObject") # type: ignore[attr-defined]
xobject.subtype = Name("Form") # type: ignore[attr-defined]
bbox = group.get_bounding_box()
xobject.b_box = PDFArray(bbox) # type: ignore[attr-defined]
xobject.group = "<</S /Transparency /CS /DeviceRGB /I true>>" # type: ignore[attr-defined]
return xobject
|