Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

129 KB
Raw Blame
"""
PDF generation service using WeasyPrint.

Based on deep research findings, WeasyPrint is the optimal choice for
production Flask applications due to:
- Pure Python (no external binaries except Pango)
- Modern CSS3 support
- Active maintenance (v66.0 as of July 2025)
- Good paged media features
"""

import contextlib
import gzip
import http.client
import io
import platform
import re
import struct
import urllib.request
import urllib.response
import zlib
from contextvars import ContextVar
from html import escape
from typing import Optional, Dict, Any, NoReturn
from urllib.parse import unquote, urlsplit

import markdown  # type: ignore[import-untyped]
from loguru import logger
from markdown.preprocessors import Preprocessor  # type: ignore[import-untyped]

from ...security import dns_pinning, redact_url_for_log, validate_url
from ...utilities.resource_utils import safe_close


# WeasyPrint pulls in Pango/Cairo/fontTools — a heavy, multi-second import.
# This module is imported eagerly at router import time
# (web/routers/research.py -> pdf_service), so importing WeasyPrint at module load
# blocked web-server cold start by ~20s on CPU-constrained CI runners
# (issue #4431: "cold heavy-import under CI 2-core starvation"). PDF export is
# a rare, on-demand operation, so the import is deferred to first use.
#
# These names stay module-level (filled in by _ensure_weasyprint) so the
# render path — and the tests that patch them — keep working as before; they
# are just populated lazily instead of at import time.
HTML = None  # type: ignore[assignment,misc]
CSS = None  # type: ignore[assignment,misc]
# None until the import is attempted; True/False once determined.
WEASYPRINT_AVAILABLE: Optional[bool] = None


def _ensure_weasyprint() -> None:
    """Import WeasyPrint on first use, populating the module-level globals.

    Idempotent: the import is attempted once and the outcome cached in
    ``WEASYPRINT_AVAILABLE``. Handles the same ``(OSError, ImportError)``
    failure modes (e.g. missing Pango/Cairo system libraries) the original
    module-level guard did.
    """
    global HTML, CSS, WEASYPRINT_AVAILABLE, _URL_FETCHER
    global URLFetchingError, URLFetcherResponse
    if WEASYPRINT_AVAILABLE is not None:
        return
    try:
        from weasyprint import HTML as _HTML, CSS as _CSS
        from weasyprint.urls import (
            URLFetcher,
            URLFetchingError as _URLFetchingError,
        )

        try:
            from weasyprint.urls import URLFetcherResponse as _Response
        except ImportError:  # WeasyPrint < 68
            _Response = None

        HTML, CSS = _HTML, _CSS
        URLFetchingError = _URLFetchingError
        URLFetcherResponse = _Response
        # Built in full before it is published, so no caller can pick up a
        # fetcher that does not have the size limits yet.
        _URL_FETCHER = _build_url_fetcher(URLFetcher)
        WEASYPRINT_AVAILABLE = True
    except (OSError, ImportError):
        WEASYPRINT_AVAILABLE = False
        logger.warning("WeasyPrint not available — PDF export will be disabled")


def weasyprint_available() -> bool:
    """Return True when WeasyPrint and its system libraries can be imported.

    Triggers the lazy import on first call.
    """
    _ensure_weasyprint()
    return bool(WEASYPRINT_AVAILABLE)


_WEASYPRINT_DOCS_URL = (
    "https://doc.courtbouillon.org/weasyprint/stable/first_steps.html"
)


class UnsafePDFResourceURLError(ValueError):
    """Raised to refuse a blocked resource fetch.

    WeasyPrint's ``fetch()`` (``weasyprint/urls.py``) wraps any
    ``Exception`` the ``url_fetcher`` raises as ``URLFetchingError``;
    whether that skips the resource or aborts the render depends on the
    call site. ``@import`` is fetched inside a try/except in
    ``css/__init__.py`` and is skipped; ``@color-profile`` is fetched
    outside one, which is why ``markdown_to_pdf`` catches the error
    around the custom stylesheet itself. Subclassing ``ValueError`` here
    is a conventional choice, not what triggers the skip.
    """


class PDFResourceTooLargeError(ValueError):
    """Raised to refuse a resource over the PDF export's size limits.

    Raised inside the url_fetcher (by `_BoundedBodyProcessor`, or by
    `_refuse_if_budget_spent` before a fetch starts), so WeasyPrint
    handles it exactly as it handles `UnsafePDFResourceURLError`: the
    resource is skipped wherever a failed fetch is, and the retry in
    `_render_pdf` skips it where the first pass could not. The message
    never contains the URL.
    """


class UnsupportedPDFResourceError(PDFResourceTooLargeError):
    """Refuse a resource WeasyPrint would decode without a size bound.

    The per-resource and export byte caps bound what the fetcher reads and
    what it inflates from a gzip or deflate ``Content-Encoding``. They do
    not bound the work WeasyPrint does *after* the fetch returns: it hands a
    ``wOFF``/``wOF2`` web font to zlib/brotli, every non-SVG image to
    Pillow, and every glyph image of a colour-bitmap font to Pillow, with no
    size limit of its own (`_refuse_unbounded_resource` has the exact call
    sites). A small body can expand enormously there, so those bodies are
    refused in the fetcher instead. Subclasses
    `PDFResourceTooLargeError`, so it is skipped and logged exactly like an
    over-cap resource, and the message never contains the URL.
    """


# Populated by _ensure_weasyprint() on first PDF use. The URLFetcher preserves
# the allow_redirects=False posture that default_url_fetcher hard-coded.
# Redirects disabled keeps the SSRF guard airtight — validate_url only inspects
# the initial URL, so a 30x to a cloud metadata endpoint (see
# ssrf_validator.ALWAYS_BLOCKED_METADATA_IPS) would otherwise slip past.
# It also carries a `_BoundedBodyProcessor`, which reads every body before the
# fetch returns, bounds what reading it may cost (`MAX_PDF_RESOURCE_BYTES`) and
# charges that cost to the export's budget (`MAX_PDF_EXPORT_RESOURCE_BYTES`).
_URL_FETCHER = None
# Populated alongside the fetcher; see `_render_pdf` for why it is needed.
URLFetchingError = None
# The response type WeasyPrint 68 introduced. 69+ accepts nothing else, so the
# empty resource is built through this whenever it exists; the dict shape is
# used only on WeasyPrint < 68 (see `_empty_resource_for`).
URLFetcherResponse = None

# What the retry hands WeasyPrint in place of a resource it may not have. An
# empty body of an opaque type: every consumer treats it as nothing usable and
# moves on, which is the outcome the raising fetcher was already producing for
# every at-rule but one.
_EMPTY_RESOURCE_MIME_TYPE = "application/octet-stream"

# How many (redacted) skipped URLs the retry's summary warning lists; the
# rest are counted. Every skipped URL also gets its own warning.
_SKIPPED_SUMMARY_LIMIT = 20

# Size limits for what WeasyPrint fetches while rendering an export: the images
# report markdown references, and any stylesheet, font or image that those
# resources or a caller-supplied stylesheet reference in turn. Applied by
# `_BoundedBodyProcessor` to what reading each body costs (`_FetchCost`): its
# body bytes, its decoded bytes if it is gzip or deflate, and the work of
# decoding it. A compressed body is therefore bounded by what it decodes to,
# and by the work of decoding it, not by its size on the wire.
#
# This byte cap bounds reading and `Content-Encoding` decoding only. It does
# not bound the work WeasyPrint does on the body after the fetch returns: a
# compressed web font goes to zlib/brotli, and a raster image, or a font's
# colour-bitmap glyph images, go to Pillow, each able to expand far past the
# body's size. `_refuse_unbounded_resource`
# refuses those bodies in the fetcher. `MAX_PDF_IMAGE_PIXELS` per image and
# `MAX_PDF_EXPORT_IMAGE_PIXELS` across the export separately bound declared
# raster pixels. These are resource budgets, not an overall memory, CPU or
# elapsed-time quota for the renderer.
#
# 16 MiB per resource is far above what report images, stylesheets and web
# fonts need. `safe_requests.MAX_RESPONSE_SIZE` (1 GiB) is sized for documents
# a user downloads on purpose, not for resources a report merely references.
MAX_PDF_RESOURCE_BYTES = 16 * 1024 * 1024
# The most pixels one raster image a report references may declare. WeasyPrint
# opens every non-SVG image with Pillow and decodes it to a raster buffer of 4
# bytes a pixel (RGB/RGBA) to re-encode it; Pillow's own decompression-bomb
# guard only errors past ~178 M pixels (~700 MiB), and `LOAD_TRUNCATED_IMAGES`
# lets a tiny truncated body decode to its full declared size. So
# `_refuse_unbounded_resource` reads the size an image declares in its header
# and refuses one past this cap. 16 M pixels (4096x4096) admits ordinary
# content -- a 12 MP phone photo (~12 M) and a 4K/8.3 MP screenshot -- while
# holding one image's transient decode to 64 MiB of RGBA. Images are decoded
# one at a time (WeasyPrint's `pdf.write_pdf` loop calls `get_x_object` per
# image; see the per-export note below). This bounds one image's dimensions,
# not the renderer's total peak memory, which includes other buffers.
MAX_PDF_IMAGE_PIXELS = 16 * 1024 * 1024
# The most pixels all of one export's accepted raster images may be charged
# together (their declared pixels; for a JPEG, see below). The byte caps do
# not bound this: a highly compressible image (a flat colour, a simple
# chart) is tiny on the wire yet costs the same to decode
# as a noisy one its size, so a report of thousands of distinct-URL images,
# each under the per-image cap and cheap on the wire, could impose unbounded
# total decode work. (It would not keep that many *resident*: WeasyPrint's
# `RasterImage` retains only the re-encoded, compressed bytes -- JPEG kept as
# is, everything else re-encoded to PNG -- in an in-memory cache keyed by URL,
# and decodes to raw pixels only transiently, one image at a time. Re-encoding
# can change the compressed size, so the fetched byte budget is not a bound
# on resident image memory. This separate cap bounds the total pixels to
# decode.) A JPEG is charged what libjpeg decodes rather than
# what it declares: its area padded to whole MCUs (groups of 8x8 blocks, up
# to 32x32 pixels with chroma subsampling, so a 1-pixel-high image is
# decoded 8 to 32 rows high), times its scans past `_JPEG_BUDGET_SCANS`
# divided by that many (libjpeg passes over the blocks once per scan; see
# `_jpeg_budget_charge`). So the budget bounds total decode work to about
# this many pixels of ordinary decoding, not this many pixels whatever
# their shape and scans. Per-pixel cost still varies with content (noise
# costs several times a flat colour) and format.
# `_refuse_unbounded_resource` charges each accepted image these pixels
# against this budget and refuses the rest, so the export still completes. 256
# M pixels admits dozens of multi-megapixel images (about 21 at 12 MP, 64 at 4
# MP, 128 at 2 MP) -- far more than an ordinary report -- while a thousands-of-
# images reference set is cut off.
MAX_PDF_EXPORT_IMAGE_PIXELS = 256 * 1024 * 1024
# Raster image formats whose body `_refuse_unbounded_resource` accepts once it
# is within `MAX_PDF_IMAGE_PIXELS`; any other raster format Pillow would decode
# is refused. SVG is not here because it is vector XML, not a Pillow raster.
# Named as Pillow names them (``Image.OPEN``). A multi-picture JPEG (MPO) is
# opened through JPEG's own factory (``jpeg_factory``) and so classified as
# ``JPEG``; Pillow registers no separate ``MPO`` opener.
_ALLOWED_RASTER_FORMATS = frozenset({"PNG", "JPEG", "GIF", "WEBP"})
# These Pillow plugins have no signature sniffer, so their factories are run
# on the (already byte-capped) body to rule them out. Each was reviewed to
# decode no pixels and allocate nothing according to the declared image
# dimensions. Not all read a fixed-size header: PCD, SPIDER and TGA do, but
# IM and IMT read text header lines that may run to the end of the body
# (linear in the body, which the byte cap bounds). Only these factories may
# be called during classification. A new plugin without a sniffer fails closed until its
# header parser is reviewed.
_HEADER_ONLY_RASTER_FORMATS = frozenset({"IM", "IMT", "PCD", "SPIDER", "TGA"})
# Formats without a sniffer that are ruled out by a reviewed prefix instead of
# by running their factory. IPTC's ``_open`` walks every field to the end of
# the body before it can fail, but its first field must start with 0x1C (or
# the parse fails on a missing field), so a body starting with anything else
# cannot be IPTC, and one starting with 0x1C is refused as IPTC unparsed.
_PREFIX_RULED_RASTER_FORMATS = {"IPTC": b"\x1c"}
# Web-font magic numbers WeasyPrint decompresses after the fetch: ``wOFF`` via
# zlib (fontTools ``TTFont.save``) and ``wOF2`` via brotli (fontTools
# ``woff2.decompress``), both without a size limit. Refused outright.
_COMPRESSED_FONT_MAGIC = (b"wOFF", b"wOF2")
# The font containers HarfBuzz reads tables from (``OpenTypeFontFile`` in
# ``hb-open-file.hh``): a single sfnt face under one of these version tags, a
# collection of faces (``ttcf``), or a Mac resource-fork font (dfont). Pango
# builds each installed ``@font-face`` font's ``hb_face`` from the file, and
# WeasyPrint reads its colour-glyph tables through it.
_SFNT_VERSION_TAGS = frozenset({b"\x00\x01\x00\x00", b"OTTO", b"true", b"typ1"})
_FONT_COLLECTION_TAG = b"ttcf"
_RESOURCE_FORK_FONT_TAG = b"\x00\x00\x01\x00"
# Tables that make WeasyPrint draw glyphs as images rather than outlines.
# ``sbix`` and ``CBDT``/``CBLC`` hold a PNG per glyph: WeasyPrint
# (``pdf/fonts.py`` ``hb_ot_color_has_png``, ``draw/text.py``) opens each
# drawn glyph's PNG with an unrestricted ``Image.open`` and builds a
# ``RasterImage`` from it, for every occurrence, outside the fetcher and so
# outside `MAX_PDF_IMAGE_PIXELS` and the export's pixel budget: a 704-byte
# font can declare a 13000x13000 glyph. ``SVG `` holds an SVG document per
# glyph (``hb_ot_color_has_svg``), which can embed raster images of its own.
# A font with any of them is refused, so it falls back to local fonts.
_GLYPH_IMAGE_TABLES = frozenset({b"sbix", b"CBDT", b"CBLC", b"SVG "})
# Caps on the table directory `_font_refusal` reads: an ordinary font has a
# few dozen tables and a collection a few dozen faces. A directory past them
# is refused rather than walked.
_MAX_FONT_TABLES = 512
_MAX_FONT_COLLECTION_FACES = 256
# A GIF body starts with one of these (Pillow's GIF sniffer matches exactly
# them). `_gif_structure_refusal` walks such a body's block structure before
# Pillow parses any of it, the size probe included.
_GIF_SIGNATURES = (b"GIF87a", b"GIF89a")
# Caps on a GIF's block structure. Pillow's GIF parser takes one Python step
# per block, and Pillow 12.3's is worse than linear in comments: it joins a
# comment extension's data sub-blocks with ``comment += block`` and a frame's
# comment extensions with ``info["comment"] += ...``, so their cost grows
# with the square of their number (timed on the test machine: 128 Ki one-byte
# comment sub-blocks, a 256 KiB body, cost 0.25 s; 512 Ki, a 1 MiB body,
# 4.1 s; a 16 MiB body would cost minutes). Pillow 13.0.0 collects them in a
# list and joins it once, linear in their number, but still takes one Python
# step per block. The guard's probe parses the header once
# and WeasyPrint's ``Image.open`` again, so a body past any cap is refused
# before either runs. The pre-scan itself only follows offsets, and these
# caps bound its steps.
#
# WeasyPrint draws only a GIF's first frame: ``Image.open``, ``convert`` and
# ``save(format="PNG")`` without ``save_all`` read frame 0 and nothing after
# it, and nothing asks for ``n_frames`` or ``is_animated``. So the frame cap
# need not admit every animation, only far more than a report embeds: 16384
# frames is over nine minutes at 30 frames a second. For the same reason,
# once one frame is complete, the walk ends without refusing at a cut or at
# bytes it cannot read: Pillow never reads that far.
_MAX_GIF_FRAMES = 16384
# Every block and data sub-block, image data included. Encoders write image
# data in 255-byte sub-blocks, so a well-formed GIF within the 16 MiB
# resource cap has about 64 Ki of them, plus at least four blocks per frame
# (a descriptor, a graphic control extension and its sub-block, and an
# image-data sub-block: 64 Ki at the frame cap). 256 Ki is twice the sum of
# both, and bounds the pre-scan's own steps.
_MAX_GIF_BLOCKS = 256 * 1024
# Extensions (graphic control, application, plain text and comment) and
# their data sub-blocks. A graphic control extension per frame takes two (32
# Ki at the frame cap), a NETSCAPE loop block three, and an ICC profile or
# XMP packet a few thousand at most; 64 Ki leaves room for all of them
# together. Counting the extensions themselves, not only their sub-blocks,
# also bounds the walk over empty ones.
_MAX_GIF_EXTENSION_BLOCKS = 64 * 1024
# Comment extensions plus their data sub-blocks, and the bytes those carry:
# the parts Pillow concatenates. Encoders typically write one short comment
# (a tool name or a caption), in 255-byte sub-blocks. At these caps
# Pillow's concatenation costs milliseconds.
_MAX_GIF_COMMENT_BLOCKS = 4096
_MAX_GIF_COMMENT_BYTES = 64 * 1024
# A JPEG body starts with these (Pillow's JPEG sniffer matches exactly them).
# `_jpeg_structure_refusal` walks its marker segments up to the first scan
# before Pillow parses any of it.
_JPEG_SIGNATURE = b"\xff\xd8\xff"
# Markers Pillow 12.3 reads no length for (``MARKER`` entries without a
# handler): JPG, RST0-7, SOI, EOI and JPG0-13.
_JPEG_STANDALONE_MARKERS = frozenset(
    {0xC8, *range(0xD0, 0xDA), *range(0xF0, 0xFE)}
)
# Markers Pillow reads a two-byte length and a segment for: SOFn, DHT, DAC,
# SOS, DQT, DNL, DRI, DHP, EXP, APP0-15 and COM. Any other marker makes
# Pillow raise, so the walk refuses it too.
_JPEG_SEGMENT_MARKERS = frozenset(
    {*range(0xC0, 0xC8), *range(0xC9, 0xD0), *range(0xDA, 0xF0), 0xFE}
)
# Caps on a JPEG's header, everything before its first scan (SOS), which is
# all Pillow parses on open. Pillow (12.3 and 13.0.0 alike, except where
# noted) is not linear in it:
#
# - it joins every ``Exif\0\0`` APP1 segment with ``info["exif"] += s[6:]``,
#   quadratic in their number (timed on the test machine: 80 Ki one-byte
#   segments, an 880 KB body, 0.19 s; 1.4 M, under the 16 MiB cap, about
#   50 s); Pillow 12.3 then strips every leading ``Exif\0\0`` header one
#   slice at a time, quadratic in their number (512 KiB of them, 0.6 s),
#   where 13.0.0 strips at most two with one regular expression;
# - it loads the Exif data's TIFF directories (``_read_dpi_from_exif`` on
#   open; ``exif_transpose``, which WeasyPrint runs for its default
#   ``image-orientation: from-image``, also loads and decodes the Exif, GPS
#   and Interop directories), reading every tag's data however many tags
#   point at the same bytes: one 64 KiB segment of 5400 such tags, 0.19 s;
# - it decodes every tag of an ``MPF`` APP2 segment's TIFF directory
#   (``_getmp``, run by the JPEG factory itself) into Python values: one
#   64 KiB segment of 5400 overlapping tags, 9.7 s.
#
# Everything else on the header path (ICC, Photoshop and XMP segments,
# comments, tables) is linear in the body. A JPEG past any cap below is
# refused before the probe or WeasyPrint parses it.
#
# Marker segments and standalone markers, before the first scan and between
# scans up to the end of the image (EOI). An ordinary JPEG has a dozen or
# two; an ICC profile split over 64 KiB APP2 segments, an extended XMP
# packet, or MPO and multi-APP1 camera metadata add a few dozen more, and a
# progressive JPEG a table or two per scan. Only 256 full-size segments fit
# in the 16 MiB resource cap at all.
_MAX_JPEG_SEGMENTS = 1024
# Frame headers: SOFn, and DHP, which Pillow parses with the same handler.
# Pillow appends a tuple to one list for every component each of them
# declares (up to 21 Ki in one 64 KiB segment), so their memory grows with
# their number. A JPEG has one frame; only hierarchical JPEG has more, and
# libjpeg does not decode it.
_JPEG_FRAME_MARKERS = frozenset(
    {*range(0xC0, 0xC4), *range(0xC5, 0xC8), *range(0xC9, 0xCC)}
    | {*range(0xCD, 0xD0), 0xDE}
)
_MAX_JPEG_FRAME_HEADERS = 1
# Scans (SOS segments) up to EOI. The declared pixels do not bound a JPEG's
# decode work by themselves: libjpeg decodes a JPEG with several scans
# (progressive, or sequential or lossless with one scan per component or
# group of components) one scan at a time, each scan a pass over the image's
# blocks, so decode time grows with blocks times scans. libjpeg's default
# progressive scripts write 6 to 18 scans (grey to CMYK), optimizing
# encoders about as many, and a non-interleaved sequential JPEG one per
# component; 64 leaves room for custom scripts.
_MAX_JPEG_SCANS = 64
# The scans a JPEG may have and still be charged its pixels once against the
# export's pixel budget (`_jpeg_budget_charge`); one with more is charged in
# proportion. An ordinary progressive JPEG has about 10 (one of 64 is
# charged 6.4 times), so many-scan images cannot multiply the work the
# budget admits by the scan cap.
_JPEG_BUDGET_SCANS = 10
# The next marker in entropy-coded data, as libjpeg finds it: 0xFF followed by
# anything but a stuffed 0x00, a fill byte 0xFF or a restart marker (RST0-7,
# which belongs to the scan).
_JPEG_SCAN_MARKER = re.compile(rb"\xff[^\x00\xd0-\xd7\xff]")
# Fill bytes (0xFF), stuffed 0xFF00 pairs and stray bytes between segments,
# each one Python step for Pillow (about 0.27 s per MiB). Encoders write
# none or a few.
_MAX_JPEG_PADDING_BYTES = 4096
# ``Exif\0\0`` APP1 segments, and the bytes Pillow joins from them. The Exif
# standard puts Exif in one APP1 segment, at most 64 KiB; some cameras
# continue it over a few more. 16 segments and 256 KiB (four full segments)
# leave room for those while keeping the join to milliseconds.
_MAX_JPEG_EXIF_SEGMENTS = 16
_MAX_JPEG_EXIF_BYTES = 256 * 1024
# Leading ``Exif\0\0`` headers, which Pillow strips before it reads the
# TIFF data: 12.3 every one, a slice at a time; 13.0.0 at most two, so that
# a third is read as the TIFF header and fails. There is one, and a faulty
# writer may repeat it once. Also applied to PNG and WebP Exif data, which
# Pillow strips the same way.
_MAX_JPEG_EXIF_HEADERS = 4
# Caps on the TIFF directories Pillow loads from the Exif data (IFD0 and the
# Exif, GPS and Interop directories it points to) and from each MPF segment:
# their entries; the bytes the entries' data declare (each entry's ``count``
# times its type's size, past what fits in the entry itself), which Pillow
# reads; and how many numbers among them (every type but BYTE, ASCII
# and UNDEFINED), which it decodes to Python values one by one. Ordinary
# Exif has a few dozen entries and a few hundred numbers at most (a transfer
# function is 768), and MPF a handful. Their data are disjoint, so they
# total less than the Exif data (at most 64 KiB in one segment, a maker note
# included) or the MPF segment; 128 KiB is twice one segment. Timed at these
# caps, decoding and re-encoding the lot in ``exif_transpose`` costs at most
# about 0.05 s (4096 rationals).
_MAX_TIFF_IFD_ENTRIES = 1024
_MAX_TIFF_IFD_DATA_BYTES = 128 * 1024
_MAX_TIFF_IFD_NUMBERS = 4096
# The size of one value of each TIFF type Pillow 12.3 loads
# (``ImageFileDirectory_v2._load_dispatch``); it skips any other type.
_TIFF_TYPE_SIZES = {
    1: 1, 2: 1, 3: 2, 4: 4, 5: 8, 6: 1, 7: 1, 8: 2, 9: 4, 10: 8, 11: 4,
    12: 8, 13: 4, 16: 8,
}  # fmt: skip
# BYTE, ASCII and UNDEFINED: data Pillow decodes as a whole, not
# number by number.
_TIFF_BYTE_STRING_TYPES = frozenset({1, 2, 7})
# Integer representations whose first decoded value can identify a
# directory offset, indexed by their ``struct`` codes.
_TIFF_INTEGER_CODES = {
    3: "H", 4: "L", 6: "b", 8: "h", 9: "l", 13: "L", 16: "Q",
}  # fmt: skip
# TIFF headers Pillow's ``ImageFileDirectory_v2`` reads as a classic TIFF
# directory (its ``PREFIXES`` other than little-endian BigTIFF, which fails
# at once on Exif's 8-byte header).
_TIFF_CLASSIC_HEADERS = {
    b"II*\x00": "<",
    b"II\x00*": "<",
    b"MM\x00*": ">",
    b"MM*\x00": ">",
    b"MM\x00+": ">",
}
# The tags through which Pillow's ``Exif.tobytes`` loads further
# directories: from IFD0, the Exif and GPS directories; from the Exif
# directory, the Interop directory.
_EXIF_IFD_POINTERS = {None: (0x8769, 0x8825), 0x8769: (0xA005,)}
# PNG and WebP carry Exif data in a chunk: PNG in ``eXIf`` (Pillow also
# takes a ``tEXt`` chunk keyed ``exif`` as raw Exif), WebP in ``EXIF``.
# WeasyPrint's ``exif_transpose`` loads it as it does a JPEG's, with the same
# header strip and TIFF directory reads, so `_png_structure_refusal` and
# `_webp_structure_refusal` find those chunks and apply the
# ``_MAX_JPEG_EXIF_HEADERS`` and ``_MAX_TIFF_*`` caps to each. Both formats
# allow one Exif chunk; Pillow keeps PNG's last and WebP's first, so every
# one is checked, up to this many. `_png_structure_refusal` also refuses
# outright a ``zTXt`` or ``iTXt`` chunk keyed ``exif``: Pillow decodes that
# value to text, which ``getexif`` hands to ``Exif.load`` as text (a
# TypeError there today, which WeasyPrint turns into a dropped image), and
# the walk relies on none of that.
#
# ``Image.getexif`` reads one PNG carrier more: a text chunk keyed ``Raw
# profile type exif`` (the raw-profile form ImageMagick writes), which it
# hex-decodes as Exif -- but only when ``info`` has no ``exif`` entry.
# Nothing on WeasyPrint 70's path calls ``getexif`` on a PNG except
# ``exif_transpose``, which ``rotate_pillow_image`` runs only for an image
# whose ``info`` has an ``exif`` entry, so that carrier is never decoded in
# an export and the walk leaves it alone (pinned by TestPngAndWebpExif).
_MAX_EXIF_CHUNKS = 4
_PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
_PNG_TEXT_CHUNK_KINDS = (b"tEXt", b"zTXt", b"iTXt")
# Chunks the walks step over, one Python step each. Pillow's PNG reader also
# takes one per chunk, but for the chunks whose payload decompresses --
# ``iCCP`` profiles, and ``zTXt`` and compressed ``iTXt`` text -- whose work
# `_MAX_PNG_DECOMPRESSED_BYTES` bounds instead. Encoders write PNG image data
# in chunks of 8 KiB or more, so a PNG within the 16 MiB resource cap has a
# few thousand at most; an animated PNG adds two per frame. libwebp's demuxer
# walks a WebP's chunks in C; a still WebP has a handful, and an animated one
# two or three per frame, all of which the walk counts: the frame's ``ANMF``
# chunk, which it enters, and the image chunks inside (``VP8 `` or ``VP8L``,
# and ``ALPH``) -- so the cap admits over 20,000 frames.
_MAX_PNG_CHUNKS = 64 * 1024
_MAX_WEBP_CHUNKS = 64 * 1024
# What a PNG's compressed metadata chunks -- ``iCCP``, ``zTXt`` and
# compressed ``iTXt`` -- may decompress to together. Pillow decompresses
# each of them itself (``PngStream.chunk_iCCP``, ``chunk_zTXt``,
# ``chunk_iTXt``), per chunk bounded only by its own ``MAX_TEXT_CHUNK`` of
# 1 MiB, and with no cumulative bound at all for ``iCCP`` (its cumulative
# ``MAX_TEXT_MEMORY`` check covers text chunks only). A chunk that fails does
# not stop it: it swallows the ``zlib.error`` of corrupt data, after
# decompressing up to the corruption, and -- under ``LOAD_TRUNCATED_IMAGES``,
# which WeasyPrint sets on import -- the error of a chunk past 1 MiB too,
# keeps no value from that chunk and goes on to the next. 1 KiB of compressed
# zeros per iCCP chunk decompresses to 1 MiB each, so 400 such chunks (a
# 0.42 MB body) cost 0.96 s inside one Pillow open on the test machine
# (about 2.5 ms per decompressed MiB), and a 16 MiB body holds some 15,000
# of them -- tens of seconds per open, and Pillow opens an accepted PNG
# three or four times per fetch: the size probe, WeasyPrint's open, and a
# re-open each time WeasyPrint writes the image into the PDF
# (``get_x_object``: once, or twice for an image drawn both with and
# without smoothing, which ``image-rendering`` selects). So the walk
# decompresses these payloads itself (`_png_inflated_bytes`), a step at a
# time, charging each step what it puts out (and a step zlib rejects, whose
# output CPython discards, the most it may put out), and refuses the PNG as
# soon as the total passes this cap, at most one byte past it. However the
# chunks compress or fail, the walk decompresses at most that much of one
# PNG, and each Pillow open of an accepted PNG at most this cap, since
# Pillow decompresses no chunk past what the walk charged it: four or five
# passes in all, at most about 0.05 s at the measured rate. One that would
# cost more is refused before Pillow parses it. Pillow keeps only the last
# iCCP chunk's profile, so the cap is far past what a real image
# decompresses.
_MAX_PNG_DECOMPRESSED_BYTES = 4 * 1024 * 1024
# What all of one export's fetches may cost together: four resources at the
# per-resource limit. Every response is charged what reading it cost
# (`_FetchCost`), whether it was accepted, refused or failed part-way, so many
# oversized or undecodable references spend the budget just as many large
# resources do. No read asks for more than one byte past what is left, and
# once nothing is left every later fetch in the export is refused before it
# starts (`_refuse_if_budget_spent`). One export therefore reads at most this
# much body data plus one byte, and decodes at most this much plus one
# decoding step; `_FetchCost` lists what is not counted. Everything the
# export fetches draws on it, including the custom stylesheet's imports and
# the retry in `_render_pdf`.
MAX_PDF_EXPORT_RESOURCE_BYTES = 64 * 1024 * 1024
# The most one read of a body asks for, and the most one decoding step may
# produce.
_RESOURCE_READ_CHUNK_BYTES = 64 * 1024
# The most compressed data one decoding step takes in. Small, because a step
# that raises part-way is charged as if it had taken in all of it.
_INFLATE_INPUT_CHUNK_BYTES = 4 * 1024
# What each compressed byte that zlib takes in costs. Decoding work does not
# follow the output: a deflate stream whose blocks each build Huffman tables of
# their own can decode to nothing yet still cost CPU for every byte of it. At
# this cost a gzip or deflate body is refused once its compressed data passes
# 1/12 of its limit (about 1.3 MiB of the 16 MiB), and data that compresses
# less than 12 to 1 is charged more than its decoded size: text, at about 4 to
# 1, three times.
#
# 12 bounds, it does not measure. The budget lets at most ~5.3 MiB of
# compressed data be inflated across an export (64 MiB / 12), and the costliest
# such stream found in review -- dynamic-Huffman blocks that decode to nothing
# -- cost about 1 s of CPU per export when timed against this budget. The cost
# stays bounded by the budget whatever the stream: a larger constant would
# charge legitimate gzip- or deflate-encoded resources (which a CDN sends for
# CSS and SVG) more, dropping them sooner.
_INFLATED_BYTE_COST = 12
# What each member of a gzip body costs at least. A body may concatenate any
# number of members, and starting one costs about as much CPU as decoding a
# few KiB, however little it holds; an empty member is 20 bytes on the wire.
_GZIP_MEMBER_COST_BYTES = 4 * 1024

# Gzip header flags (RFC 1952, section 2.3.1), named as in ``gzip.py``.
_FHCRC, _FEXTRA, _FNAME, _FCOMMENT = 2, 4, 8, 16
# Zero padding, which gzip allows after a member.
_ZERO_BYTES = re.compile(rb"\0*")


class _ResourceBudget:
    """What the current export's fetches may still cost.

    ``remaining`` is the byte budget (`MAX_PDF_EXPORT_RESOURCE_BYTES`) for
    reading and decoding bodies. ``image_pixels`` is a separate budget, in
    pixels, for the raster images accepted so far: the byte budget does not
    bound an image's decode, so each accepted image is charged its pixels
    here (declared; for a JPEG, MCU-padded and scaled by scans:
    `_refuse_unbounded_resource`), and once it is spent further images are
    refused.
    """

    __slots__ = (
        "remaining",
        "spent_reported",
        "image_pixels",
        "image_budget_reported",
    )

    def __init__(self, total: int, image_pixels: Optional[int] = None) -> None:
        self.remaining = total
        # Whether `_refuse_if_budget_spent` has logged its warning yet.
        self.spent_reported = False
        # Read the module global at call time (so a test may patch it); the
        # default is the whole export's image-decode budget.
        self.image_pixels = (
            MAX_PDF_EXPORT_IMAGE_PIXELS
            if image_pixels is None
            else image_pixels
        )
        # Whether the image-pixel-budget-spent warning has been logged yet.
        self.image_budget_reported = False

    def charge(self, cost: int) -> None:
        # A refused body can cost more than was left: one byte more on the
        # wire, or one decoding step more (see `_inflate`).
        self.remaining = max(0, self.remaining - cost)

    def charge_image_pixels(self, pixels: int) -> bool:
        """Deduct an accepted image's charged pixels; return whether they fit.

        A declaration past what is left spends the rest of the budget (so
        every later image is refused too) and returns ``False``.
        """
        fits = pixels <= self.image_pixels
        self.image_pixels = max(0, self.image_pixels - pixels)
        return fits


# Set by `markdown_to_pdf` for the length of one export. A context variable,
# not state on a fetcher: `_safe_url_fetcher` and `_URL_FETCHER` are shared by
# every export in the process, concurrent ones included. Outside an export
# (None) only the per-resource limit applies.
_export_resource_budget: ContextVar[Optional[_ResourceBudget]] = ContextVar(
    "pdf_export_resource_budget", default=None
)


def _over_limit(limit: int) -> PDFResourceTooLargeError:
    return PDFResourceTooLargeError(f"PDF resource larger than {limit} bytes")


def _refuse_if_budget_spent(url) -> None:
    """Refuse a fetch before it starts if the export's budget is spent.

    Runs before `validate_url`, which resolves the host name, so an export
    whose budget is spent makes no further requests of any kind. Warns once
    per export, not once per resource: a report can reference any number.
    """
    budget = _export_resource_budget.get()
    if budget is None or budget.remaining > 0:
        return
    if not budget.spent_reported:
        budget.spent_reported = True
        logger.warning(
            "Refused a resource in PDF rendering without fetching it, as "
            "every later one in this export will be: the export's "
            f"{MAX_PDF_EXPORT_RESOURCE_BYTES}-byte resource budget is spent: "
            f"{redact_url_for_log(url)}"
        )
    raise PDFResourceTooLargeError(
        f"PDF export resource budget of {MAX_PDF_EXPORT_RESOURCE_BYTES} "
        "bytes is spent"
    )


class _FetchCost:
    """What reading one response has cost so far.

    ``wire`` counts the body bytes read from the response: what
    ``http.client`` returns, after any chunked framing is removed.
    ``decoded`` counts every byte inflated from a gzip or deflate body,
    including what a failed attempt decoded. ``inflated`` counts the
    compressed bytes zlib took in to decode them, each charged
    `_INFLATED_BYTE_COST`, and ``members`` the gzip members, each charged
    `_GZIP_MEMBER_COST_BYTES`. Each read and each decoding step is counted
    before it runs, at the most it can cost, then corrected to what it did
    cost once it returns (`_read_body`, `_inflate`), so one that raises
    part-way stays counted.

    Not counted: what ``http.client`` reads around the body itself. That
    is the response head (a status line and up to 100 header lines of up
    to 64 KiB each, after any number of interim ``100 Continue``
    responses), the chunk framing (a size line of up to 64 KiB, extensions
    included, and a line end for every chunk, however small), the trailer
    (any number of lines of up to 64 KiB), and what its buffered reader
    takes from the connection ahead of what it returns (up to
    ``io.DEFAULT_BUFFER_SIZE`` per response: 128 KiB on Python 3.14). A
    server can therefore still keep one fetch reading without end, as it
    can by sending slowly. A body refused unread for a ``Content-Length``
    past the limit is charged as if its first read had been made
    (`_read_body`), so at most `MAX_PDF_EXPORT_RESOURCE_BYTES` /
    `_RESOURCE_READ_CHUNK_BYTES` (1024) of those are fetched per export.
    But a fetch whose body is never read costs nothing, so the number of
    such fetches is not limited. That covers a non-2xx status (which
    ``HTTPErrorProcessor`` raises on before `_BoundedBodyProcessor` runs)
    and an empty body, and also every fetch that fails before a response
    arrives: a DNS pin that fails or is refused, a lookup the block-private
    window refuses, and a connection that fails. Each of those can still
    spend up to about 5 s on a bounded lookup
    (``dns_pinning._RESOLVE_TIMEOUT_SECONDS``) before it fails.
    """

    __slots__ = ("wire", "decoded", "inflated", "members")

    def __init__(self) -> None:
        self.wire = 0
        self.decoded = 0
        self.inflated = 0
        self.members = 0

    def total(self) -> int:
        work = (
            self.inflated * _INFLATED_BYTE_COST
            + self.members * _GZIP_MEMBER_COST_BYTES
        )
        return max(self.wire, self.decoded, work)

    def check(self, limit: int) -> None:
        """Refuse the resource once its cost has passed ``limit``."""
        if self.total() > limit:
            raise _over_limit(limit)


def _read_body(response, limit: int, cost: _FetchCost) -> bytes:
    """Read ``response``'s body to its end, as it arrives on the wire.

    Refuses it (`_FetchCost.check`) as it is read, or before any of it is
    read if its ``Content-Length`` is already past ``limit`` (charged then
    as if its first read had been made: `_RESOURCE_READ_CHUNK_BYTES`, or
    one byte past ``limit`` if that is less). No read asks
    for more than one byte past ``limit``, and each is counted before it is
    made, at what it asks for: a read that raises part-way (a chunked body
    cut short, a timeout) stays counted. A body that ends before its
    ``Content-Length`` raises ``http.client.IncompleteRead``, as an unsized
    ``read()`` would: the sized reads used here return a short body as if
    it were complete. Without a ``Content-Length``, a chunked body that
    ends early raises ``IncompleteRead`` from ``http.client``; a body that
    ends when the connection closes cannot be told apart from a complete
    one.
    """
    declared = getattr(response, "length", None)  # http.client's parsed value
    if isinstance(declared, int) and declared > limit:
        # Charged what its first read would have asked for, as if that read
        # had been made, so a declared length past the limit still spends
        # the budget and the number of such fetches is bounded too.
        cost.wire = min(_RESOURCE_READ_CHUNK_BYTES, limit + 1)
        raise _over_limit(limit)
    chunks = []
    while True:
        size = min(_RESOURCE_READ_CHUNK_BYTES, limit - cost.wire + 1)
        cost.wire += size
        chunk = response.read(size)
        cost.wire -= size - len(chunk)
        if not chunk:
            break
        cost.check(limit)
        chunks.append(chunk)
    data = b"".join(chunks)
    if isinstance(declared, int) and len(data) < declared:
        raise http.client.IncompleteRead(data, declared - len(data))
    return data


def _inflate(
    inflater, data: bytes, start: int, limit: int, cost: _FetchCost, out: list
) -> Optional[int]:
    """Feed ``data[start:]`` to ``inflater`` until its stream ends.

    Each call takes in at most `_INFLATE_INPUT_CHUNK_BYTES` and puts out at
    most `_RESOURCE_READ_CHUNK_BYTES`. It is counted in ``cost`` before it
    runs, at those most, then corrected to what it took in and put out,
    refusing past ``limit`` before the next call. So a refused stream is
    decoded at most one call past its limit, and a call that raises
    part-way stays counted. Appends the output to ``out``. Returns the
    offset just past the end of the stream, or None if the data ends first.
    """
    view = memoryview(data)
    pos = start
    pending = b""
    while not inflater.eof:
        if pending:
            piece = pending
        else:
            piece = view[pos : pos + _INFLATE_INPUT_CHUNK_BYTES]
            pos += len(piece)
        cost.inflated += len(piece)
        cost.decoded += _RESOURCE_READ_CHUNK_BYTES
        try:
            chunk = inflater.decompress(piece, _RESOURCE_READ_CHUNK_BYTES)
        except zlib.error:
            # A step zlib rejects returns no output, so the output charged in
            # advance was never produced. The work stays charged.
            cost.decoded -= _RESOURCE_READ_CHUNK_BYTES
            raise
        # What zlib did not take in: left for the next call or, once the
        # stream has ended, past its end (CPython then sets both).
        cost.inflated -= len(
            inflater.unused_data if inflater.eof else inflater.unconsumed_tail
        )
        cost.decoded -= _RESOURCE_READ_CHUNK_BYTES - len(chunk)
        cost.check(limit)
        if not chunk and not piece:
            return None
        if chunk:
            out.append(chunk)
        pending = inflater.unconsumed_tail
    return pos - len(inflater.unused_data)


def _inflate_capped(
    data: bytes, wbits: int, limit: int, cost: _FetchCost
) -> bytes:
    """``zlib.decompress(data, wbits)``, within ``limit`` (see `_inflate`)."""
    out: list = []
    if _inflate(zlib.decompressobj(wbits), data, 0, limit, cost, out) is None:
        # What zlib.decompress raises for a stream that ends early.
        raise zlib.error("incomplete or truncated stream")
    # Like zlib.decompress, ignores anything after the end of the stream.
    return b"".join(out)


def _gzip_truncated() -> EOFError:
    # What GzipFile raises for a body that ends part-way through a member.
    return EOFError(
        "Compressed file ended before the end-of-stream marker was reached"
    )


def _gzip_header_end(data: bytes, pos: int) -> int:
    """Where the deflate data of the gzip member at ``pos`` starts.

    Parses the header as ``gzip._read_gzip_header`` does. It checks the
    magic number and the compression method. It skips the optional extra
    field, name, comment and header CRC, and does not check that CRC. It
    ignores the other flag bits.
    """
    magic = data[pos : pos + 2]
    if magic != b"\037\213":
        raise gzip.BadGzipFile(f"Not a gzipped file ({magic!r})")
    if len(data) < pos + 10:
        raise _gzip_truncated()
    if data[pos + 2] != 8:
        raise gzip.BadGzipFile("Unknown compression method")
    flags = data[pos + 3]
    pos += 10
    if flags & _FEXTRA:
        if len(data) < pos + 2:
            raise _gzip_truncated()
        pos += 2 + int.from_bytes(data[pos : pos + 2], "little")
        if len(data) < pos:
            raise _gzip_truncated()
    for field in (_FNAME, _FCOMMENT):
        if flags & field:  # ends at a zero byte, or at the end of the data
            end = data.find(b"\0", pos)
            pos = len(data) if end < 0 else end + 1
    if flags & _FHCRC:
        pos += 2
        if len(data) < pos:
            raise _gzip_truncated()
    return pos


def _gunzip_capped(data: bytes, limit: int, cost: _FetchCost) -> bytes:
    """Decode a gzip body as ``GzipFile`` does, within ``limit``.

    Member by member, as ``gzip._GzipReader`` reads them: the header as
    `_gzip_header_end` parses it, the deflate data inflated raw, then the
    CRC-32 and length in the trailer checked, and any zero bytes after a
    member skipped. An empty body decodes to nothing. What it returns, and
    which bodies it rejects, match ``GzipFile``; two things differ, both in
    what decoding costs. Each member is charged to ``cost``
    (`_GZIP_MEMBER_COST_BYTES`), so a body of many tiny members is refused
    rather than decoded member after member. Zero padding is skipped in one
    step, not one byte at a time.
    """
    out: list = []
    pos = 0
    while pos < len(data):
        cost.members += 1
        cost.check(limit)
        first = len(out)
        end = _inflate(
            zlib.decompressobj(-zlib.MAX_WBITS),
            data,
            _gzip_header_end(data, pos),
            limit,
            cost,
            out,
        )
        if end is None or len(data) < end + 8:
            raise _gzip_truncated()
        crc, size = struct.unpack_from("<II", data, end)
        member_crc = 0
        member_size = 0
        for chunk in out[first:]:
            # Validate the RFC 1952 trailer checksum for each GZIP member.
            # bearer:disable python_lang_weak_hash_crc32
            member_crc = zlib.crc32(chunk, member_crc)
            member_size += len(chunk)
        if crc != member_crc:
            raise gzip.BadGzipFile("CRC check failed")
        if size != member_size & 0xFFFFFFFF:
            raise gzip.BadGzipFile("Incorrect length of data produced")
        pos = _ZERO_BYTES.match(data, end + 8).end()
    return b"".join(out)


def _decoded_body(response, limit: int, cost: _FetchCost) -> bytes:
    """Read and decode ``response``'s body as WeasyPrint's `URLFetcher` would.

    Refuses it with `PDFResourceTooLargeError` once what it costs
    (`_FetchCost`) passes ``limit``, counted as it is read and decoded. A
    declared ``Content-Length`` past the limit is refused before any of the
    body is read. The decoding follows ``URLFetcher.fetch``
    (``weasyprint/urls.py``): the first ``Content-Encoding`` header, compared
    exactly; ``gzip`` as ``GzipFile`` decodes it (`_gunzip_capped`);
    ``deflate`` as a zlib stream, retried as raw deflate; anything else
    passed through as it is.
    """
    data = _read_body(response, limit, cost)
    encoding = response.headers.get("Content-Encoding")
    if encoding == "gzip":
        return _gunzip_capped(data, limit, cost)
    if encoding == "deflate":
        try:
            return _inflate_capped(data, zlib.MAX_WBITS, limit, cost)
        except zlib.error:
            return _inflate_capped(data, -zlib.MAX_WBITS, limit, cost)
    return data


def _pillow_raster_format(body: bytes) -> Optional[str]:
    """The Pillow format name for ``body``'s header, or None.

    Use registered ``accept`` sniffers where available. Pillow also opens
    formats without a sniffer: rule those out by a reviewed prefix
    (`_PREFIX_RULED_RASTER_FORMATS`) or by running their reviewed
    header-only factory (`_HEADER_ONLY_RASTER_FORMATS`) on the already
    byte-capped body. Never open an unreviewed format: some (ICO, ICNS, BLP)
    decode pixels during ``_open``. A header-only factory or accept sniffer
    that raises any exception other than the non-matches ``Image.open``
    skips (``SyntaxError``, ``IndexError``, ``TypeError``, ``struct.error``)
    fails closed: its format is returned, so the body is refused with the
    guard's own warning. Non-raster bodies (SVG, ICC profiles,
    stylesheets, TTF/OTF fonts, text) return None.
    """
    from PIL import Image

    Image.init()  # populate Image.OPEN with every plugin (idempotent)
    prefix = bytes(body[:32])
    for fmt, (factory, accept) in list(Image.OPEN.items()):
        if accept is None:
            ruled_by = _PREFIX_RULED_RASTER_FORMATS.get(fmt)
            if ruled_by is not None:
                if prefix.startswith(ruled_by):
                    return fmt
                continue
            if fmt not in _HEADER_ONLY_RASTER_FORMATS:
                # Cannot rule out this format without running an unreviewed
                # decoder. Refuse the resource, leaving the export usable.
                return fmt
            try:
                with io.BytesIO(body) as stream, factory(stream):
                    return fmt
            except (SyntaxError, IndexError, TypeError, struct.error):
                # Same non-matches Image.open skips while trying plugins.
                continue
            except Exception:
                # Image.open would raise these rather than try the next
                # plugin: fail closed under this format's name.
                return fmt
        try:
            matched = accept(prefix)
        except (SyntaxError, IndexError, TypeError, struct.error):
            # Same non-matches Image.open skips while trying plugins.
            continue
        except Exception:
            # Image.open would raise these rather than try the next plugin:
            # fail closed under this format's name.
            return fmt
        # A str result is a reason the format was *not* accepted (e.g. WebP
        # support missing), which Image.open treats as no match.
        if matched and not isinstance(matched, str):
            return fmt
    return None


def _declared_image_pixels(body: bytes, fmt: str) -> Optional[int]:
    """The pixel count an allow-listed raster image declares in its header.

    For a JPEG, the declared area padded to whole MCUs (`_jpeg_mcu_padded`),
    which is what libjpeg decodes; both caps compare against this count.

    Runs the identified format's own factory (``Image.OPEN[fmt][0]``), not
    ``Image.open``: so no other format's decoder is tried if this header
    fails, and the ``_decompression_bomb_check`` that ``Image.open`` runs
    after the factory (which warns past ~89 M pixels) is not reached. A
    factory may still run that check itself: GIF's ``_seek`` does when a
    frame extends past the logical screen, and enlarges the size it reports
    to cover the frame, so the cap below still sees the enlarged size. PNG
    animation headers can also emit a warning. Nothing here
    touches the process-wide ``warnings`` filters, which
    ``warnings.catch_warnings`` would, unsafely, while exports run in other
    threads; the cap below answers an oversized declaration instead.

    No factory decodes pixels, but not all of them read only a fixed-size
    header. PNG reads every chunk before its first image data, one Python
    step per chunk except the ones whose payload decompresses (``iCCP``,
    ``zTXt``, compressed ``iTXt``), which `_png_structure_refusal` bounds
    before this probe runs by decompressing them itself within
    `_MAX_PNG_DECOMPRESSED_BYTES`. GIF reads every block before its
    first image descriptor, one Python step each (Pillow 12.3 also joins
    comment sub-blocks by repeated concatenation, quadratic in their
    number), so `_gif_structure_refusal` bounds a GIF's blocks before this
    probe runs. JPEG reads every marker segment before its first scan, and
    is not linear in them either: it joins Exif segments by repeated
    concatenation, and reads and decodes the TIFF directories of its Exif
    and MPF data however many tags share the same bytes;
    `_jpeg_structure_refusal` bounds those before this probe runs. WebP
    hands the body to libwebp's demuxer and animation decoder, which
    allocates a canvas buffer for the declared size before this cap is
    compared: in Pillow 12.3 up to 16383x16383 RGBA, about 1 GiB of virtual
    memory (mostly never touched); Pillow 13.0.0 first reads the declared
    size from the header and raises past twice its own ``MAX_IMAGE_PIXELS``
    (about 179 M pixels, a 0.7 GB canvas). Returns None if the header
    cannot be read (an allocation failure included), so the caller can
    refuse it.
    """
    from PIL import Image

    Image.init()  # idempotent; Image.OPEN may not be populated yet
    opener = Image.OPEN.get(fmt)
    if opener is None:
        return None
    try:
        with opener[0](io.BytesIO(body), "") as image:
            width, height = image.size
            if fmt == "JPEG":
                width, height = _jpeg_mcu_padded(image, width, height)
    except Exception:
        return None
    return width * height


def _jpeg_mcu_padded(image, width: int, height: int) -> tuple[int, int]:
    """``width`` x ``height`` rounded up to whole MCUs of the JPEG ``image``.

    An MCU is 8 pixels times the largest horizontal and vertical sampling
    factor of the frame's components; libjpeg decodes whole MCUs. Pillow's
    ``layer`` holds each component as (id, high nibble, low nibble, table)
    of its sampling byte, which the JPEG standard defines as horizontal,
    then vertical (Pillow's own comment names them the other way round).
    """
    layers = getattr(image, "layer", None) or [(0, 1, 1, 0)]
    mcu_h = 8 * max(max(layer[1] for layer in layers), 1)
    mcu_v = 8 * max(max(layer[2] for layer in layers), 1)
    return -(-width // mcu_h) * mcu_h, -(-height // mcu_v) * mcu_v


class _MalformedImage(ValueError):
    """An image structure a pre-scan cannot walk within its caps."""


def _gif_sub_blocks(body: bytes, pos: int, limit: int) -> tuple[int, int, int]:
    """Skip the data sub-blocks at ``pos``, up to and past their terminator.

    Returns ``(end, count, size)``: the offset after the terminator, how many
    sub-blocks there were and how many bytes they carry. Stops after
    ``limit + 1`` sub-blocks, returning that count, so a caller's cap bounds
    the walk. Only offsets are followed; no sub-block is copied. Raises
    `_MalformedImage` if a sub-block or the terminator lies past the body.
    """
    end = len(body)
    count = size = 0
    while count <= limit:
        if pos >= end:
            raise _MalformedImage("truncated")
        length = body[pos]
        pos += 1
        if length == 0:
            return pos, count, size
        pos += length
        if pos > end:
            raise _MalformedImage("truncated")
        count += 1
        size += length
    return pos, count, size


class _GifWalk:
    """The counts `_walk_gif` checks against the ``_MAX_GIF_*`` caps."""

    def __init__(self) -> None:
        self.frames = self.complete_frames = self.blocks = 0
        self.extension_blocks = self.comment_blocks = self.comment_bytes = 0

    def over_cap(self) -> Optional[str]:
        """Why the counts so far are refused, or None."""
        if (
            self.frames <= _MAX_GIF_FRAMES
            and self.blocks <= _MAX_GIF_BLOCKS
            and self.extension_blocks <= _MAX_GIF_EXTENSION_BLOCKS
            and self.comment_blocks <= _MAX_GIF_COMMENT_BLOCKS
            and self.comment_bytes <= _MAX_GIF_COMMENT_BYTES
        ):
            return None  # the common case, checked once per block
        for count, cap, what in (
            (self.frames, _MAX_GIF_FRAMES, "frames"),
            (self.blocks, _MAX_GIF_BLOCKS, "blocks"),
            (
                self.extension_blocks,
                _MAX_GIF_EXTENSION_BLOCKS,
                "extension blocks",
            ),
            (self.comment_blocks, _MAX_GIF_COMMENT_BLOCKS, "comment blocks"),
            (self.comment_bytes, _MAX_GIF_COMMENT_BYTES, "bytes of comments"),
        ):
            if count > cap:
                return f"a GIF image with more than {cap} {what}"
        return None


def _gif_block(body: bytes, pos: int, walk: _GifWalk) -> Optional[int]:
    """Walk the block at ``pos``; the offset after it, or None at the end.

    Counts what it walks into ``walk``, stopping a sub-block walk once a
    cap is passed. Raises `_MalformedImage` for a block it cannot walk.
    """
    end = len(body)
    if pos >= end:
        raise _MalformedImage("truncated")
    introducer = body[pos]
    pos += 1
    walk.blocks += 1
    if introducer == 0x3B:  # trailer
        if not walk.complete_frames:
            raise _MalformedImage("no image")
        return None
    if introducer == 0x21:  # extension: label, then sub-blocks
        if pos >= end:
            raise _MalformedImage("truncated")
        is_comment = body[pos] == 0xFE
        pos += 1
        walk.extension_blocks += 1
        limit = min(
            _MAX_GIF_BLOCKS - walk.blocks,
            _MAX_GIF_EXTENSION_BLOCKS - walk.extension_blocks,
        )
        if is_comment:
            walk.comment_blocks += 1
            limit = min(limit, _MAX_GIF_COMMENT_BLOCKS - walk.comment_blocks)
        pos, count, size = _gif_sub_blocks(body, pos, max(limit, 0))
        walk.blocks += count
        walk.extension_blocks += count
        if is_comment:
            walk.comment_blocks += count
            walk.comment_bytes += size
        return pos
    if introducer == 0x2C:  # image descriptor
        walk.frames += 1
        if walk.frames > _MAX_GIF_FRAMES:
            return pos
        if pos + 9 > end:
            raise _MalformedImage("truncated")
        flags = body[pos + 8]
        pos += 9
        if flags & 0x80:
            pos += 3 << ((flags & 7) + 1)
        pos += 1  # LZW minimum code size
        if pos > end:
            raise _MalformedImage("truncated")
        pos, count, _ = _gif_sub_blocks(
            body, pos, max(_MAX_GIF_BLOCKS - walk.blocks, 0)
        )
        walk.blocks += count
        if walk.blocks <= _MAX_GIF_BLOCKS:
            walk.complete_frames += 1
        return pos
    raise _MalformedImage(f"unknown block 0x{introducer:02X}")


def _walk_gif(body: bytes) -> Optional[str]:
    """`_gif_structure_refusal`'s walk: a cap a body is past, or None.

    Raises `_MalformedImage` for a structure it cannot walk before its
    first complete frame. After that, the walk just ends there: Pillow reads
    only frame 0 for WeasyPrint, so nothing it cannot walk is parsed.
    """
    end = len(body)
    if end < 13:
        raise _MalformedImage("truncated")
    pos: Optional[int] = 13
    flags = body[10]
    if flags & 0x80:
        pos = 13 + (3 << ((flags & 7) + 1))
    walk = _GifWalk()
    while pos is not None:
        if pos == end and walk.complete_frames:
            return None  # no trailer, but no block is cut short
        try:
            pos = _gif_block(body, pos, walk)
        except _MalformedImage:
            if walk.complete_frames:
                return None
            raise
        reason = walk.over_cap()
        if reason is not None:
            return reason
    return None


def _gif_structure_refusal(body: bytes) -> Optional[str]:
    """Why a GIF body's block structure is refused, or None if it is not.

    Walks the structure by byte offsets alone -- the logical screen
    descriptor and global colour table, then each extension and its data
    sub-blocks and each image descriptor, local colour table and image data
    sub-blocks, to the trailer -- before Pillow parses any of it. Refuses a
    body past `_MAX_GIF_FRAMES`, `_MAX_GIF_BLOCKS`,
    `_MAX_GIF_EXTENSION_BLOCKS`, `_MAX_GIF_COMMENT_BLOCKS` or
    `_MAX_GIF_COMMENT_BYTES`. Before the first complete frame, which is all
    WeasyPrint draws, it also refuses (fails closed) a structure it cannot
    walk: a block or table past the end of the body, an unknown block type,
    or a trailer with no image. After it, the walk ends at the trailer, at
    the end of the body, or at whatever it cannot walk, and accepts. Every
    step either advances past a counted block or stops, so the walk is
    linear and bounded by the caps.
    """
    try:
        return _walk_gif(body)
    except _MalformedImage as exc:
        return f"a GIF image whose block structure is unreadable ({exc})"


class _TiffWalk:
    """What `_tiff_refusal` has charged against the ``_MAX_TIFF_*`` caps."""

    def __init__(self) -> None:
        self.entries = self.data_bytes = self.numbers = 0

    def over_cap(self, what: str) -> Optional[str]:
        """``what`` names the data: ``"a JPEG image whose MPF data"``."""
        if self.entries > _MAX_TIFF_IFD_ENTRIES:
            return (
                f"{what} has more than "
                f"{_MAX_TIFF_IFD_ENTRIES} directory entries"
            )
        if self.data_bytes > _MAX_TIFF_IFD_DATA_BYTES:
            return (
                f"{what} declares more than "
                f"{_MAX_TIFF_IFD_DATA_BYTES} bytes of tag data"
            )
        if self.numbers > _MAX_TIFF_IFD_NUMBERS:
            return (
                f"{what} declares more than "
                f"{_MAX_TIFF_IFD_NUMBERS} numeric tag values"
            )
        return None


def _tiff_directory(
    data: bytes, start: int, stop: int, at: int, order: str, walk: _TiffWalk
) -> dict:
    """Charge the TIFF directory at ``start + at`` to ``walk``.

    Mirrors what Pillow's ``ImageFileDirectory_v2.load`` reads: the entry
    count, then as many 12-byte entries as fit before ``stop``. Each entry
    of a type Pillow loads is charged its declared data past four bytes
    (bounded by the TIFF data's length, as Pillow's read is). Returns the
    first integer values of pointer entries, grouped by tag. Account for
    each supported directory reference so duplicate-tag selection across
    supported Pillow versions does not change the budget. Only offsets
    are followed; nothing is copied.
    """
    pos = start + at
    if at < 0 or pos + 2 > stop:
        return {}  # Pillow's read of the entry count fails: nothing loaded
    (count,) = struct.unpack_from(order + "H", data, pos)
    count = min(count, (stop - pos - 2) // 12)
    walk.entries += count
    if walk.entries > _MAX_TIFF_IFD_ENTRIES:
        return {}
    values = {}
    for entry in range(pos + 2, pos + 2 + 12 * count, 12):
        tag, kind, items = struct.unpack_from(order + "HHL", data, entry)
        unit = _TIFF_TYPE_SIZES.get(kind)
        if unit is None:
            continue
        size = items * unit
        data_at = entry + 8
        if size > 4:
            walk.data_bytes += min(size, stop - start)
            if kind not in _TIFF_BYTE_STRING_TYPES:
                walk.numbers += min(items, (stop - start) // unit)
            (offset,) = struct.unpack_from(order + "L", data, entry + 8)
            data_at = start + offset
        elif kind not in _TIFF_BYTE_STRING_TYPES:
            walk.numbers += items
        if (
            items
            and tag in (0x8769, 0x8825, 0xA005)
            and kind in _TIFF_INTEGER_CODES
            and data_at + size <= stop
        ):
            (value,) = struct.unpack_from(
                order + _TIFF_INTEGER_CODES[kind], data, data_at
            )
            values.setdefault(tag, []).append(value)
    return values


def _tiff_refusal(
    data: bytes, start: int, stop: int, what: str, *, exif: bool
) -> Optional[str]:
    """Why the TIFF data at ``data[start:stop]`` is refused, or None.

    Walks the first directory, and for Exif (``exif``) the Exif, GPS and
    Interop directories Pillow's ``Exif.tobytes`` loads through it
    (`_EXIF_IFD_POINTERS`), charging them all to one `_TiffWalk`. Data
    Pillow cannot read as a classic TIFF directory fails there at once, so
    it is not refused here.
    """
    if stop - start < 8:
        return None
    order = _TIFF_CLASSIC_HEADERS.get(data[start : start + 4])
    if order is None:
        return None
    walk = _TiffWalk()
    (first,) = struct.unpack_from(order + "L", data, start + 4)
    # Directory entries, including repeated references, share one budget.
    pending: list = [(None, first)]
    while pending:
        parent, at = pending.pop()
        values = _tiff_directory(data, start, stop, at, order, walk)
        reason = walk.over_cap(what)
        if reason is not None:
            return reason
        if exif:
            for pointer in _EXIF_IFD_POINTERS.get(parent, ()):
                pending.extend((pointer, at) for at in values.get(pointer, ()))
    return None


class _JpegWalk:
    """What `_walk_jpeg` counts against the ``_MAX_JPEG_*`` caps."""

    def __init__(self) -> None:
        self.segments = self.padding = self.frames = 0
        self.scans = 0
        self.scan_data = 0  # where the first scan's entropy-coded data starts
        self.exif: list = []  # (start, stop) of each Exif segment's payload
        self.exif_bytes = 0
        self.refusal: Optional[str] = None  # an MPF segment's, if any

    def over_cap(self) -> Optional[str]:
        if self.refusal is not None:
            return self.refusal
        for count, cap, what in (
            (self.segments, _MAX_JPEG_SEGMENTS, "marker segments"),
            (self.padding, _MAX_JPEG_PADDING_BYTES, "padding bytes"),
            (self.frames, _MAX_JPEG_FRAME_HEADERS, "frame headers"),
            (self.scans, _MAX_JPEG_SCANS, "scans"),
            (len(self.exif), _MAX_JPEG_EXIF_SEGMENTS, "Exif segments"),
            (self.exif_bytes, _MAX_JPEG_EXIF_BYTES, "bytes of Exif"),
        ):
            if count > cap:
                return f"a JPEG image with more than {cap} {what}"
        return None


def _exif_refusal(
    data: bytes, start: int, stop: int, image: str, headers: int = 0
) -> Optional[str]:
    """Why the Exif data ``data[start:stop]`` of ``image`` is refused, or None.

    Mirrors Pillow's ``Exif.load``, which strips leading ``Exif\\0\\0``
    headers and reads the rest as TIFF data: this skips every leading
    header (``headers`` counts those Pillow itself put in front, as it does
    for PNG's ``eXIf``), refusing past `_MAX_JPEG_EXIF_HEADERS`, then checks
    the rest (`_tiff_refusal`). Pillow 12.3 strips every header too, one
    slice at a time; 13.0.0 strips at most two and fails to read a third
    as TIFF, so it reads TIFF data only where this checks it. ``image``
    names the image in the reason.
    """
    while data.startswith(b"Exif\x00\x00", start, stop):
        headers += 1
        start += 6
        if headers > _MAX_JPEG_EXIF_HEADERS:
            return (
                f"{image} whose Exif data repeats its header more than "
                f"{_MAX_JPEG_EXIF_HEADERS} times"
            )
    return _tiff_refusal(
        data, start, stop, f"{image} whose Exif data", exif=True
    )


def _jpeg_exif_refusal(body: bytes, parts: list) -> Optional[str]:
    """Why the Exif data Pillow joins from ``parts`` is refused, or None.

    Pillow keeps the first ``Exif\\0\\0`` segment whole and appends the rest
    without their header. One segment is walked in place; several are
    joined once, within `_MAX_JPEG_EXIF_BYTES`, as Pillow's walk will see
    them.
    """
    if len(parts) == 1:
        data, (start, stop) = body, parts[0]
    else:
        first, *rest = parts
        data = body[first[0] : first[1]] + b"".join(
            body[a + 6 : b] for a, b in rest
        )
        start, stop = 0, len(data)
    return _exif_refusal(data, start, stop, "a JPEG image")


def _jpeg_segment(body: bytes, pos: int, walk: _JpegWalk) -> int:
    """Walk the marker or padding byte at ``pos``; the offset after it.

    Steps as Pillow's ``JpegImageFile._open`` does: a stray byte, a fill
    byte (0xFF before another 0xFF) or a stuffed 0xFF00 is padding; a
    standalone marker is two bytes; any other known marker is followed by a
    segment whose two-byte length is read and checked against the body.
    Returns -1 after the first scan's header (SOS), where Pillow's open
    stops. Raises `_MalformedImage` past the body or at a marker Pillow
    would refuse, and also (failing closed) at a segment length of 0 or 1,
    which no valid segment has: there Pillow reads an empty segment and
    goes on after the length, so the walk does not mirror it.
    """
    end = len(body)
    if pos + 1 >= end:
        raise _MalformedImage("truncated before the first scan")
    marker = body[pos + 1]
    if body[pos] != 0xFF or marker == 0xFF:
        walk.padding += 1
        return pos + 1
    if marker == 0x00:
        walk.padding += 1
        return pos + 2
    walk.segments += 1
    if marker in _JPEG_STANDALONE_MARKERS:
        return pos + 2
    if marker not in _JPEG_SEGMENT_MARKERS:
        raise _MalformedImage(f"unknown marker 0xFF{marker:02X}")
    if pos + 4 > end:
        raise _MalformedImage("truncated before the first scan")
    length = (body[pos + 2] << 8) | body[pos + 3]
    start, stop = pos + 4, pos + 2 + length
    if length < 2:
        raise _MalformedImage(f"segment length {length}")
    if stop > end:
        raise _MalformedImage("truncated before the first scan")
    if marker in _JPEG_FRAME_MARKERS:
        walk.frames += 1
    if marker == 0xDA:  # start of scan
        walk.scans = 1
        walk.scan_data = stop
        return -1
    if marker == 0xE1 and body.startswith(b"Exif\x00\x00", start, stop):
        walk.exif.append((start, stop))
        walk.exif_bytes += stop - start - (6 if len(walk.exif) > 1 else 0)
    elif marker == 0xE2 and body.startswith(b"MPF\x00", start, stop):
        walk.refusal = _tiff_refusal(
            body, start + 4, stop, "a JPEG image whose MPF data", exif=False
        )
    return stop


def _jpeg_scans(body: bytes, pos: int, walk: _JpegWalk) -> None:
    """Count the markers from ``pos``, the first scan's data, to EOI.

    Steps over entropy-coded data as libjpeg does (`_JPEG_SCAN_MARKER`),
    then over each marker: a segment by its length (libjpeg skips at least
    the two length bytes), any other marker as two bytes. Counts every
    marker against `_MAX_JPEG_SEGMENTS` and every SOS against
    `_MAX_JPEG_SCANS`, and stops at EOI, at the end of the body, at a
    segment cut short or once a cap is passed. Where libjpeg would stop at
    an error instead, this walk goes on, so it counts no fewer scans.
    """
    end = len(body)
    while walk.over_cap() is None:
        found = _JPEG_SCAN_MARKER.search(body, pos)
        if found is None:
            return
        pos = found.start()
        marker = body[pos + 1]
        if marker == 0xD9:  # end of image: libjpeg reads no further
            return
        walk.segments += 1
        if marker not in _JPEG_SEGMENT_MARKERS:
            pos += 2
            continue
        if pos + 4 > end:
            return
        if marker == 0xDA:
            walk.scans += 1
        pos += 2 + max((body[pos + 2] << 8) | body[pos + 3], 2)


def _walk_jpeg(body: bytes, walk: _JpegWalk) -> Optional[str]:
    """`_jpeg_structure_refusal`'s walk: a cap a body is past, or None."""
    pos = 2  # Pillow's open starts at the 0xFF after SOI
    while pos >= 0:
        pos = _jpeg_segment(body, pos, walk)
        reason = walk.over_cap()
        if reason is not None:
            return reason
    _jpeg_scans(body, walk.scan_data, walk)
    reason = walk.over_cap()
    if reason is not None:
        return reason
    if walk.exif:
        return _jpeg_exif_refusal(body, walk.exif)
    return None


def _jpeg_structure_refusal(
    body: bytes, walk: Optional[_JpegWalk] = None
) -> Optional[str]:
    """Why a JPEG body's header is refused, or None if it is not.

    ``walk``, if given, is left holding what the walk counted (its
    ``scans`` for `_jpeg_budget_charge`).

    Walks the marker segments from SOI to the first scan (SOS) by offsets,
    stepping over padding as Pillow does, then the markers between scans to
    EOI as libjpeg's decoder finds them (`_jpeg_scans`). Refuses a header
    past `_MAX_JPEG_SEGMENTS`, `_MAX_JPEG_PADDING_BYTES`,
    `_MAX_JPEG_FRAME_HEADERS`, `_MAX_JPEG_EXIF_SEGMENTS`,
    `_MAX_JPEG_EXIF_BYTES` or `_MAX_JPEG_EXIF_HEADERS`, an image past
    `_MAX_JPEG_SCANS` or (with the header's) `_MAX_JPEG_SEGMENTS`, Exif or
    MPF data whose TIFF directories pass `_MAX_TIFF_IFD_ENTRIES`,
    `_MAX_TIFF_IFD_DATA_BYTES` or `_MAX_TIFF_IFD_NUMBERS`, and (failing
    closed) a header cut short before the first scan or holding a marker
    Pillow would refuse. Every step advances past a counted byte or
    segment, or (in entropy-coded data) is a regular-expression search, so
    the walk is linear and bounded by the caps.
    """
    try:
        return _walk_jpeg(body, _JpegWalk() if walk is None else walk)
    except _MalformedImage as exc:
        return f"a JPEG image whose header is unreadable ({exc})"


def _jpeg_budget_charge(pixels: int, scans: int) -> int:
    """What a JPEG of ``pixels`` (MCU-padded) and ``scans`` scans is charged
    against the export's pixel budget: its pixels, scaled by its scans past
    `_JPEG_BUDGET_SCANS` (libjpeg passes over the image once per scan)."""
    return pixels * max(scans, _JPEG_BUDGET_SCANS) // _JPEG_BUDGET_SCANS


def _png_text_key(body: bytes, start: int, stop: int) -> bytes:
    """The keyword the text chunk ``body[start:stop]`` maps to in Pillow's
    ``info``.

    Pillow's ``chunk_tEXt``, ``chunk_zTXt`` and ``chunk_iTXt`` split the
    chunk at its first NUL; ``tEXt`` and ``zTXt`` fall back to the whole
    chunk as the keyword when there is none (their "broken tEXt tags"), and
    ``iTXt`` stores nothing its header cannot split. The walk keys every
    flavour the same way, and refuses an ``exif`` keyword even in an
    unsplittable ``iTXt`` chunk Pillow would drop -- erring in the safe
    direction only.
    """
    sep = body.find(b"\0", start, stop)
    if sep < 0:
        return body[start:stop]
    return body[start:sep]


def _png_itxt_payload(body: bytes, start: int, stop: int) -> Optional[tuple]:
    """Where ``chunk_iTXt`` decompresses its text from, or None if it does
    not decompress (or would store nothing at all).

    After the keyword and its NUL come a compression flag and method, then
    the language and translated keyword each NUL-terminated, then the text,
    a zlib stream when the flag is set and the method is 0. Mirrors
    Pillow's parse: a chunk whose header does not split this way stores
    nothing and costs no decompression.
    """
    sep = body.find(b"\0", start, stop)
    if sep < 0 or sep + 3 > stop:
        return None
    compressed, method = body[sep + 1], body[sep + 2]
    lang_end = body.find(b"\0", sep + 3, stop)
    if lang_end < 0:
        return None
    text = body.find(b"\0", lang_end + 1, stop)
    if text < 0:
        return None
    if compressed == 0 or method != 0:
        return None
    return text + 1, stop


def _png_inflated_bytes(
    body: bytes, start: int, stop: int, left: int
) -> Optional[int]:
    """What decompressing the zlib stream ``body[start:stop]`` is charged,
    or None if that is more than ``left``.

    Decompresses it a step at a time, as `_inflate` does: each step takes
    in at most `_INFLATE_INPUT_CHUNK_BYTES` and puts out at most
    `_RESOURCE_READ_CHUNK_BYTES`, and never more than ``left + 1`` bytes
    in all (never 0, which zlib reads as no limit). Each step is charged
    what it puts out as it returns, so what a stream puts out before zlib
    rejects it is charged too. A step zlib rejects returns nothing (CPython
    discards its output), so it is charged the most it may put out, and the
    stream ends there. The count stops one byte past ``left``, so the work
    is bounded by ``left`` plus the compressed bytes taken in, which the
    body bounds. Pillow decompresses each such chunk (``chunk_iCCP``,
    ``chunk_zTXt``, ``chunk_iTXt``) in one call of at most ``MAX_TEXT_CHUNK``
    and swallows a ``zlib.error``, so it decompresses no more of the stream
    than this charges. The charge is the stream's decompressed size when
    zlib accepts all of it; a payload zlib rejects outright, before any
    output, is charged a whole step.
    """
    inflater = zlib.decompressobj()
    view = memoryview(body)
    pos = start
    pending = b""
    charged = 0
    while not inflater.eof:
        if pending:
            piece = pending
        else:
            piece = view[pos : min(pos + _INFLATE_INPUT_CHUNK_BYTES, stop)]
            pos += len(piece)
        most = min(_RESOURCE_READ_CHUNK_BYTES, left + 1 - charged)
        try:
            out = inflater.decompress(piece, most)
        except zlib.error:
            charged += most
            break
        charged += len(out)
        if charged > left or (not out and not piece):
            break
        pending = inflater.unconsumed_tail
    return charged if charged <= left else None


def _png_structure_refusal(body: bytes) -> Optional[str]:
    """Why a PNG body's chunk structure is refused, or None if it is not.

    Walks the chunks after the signature by their lengths to IEND, the end
    of the body or a chunk cut short (which Pillow's reader cannot read
    either), and applies `_exif_refusal` to every chunk Pillow stores as
    ``info["exif"]``: ``eXIf``, to which Pillow prepends an
    ``Exif\\0\\0`` header, and ``tEXt`` keyed ``exif``, whose value it
    stores as is. Refuses outright a ``zTXt`` or ``iTXt`` chunk keyed
    ``exif``, whatever its value: Pillow decodes that value to text, which
    ``getexif`` hands to ``Exif.load`` as text (a TypeError there today,
    which WeasyPrint turns into a dropped image), and the walk relies on
    none of that rather than on what ``Exif.load`` rejects. A text chunk
    keyed ``Raw profile type exif`` is left alone: WeasyPrint never decodes
    it (see `_MAX_EXIF_CHUNKS`). Refuses a PNG past `_MAX_PNG_CHUNKS` or
    `_MAX_EXIF_CHUNKS`, and one whose compressed metadata (``iCCP``,
    ``zTXt``, compressed ``iTXt``) is charged more than
    `_MAX_PNG_DECOMPRESSED_BYTES` in all (`_png_inflated_bytes`).

    Each step advances past a counted chunk, and each payload that
    decompresses is decompressed once here, a step at a time and no further
    than one byte past what is left of `_MAX_PNG_DECOMPRESSED_BYTES`, so the
    walk is bounded by the caps. So is Pillow's decompression of the same
    chunks in each open of an accepted PNG (the size probe, WeasyPrint's
    open and its re-opens in ``get_x_object``), however they compress or
    fail: it decompresses no chunk past what the walk charged it.
    """
    end = len(body)
    pos = len(_PNG_SIGNATURE)
    chunks = exif = inflated = 0
    while pos + 8 <= end:
        chunks += 1
        if chunks > _MAX_PNG_CHUNKS:
            return f"a PNG image with more than {_MAX_PNG_CHUNKS} chunks"
        length, kind = struct.unpack_from(">L4s", body, pos)
        start = pos + 8
        stop = start + length
        if kind == b"IEND" or stop > end:
            return None
        headers = None
        pump = None  # the payload Pillow decompresses, where it decompresses
        if kind == b"eXIf":
            headers = 1
        elif kind == b"tEXt" and body.startswith(b"exif\x00", start, stop):
            headers, start = 0, start + 5
        elif kind == b"iCCP":
            # A keyword, a NUL, a compression-method byte, then the profile
            # as a zlib stream (Pillow's chunk_iCCP).
            sep = body.find(b"\0", start, stop)
            comp = start if sep < 0 else sep + 1
            pump = (comp + 1, stop) if comp < stop and body[comp] == 0 else None
        elif kind in _PNG_TEXT_CHUNK_KINDS:
            key = _png_text_key(body, start, stop)
            if key == b"exif" and kind != b"tEXt":
                # A zTXt or iTXt value keyed exif is decoded to text and
                # handed to Exif.load as text; refused rather than relied
                # on (see the docstring).
                which = "an" if kind == b"iTXt" else "a"
                return (
                    f"a PNG image with Exif in {which} {kind.decode()} chunk "
                    f"keyed {key.decode('latin-1')!r}"
                )
            if kind == b"zTXt":
                # A keyword, a NUL, a compression-method byte, then the text
                # as a zlib stream (Pillow's chunk_zTXt).
                sep = body.find(b"\0", start, stop)
                pump = (
                    (sep + 2, stop)
                    if sep >= 0 and body[sep + 1 : sep + 2] == b"\0"
                    else None
                )
            elif kind == b"iTXt":
                # (tEXt stores its value as is: nothing to decompress.)
                pump = _png_itxt_payload(body, start, stop)
        if pump is not None:
            more = _png_inflated_bytes(
                body, pump[0], pump[1], _MAX_PNG_DECOMPRESSED_BYTES - inflated
            )
            if more is None:
                return (
                    "a PNG image whose metadata chunks decompress to more "
                    f"than {_MAX_PNG_DECOMPRESSED_BYTES} bytes"
                )
            inflated += more
        if headers is not None:
            exif += 1
            if exif > _MAX_EXIF_CHUNKS:
                return (
                    f"a PNG image with more than {_MAX_EXIF_CHUNKS} Exif chunks"
                )
            reason = _exif_refusal(body, start, stop, "a PNG image", headers)
            if reason is not None:
                return reason
        pos = stop + 4  # the chunk's CRC
    return None


def _webp_structure_refusal(body: bytes) -> Optional[str]:
    """Why a WebP body's Exif data is refused, or None if it is not.

    Walks the RIFF chunks after the 12-byte header to the end of the body
    or a chunk cut short. libwebp's demuxer (1.6.0, ``ParseVP8XChunks``)
    steps over each chunk by its even-padded size except ``ANMF``, whose
    payload it enters (``ParseAnimationFrame`` reads the 16-byte frame
    header, ``StoreFrame`` the frame's image chunks) and from where it goes
    on parsing chunks as if at the top level. It does check the ``ANMF``
    size (rejecting one under 16 bytes, or frame image chunks that overrun
    it or the file); this walk ignores that size and enters every ``ANMF``,
    whatever the VP8X flags, so it visits every position the demuxer does
    and some it would reject. Applies
    `_exif_refusal` to every ``EXIF`` chunk (Pillow stores the first the
    demuxer keeps as ``info["exif"]``, as is), whether or not the demuxer
    would keep it. Refuses a WebP past `_MAX_WEBP_CHUNKS` or
    `_MAX_EXIF_CHUNKS`. Each step advances past a counted chunk, so the
    walk is linear and bounded by the caps.
    """
    end = len(body)
    pos = 12
    chunks = exif = 0
    while pos + 8 <= end:
        chunks += 1
        if chunks > _MAX_WEBP_CHUNKS:
            return f"a WebP image with more than {_MAX_WEBP_CHUNKS} chunks"
        kind, length = struct.unpack_from("<4sL", body, pos)
        start = pos + 8
        stop = start + length
        if kind == b"ANMF":
            pos = start + 16  # the frame header; its chunks follow
            continue
        if stop > end:
            return None
        if kind == b"EXIF":
            exif += 1
            if exif > _MAX_EXIF_CHUNKS:
                return (
                    f"a WebP image with more than {_MAX_EXIF_CHUNKS} "
                    "Exif chunks"
                )
            reason = _exif_refusal(body, start, stop, "a WebP image")
            if reason is not None:
                return reason
        pos = stop + (length & 1)
    return None


class _MalformedFont(ValueError):
    """A font container `_font_refusal` cannot read within its caps."""


def _sfnt_table_tags(body: bytes, offset: int) -> list:
    """The table tags of the sfnt table directory at ``offset``.

    Reads the fixed 12-byte header and ``numTables`` 16-byte records, each
    bounds-checked, and checks each table lies within the body. Raises
    `_MalformedFont` rather than reading past the body or past
    `_MAX_FONT_TABLES` records.
    """
    records = offset + 12
    if records > len(body):
        raise _MalformedFont("font table directory past the end of the body")
    (count,) = struct.unpack_from(">H", body, offset + 4)
    if count > _MAX_FONT_TABLES:
        raise _MalformedFont("font table directory too long")
    end = records + 16 * count
    if end > len(body):
        raise _MalformedFont("font table records past the end of the body")
    tags = []
    for record in range(records, end, 16):
        table_offset, length = struct.unpack_from(">II", body, record + 8)
        if table_offset + length > len(body):
            raise _MalformedFont("font table past the end of the body")
        tags.append(bytes(body[record : record + 4]))
    return tags


def _font_face_offsets(body: bytes) -> list:
    """Where each face's table directory starts: one face, or a collection's."""
    if body[:4] != _FONT_COLLECTION_TAG:
        return [0]
    if len(body) < 12:
        raise _MalformedFont("font collection header past the end of the body")
    (count,) = struct.unpack_from(">I", body, 8)
    if count > _MAX_FONT_COLLECTION_FACES:
        raise _MalformedFont("font collection has too many faces")
    if 12 + 4 * count > len(body):
        raise _MalformedFont("font collection offsets past the end of the body")
    return list(struct.unpack_from(f">{count}I", body, 12))


def _font_refusal(body: bytes) -> Optional[str]:
    """Why an sfnt or ``ttcf`` font body is refused, or None if it is not.

    Reads every face's table directory (pure ``struct``, bounded by
    `_MAX_FONT_TABLES` and `_MAX_FONT_COLLECTION_FACES`, every offset
    checked against the body) and refuses a font that has any of
    `_GLYPH_IMAGE_TABLES`. Every face of a collection is checked, since
    Fontconfig may install any of them. A directory that cannot be read
    within those bounds is refused too (fails closed): HarfBuzz may read
    more of a damaged font than this reader admits. The tags are compared
    in the records, not looked up by binary search as HarfBuzz does, so an
    unsorted directory cannot hide a table from this check.
    """
    try:
        for offset in _font_face_offsets(body):
            found = sorted(set(_sfnt_table_tags(body, offset)))
            images = [tag for tag in found if tag in _GLYPH_IMAGE_TABLES]
            if images:
                names = ", ".join(tag.decode("latin-1") for tag in images)
                return f"a font with image glyphs ({names.strip()})"
    except _MalformedFont as exc:
        return f"a font whose table directory is unreadable ({exc})"
    return None


def _refuse_image_or_font(url, reason: str) -> NoReturn:
    """Log (redacted) and raise for a body WeasyPrint would decode unbounded."""
    logger.warning(
        f"Refused {reason} in PDF rendering, which WeasyPrint would decode "
        f"without a size bound: {redact_url_for_log(url)}"
    )
    raise UnsupportedPDFResourceError(f"PDF resource refused: {reason}")


def _refuse_unbounded_resource(body: bytes, url) -> None:
    """Refuse a body WeasyPrint would decode without a size bound.

    Runs on the decoded body (after any ``Content-Encoding``), which is what
    WeasyPrint hands to fontTools, Fontconfig/HarfBuzz or Pillow. Refuses:

    - a compressed web font (``wOFF``/``wOF2``), decompressed with no limit;
    - an sfnt or ``ttcf`` font with colour-bitmap or SVG glyph tables
      (`_GLYPH_IMAGE_TABLES`), whose glyph images WeasyPrint opens with an
      unrestricted ``Image.open`` per drawn glyph, or whose table directory
      cannot be read within bounds (`_font_refusal`);
    - a Mac resource-fork (dfont) font, which HarfBuzz also reads tables
      from but this guard does not parse;
    - a GIF or JPEG whose block or marker structure is past the
      `_MAX_GIF_*` or `_MAX_JPEG_*`/`_MAX_TIFF_*` caps or cannot be walked
      (`_gif_structure_refusal`, `_jpeg_structure_refusal`), a PNG with a
      ``zTXt``/``iTXt`` chunk keyed ``exif``, or a PNG or WebP whose
      chunks or Exif data are past `_MAX_PNG_CHUNKS`, `_MAX_WEBP_CHUNKS`,
      `_MAX_EXIF_CHUNKS` or the Exif caps, or whose compressed metadata
      decompresses past `_MAX_PNG_DECOMPRESSED_BYTES` (as
      `_png_inflated_bytes` charges it)
      (`_png_structure_refusal`, `_webp_structure_refusal`), checked before
      Pillow parses it;
    - a raster image whose format is not allow-listed;
    - a raster image whose header declares more than `MAX_PDF_IMAGE_PIXELS`
      (a JPEG: its MCU-padded area);
    - an allow-listed image once the export's `MAX_PDF_EXPORT_IMAGE_PIXELS`
      decode budget is spent. Each accepted image is charged its pixels
      against that budget (declared; a JPEG its MCU-padded area, scaled by its
      scans past `_JPEG_BUDGET_SCANS`: `_declared_image_pixels`,
      `_jpeg_budget_charge`), so many images that are cheap on the wire
      (a flat colour, a simple chart) but costly to decode cannot add up to
      unbounded total decode work. WeasyPrint keeps no raw image resident --
      its `RasterImage` retains only re-encoded, compressed bytes and decodes
      one image at a time -- so this bounds decode work (in pixels of
      ordinary decoding), not resident RAM.

    Everything else -- SVG, an ICC colour profile, a stylesheet, a TTF/OTF
    font with outline glyphs only, plain text -- passes. The byte cap bounds
    such a font's file, not the work FreeType and HarfBuzz do shaping and
    drawing it (COLR vector glyphs included). Raising
    `UnsupportedPDFResourceError` fails the fetch exactly as an over-cap body
    does: skipped, and the export completes with the resource left out. The
    warning names no URL beyond scheme://host:port.
    """
    magic = body[:4]
    if magic in _COMPRESSED_FONT_MAGIC:
        # WeasyPrint sends wOFF to zlib and wOF2 to brotli with no size limit
        # (weasyprint/text/fonts.py); the export falls back to local fonts.
        _refuse_image_or_font(url, "a compressed web font")
    if magic in _SFNT_VERSION_TAGS or magic == _FONT_COLLECTION_TAG:
        # An uncompressed font: its outlines are bounded by the byte cap, its
        # glyph images are not (see _GLYPH_IMAGE_TABLES).
        reason = _font_refusal(body)
        if reason is not None:
            _refuse_image_or_font(url, reason)
    # Before any Pillow parse, the size probe's included: Pillow's GIF and
    # JPEG header parsers, and its Exif loading for JPEG, PNG and WebP, are
    # superlinear in some structures (see the _MAX_GIF_*, _MAX_JPEG_* and
    # _MAX_TIFF_* caps).
    reason = None
    jpeg = _JpegWalk()
    if body[:6] in _GIF_SIGNATURES:
        reason = _gif_structure_refusal(body)
    elif body[:3] == _JPEG_SIGNATURE:
        reason = _jpeg_structure_refusal(body, jpeg)
    elif body[:8] == _PNG_SIGNATURE:
        reason = _png_structure_refusal(body)
    elif body[:4] == b"RIFF" and body[8:12] == b"WEBP":
        reason = _webp_structure_refusal(body)
    if reason is not None:
        _refuse_image_or_font(url, reason)
    fmt = _pillow_raster_format(body)
    if fmt is None:
        if magic == _RESOURCE_FORK_FONT_TAG:
            # Pillow's ICO sniffer claims this prefix first; refused here too
            # in case that plugin is unavailable.
            _refuse_image_or_font(url, "a resource-fork (dfont) font")
        # Not a raster Pillow decodes: SVG, ICC profile, stylesheet, TTF/OTF,
        # text. Left for WeasyPrint to handle.
        return
    if fmt not in _ALLOWED_RASTER_FORMATS:
        # A raster Pillow would decode but we do not bound (its declared size
        # is not read here, and some formats decode on open).
        _refuse_image_or_font(
            url, f"a {fmt} image (not one of PNG, JPEG, GIF, WebP or SVG)"
        )
    pixels = _declared_image_pixels(body, fmt)
    if pixels is None:
        # Do not pass a body with an unverified size to the renderer, whose
        # unrestricted Image.open may try other formats after this fails.
        _refuse_image_or_font(url, f"a {fmt} image with an unreadable header")
    if pixels > MAX_PDF_IMAGE_PIXELS:
        _refuse_image_or_font(
            url,
            f"a {pixels}-pixel {fmt} image, over the "
            f"{MAX_PDF_IMAGE_PIXELS}-pixel per-image limit",
        )
    charge = pixels
    if fmt == "JPEG":
        charge = _jpeg_budget_charge(pixels, jpeg.scans)
    budget = _export_resource_budget.get()
    if budget is not None and not budget.charge_image_pixels(charge):
        # Warn once per export, not once per image: a report can reference any
        # number. Every over-budget image is still refused (and dropped).
        if not budget.image_budget_reported:
            budget.image_budget_reported = True
            logger.warning(
                "Refused a raster image in PDF rendering: the export's "
                f"{MAX_PDF_EXPORT_IMAGE_PIXELS}-pixel image-decode budget is "
                f"spent, so further images are refused: "
                f"{redact_url_for_log(url)}"
            )
        raise UnsupportedPDFResourceError(
            "PDF export image-decode budget of "
            f"{MAX_PDF_EXPORT_IMAGE_PIXELS} pixels is spent"
        )


class _BoundedBodyProcessor(urllib.request.BaseHandler):
    """Reads each http(s) body `_URL_FETCHER` receives, within the limits.

    WeasyPrint's ``URLFetcher.fetch`` sets no size limit: it decodes a
    deflate body in full inside the fetch, and returns a plain or gzip body
    as a stream that its consumers read to the end after the fetch has
    returned. This response processor runs inside the fetch instead: it
    reads and decodes the body (`_decoded_body`), drops the
    ``Content-Encoding`` header so ``fetch`` does not decode it a second
    time, and passes the decoded bytes on in memory.

    A body that costs more than `MAX_PDF_RESOURCE_BYTES`, or more than what
    is left of the export's `MAX_PDF_EXPORT_RESOURCE_BYTES`, raises
    `PDFResourceTooLargeError`. Whatever the outcome, accepted, refused or
    failed, the export's budget is charged what reading the body cost
    (`_FetchCost`). Once nothing is left, `_refuse_if_budget_spent` refuses
    each further fetch before it starts, so none reaches this processor.
    Because the body is read here, these also fail the fetch itself rather
    than a later read: a body cut short (``IncompleteRead``, see
    `_read_body`), a corrupt gzip or deflate body, and a read timeout
    part-way through the body.

    The byte cap bounds reading and `Content-Encoding` decoding only, not
    the work WeasyPrint does on the body afterwards. So the read, charged
    body is then passed to `_refuse_unbounded_resource`, which refuses a
    compressed web font, a font with colour-bitmap or SVG glyph tables (or
    an unreadable table directory), a GIF, JPEG, PNG or WebP whose
    structure is past its pre-scan's caps or (GIF, JPEG) unreadable, a
    raster image that is not an
    allow-listed format
    or declares more than `MAX_PDF_IMAGE_PIXELS`, and an allow-listed image
    once the export's `MAX_PDF_EXPORT_IMAGE_PIXELS` decode budget is spent
    (`UnsupportedPDFResourceError`) -- the bodies WeasyPrint would otherwise
    hand to zlib/brotli or Pillow unbounded. This processor is the one seam
    every http(s) body passes through; data:, file: and ftp: URLs never
    reach it, as ``validate_url`` refuses them before the fetch.

    Only the response is handled here; the request, ``validate_url`` and
    the refusal of redirects are left as they are.
    """

    # After HTTPErrorProcessor, which raises on any non-2xx status, so only
    # a body WeasyPrint would go on to use is read.
    handler_order = urllib.request.HTTPErrorProcessor.handler_order + 1

    def http_response(self, request, response):
        budget = _export_resource_budget.get()
        limit = MAX_PDF_RESOURCE_BYTES
        budget_bound = False
        if budget is not None and budget.remaining < limit:
            limit = budget.remaining
            budget_bound = True
        cost = _FetchCost()
        try:
            body = _decoded_body(response, limit, cost)
        except PDFResourceTooLargeError:
            which = (
                f"this export's remaining {limit} of "
                f"{MAX_PDF_EXPORT_RESOURCE_BYTES} bytes"
                if budget_bound
                else f"the {limit}-byte per-resource limit"
            )
            logger.warning(
                f"Refused a resource over {which} in PDF rendering: "
                f"{redact_url_for_log(request.full_url)}"
            )
            raise
        finally:
            safe_close(response, "PDF resource response")
            if budget is not None:
                budget.charge(cost.total())
        # The body is read and charged; now refuse one WeasyPrint would
        # decode without a size bound (a compressed web font, a font with
        # image glyphs, an oversized or non-allow-listed raster image). Raised after the charge, so
        # even a refused resource spends the budget for what reading it
        # cost, and outside the try above so its own warning stands rather
        # than the over-cap one.
        _refuse_unbounded_resource(body, request.full_url)
        del response.headers["Content-Encoding"]
        return urllib.response.addinfourl(
            io.BytesIO(body), response.headers, response.url, response.status
        )

    https_response = http_response


class _SafeUrlFetcher:
    """WeasyPrint url_fetcher that blocks SSRF targets (GHSA-fj2m-qvh9-jq4q).

    What it does fetch goes through `_URL_FETCHER`, whose
    `_BoundedBodyProcessor` bounds the size of every body. Once the
    export's budget is spent it fetches nothing at all
    (`_refuse_if_budget_spent`).
    """

    # WeasyPrint 70's fetch() context manager (weasyprint/urls.py) reads
    # ``_fail_on_errors`` off the *url_fetcher itself* after catching an
    # exception it raised — a callable without that attribute turns every
    # blocked URL into an ``AttributeError`` mid-render. Carrying it as a
    # class attribute makes it part of this type's definition: every
    # instance gets it for free, with no separate import-time attachment
    # step (the bare-function equivalent, ``fn._fail_on_errors = ...``
    # after ``def fn(url): ...``) to forget on a future rewrite. That is
    # not the same as being un-droppable — a rebind through a lambda,
    # ``functools.partial``, or an un-attribute-aware wrapper would still
    # lose it, and ``functools.wraps`` -- which copies a *function's*
    # ``__dict__`` and so kept the old bare-function attribute -- does
    # not copy this class attribute. The actual guard is
    # ``test_fetchers_expose_the_fail_on_errors_contract`` in
    # ``tests/web/services/test_pdf_service.py``, which asserts
    # ``getattr(fetcher, "_fail_on_errors", None) is False`` on the wired
    # module-level ``_safe_url_fetcher`` and on a ``_SkippingUrlFetcher()``
    # instance as the retry builds one. ``False`` mirrors the
    # ``URLFetcher(fail_on_errors=False)`` default: the refusal surfaces
    # as ``URLFetchingError``, which WeasyPrint's callers warn-and-skip.
    # That keeps the 68.x ValueError-as-skip posture (pinned end-to-end
    # by ``test_render_succeeds_when_body_url_is_blocked``) while the
    # version floor is ~=70.0 for the GHSA-jf6q-chmf-3h3v
    # url_fetcher-bypass fix.
    _fail_on_errors = False

    def __call__(self, url):
        _refuse_if_budget_spent(url)
        if not validate_url(url):
            # A rejected URL is adversarial-shaped and may carry the
            # operator's real credentials (RFC 3986 userinfo) or secret
            # query tokens; log and raise only scheme://host:port, the
            # same discipline every ssrf_validator site follows.
            redacted = redact_url_for_log(url)
            logger.warning(f"Blocked unsafe URL in PDF rendering: {redacted}")
            raise UnsafePDFResourceURLError(
                f"Blocked unsafe URL in PDF rendering: {redacted}"
            )
        if _has_userinfo(url):
            # urllib passes the whole authority (``user@host``) to
            # getaddrinfo, while the DNS pin is keyed on the host alone, so
            # such a lookup would miss the pin. These fetches never need
            # credentials; refuse them.
            redacted = redact_url_for_log(url)
            logger.warning(
                f"Blocked URL with userinfo in PDF rendering: {redacted}"
            )
            raise UnsafePDFResourceURLError(
                f"Blocked URL with userinfo in PDF rendering: {redacted}"
            )
        _ensure_weasyprint()
        # validate_url resolved the host, but urllib would resolve it again
        # at connect time. Pin the validated addresses for this fetch so the
        # connection goes to an address that passed the same policy (see
        # security.dns_pinning). The block-private window checks any lookup
        # during the fetch that misses the pin against the same policy, on
        # the answer actually used. Both are thread-local, and WeasyPrint
        # fetches on the calling thread; urllib keeps the hostname for TLS
        # SNI and certificate checks. Without the shim neither applies, so
        # refuse rather than fetch unpinned.
        if not dns_pinning._shim_installed():
            redacted = redact_url_for_log(url)
            logger.warning(
                f"DNS pin unavailable; refusing PDF resource fetch: {redacted}"
            )
            raise UnsafePDFResourceURLError(
                f"DNS pin unavailable; refusing PDF resource fetch: {redacted}"
            )
        with (
            dns_pinning.block_private_resolution(),
            _allow_proxy_lookup(url),
            dns_pinning.pinned_request(url),
        ):
            return _URL_FETCHER.fetch(url)


def _allow_proxy_lookup(url):
    """Let urllib's lookup of the configured forward proxy pass the window.

    With ``HTTP(S)_PROXY`` set, urllib connects to the proxy, which resolves
    the target itself; the proxy is operator configuration and is often on
    a private or loopback address. Its lookup misses the pin, so the
    block-private window would refuse it. Relax exactly that (host, port)
    lookup to allow private and loopback addresses; metadata and the rest of
    link-local stay refused (see ``dns_pinning.allow_resolution``). Returns
    a no-op context when the URL will not go through a proxy.
    """
    endpoint = _proxy_endpoint(url)
    if endpoint is None:
        return contextlib.nullcontext()
    host, port = endpoint
    return dns_pinning.allow_resolution(
        host,
        port,
        allow_localhost=True,
        allow_private_ips=True,
        block_link_local=True,
    )


def _proxy_endpoint(url):
    """The (host, port) urllib will look up to reach the proxy for ``url``.

    Read from the fetcher's own ``ProxyHandler`` snapshot (what urllib will
    use), not a fresh ``getproxies()``. Mirrors ``ProxyHandler.proxy_open``
    (scheme lookup, ``proxy_bypass``, ``_parse_proxy``, ``unquote`` of the
    host:port) and ``http.client``'s host/port split, so the result is the
    exact host string and port passed to ``getaddrinfo``. None when no proxy
    applies or it cannot be worked out; the proxy lookup then gets the
    window's public-only policy, so a private proxy is refused (fail
    closed), never let through unchecked.

    Not mirrored, so it fails closed the same way: a plain-http URL whose
    ``http_proxy`` has an ``https://`` scheme while ``https_proxy`` is also
    set. urllib then re-enters its opener and tunnels to the first proxy
    through the ``https_proxy`` one, so the lookup is of the
    ``https_proxy`` host, which is not exempted.
    """
    parse_proxy = getattr(urllib.request, "_parse_proxy", None)
    if parse_proxy is None:
        # Private urllib helper; if a future Python drops it, fail closed
        # (no exemption) with a clear message rather than guessing.
        logger.warning(
            "urllib.request._parse_proxy is unavailable; a private forward "
            "proxy will be refused for PDF resource fetches"
        )
        return None
    proxies = None
    for handler in getattr(_URL_FETCHER, "handlers", ()):
        if isinstance(handler, urllib.request.ProxyHandler):
            proxies = handler.proxies
            break
    try:
        scheme = urlsplit(url).scheme.lower()
        proxy = (proxies or {}).get(scheme)
        if not proxy:
            return None
        # urllib's ``Request.host``: the authority (userinfo is refused
        # before this runs, and validate_url refuses percent-escapes).
        request_host = urlsplit(url).netloc
        if request_host and urllib.request.proxy_bypass(request_host):
            return None
        proxy_type, _user, _password, hostport = parse_proxy(proxy)
    except ValueError:
        return None
    hostport = unquote(hostport)  # as ProxyHandler.proxy_open does
    # The connection class urllib uses: an https URL tunnels through the
    # proxy over HTTPSConnection; otherwise the proxy URL's scheme decides.
    conn_scheme = "https" if scheme == "https" else (proxy_type or scheme)
    port = 443 if conn_scheme == "https" else 80
    host = hostport
    i, j = hostport.rfind(":"), hostport.rfind("]")
    if i > j:
        host = hostport[:i]
        if hostport[i + 1 :]:
            try:
                port = int(hostport[i + 1 :])
            except ValueError:
                return None
    if host[:1] == "[" and host[-1:] == "]":
        host = host[1:-1]
    return (host, port) if host else None


def _has_userinfo(url) -> bool:
    """True if the URL's authority carries userinfo (``@``, or ``%40``).

    The ``%40`` branch is defence in depth: an authority with ``%40`` but
    no ``@`` has the escape in its host or port, which ``validate_url``
    already refuses. An authority that cannot be parsed counts as carrying
    userinfo, so the caller refuses it.
    """
    try:
        netloc = urlsplit(url).netloc
    except ValueError:
        return True
    return "@" in netloc or "%40" in netloc.lower()


_safe_url_fetcher = _SafeUrlFetcher()


def _build_url_fetcher(fetcher_cls):
    """A new URL fetcher of WeasyPrint's ``fetcher_cls``, set up for exports.

    Redirects are disabled (see `_URL_FETCHER`) and a `_BoundedBodyProcessor`
    is attached. `_ensure_weasyprint` builds `_URL_FETCHER` with this, and
    the tests build their own fetchers with it too, so a handler added here
    reaches both.
    """
    fetcher = fetcher_cls(allow_redirects=False)
    fetcher.add_handler(_BoundedBodyProcessor())
    return fetcher


def _empty_resource_for(url):
    """The empty-resource response a skipped fetch hands WeasyPrint.

    Built through ``URLFetcherResponse`` when WeasyPrint 68+ provides it.
    WeasyPrint 69+ accepts nothing else (``weasyprint.urls.fetch`` asserts
    ``isinstance(resource, URLFetcherResponse)``), so if the full
    construction is refused -- an upstream signature change -- the fallback
    is the bare ``URLFetcherResponse(url)`` (empty body), not a dict. The
    dict is reached only when ``URLFetcherResponse`` does not exist, i.e.
    WeasyPrint < 68, which still reads that shape.

    The full construction (with a body and headers) is guarded so a
    *skipped* resource becomes a render failure only if
    ``URLFetcherResponse`` itself cannot be constructed at all -- the bare,
    no-body fallback below raises too. That bare call is deliberately left
    unguarded rather than caught with a dict as a last resort: on
    WeasyPrint 69+, ``weasyprint.urls.fetch()`` asserts
    ``isinstance(resource, URLFetcherResponse)`` *outside* the
    try/except that turns a raising ``url_fetcher`` into a skippable
    ``URLFetchingError``, so a dict would fail that assertion uncaught --
    strictly worse than letting the original exception propagate, since
    some WeasyPrint call sites (e.g. ``@import``) catch ``URLFetchingError``
    but none catch a bare ``AssertionError``. Letting the bare call's own
    exception propagate keeps it inside that try/except instead, so this
    already-vanishingly-rare double failure is handled exactly like any
    other resource failure (#6588): skipped wherever the caller already
    tolerates one.
    """
    if URLFetcherResponse is not None:
        try:
            return URLFetcherResponse(
                url,
                body=b"",
                headers={"Content-Type": _EMPTY_RESOURCE_MIME_TYPE},
            )
        except Exception as exc:
            # Type name only: the message of an arbitrary exception is not
            # vetted and could echo the URL (with its userinfo) back.
            logger.warning(
                f"URLFetcherResponse construction failed "
                f"({type(exc).__name__}); retrying with an empty body only"
            )
        return URLFetcherResponse(url)
    return {
        "string": b"",
        "mime_type": _EMPTY_RESOURCE_MIME_TYPE,
        "redirected_url": url,
    }


class _SkippingUrlFetcher:
    """As `_safe_url_fetcher`, but any failure yields an empty resource.

    The fetcher the retry in `_render_pdf` renders with: it builds a fresh
    instance per retry, so ``skipped`` lists exactly the URLs that render
    dropped. No fetch happens for a URL the guard refuses -- every call goes
    through `_safe_url_fetcher`, so `validate_url` still runs first and
    still decides, and the SSRF posture is byte for byte the normal path's.
    The difference is only what WeasyPrint is told afterwards: nothing,
    rather than an exception it handles inconsistently.

    ``__call__`` never raises for a resource failure, unless the WeasyPrint
    response class itself cannot be constructed for the empty-resource
    fallback (see `_empty_resource_for`) -- an upstream signature change,
    not a resource failure. The retry exists to skip whatever the first
    pass could not survive, and the failure set of
    a remote fetch is open-ended: besides the refusal
    (`UnsafePDFResourceURLError`) and ``OSError``/``ValueError``, a hostile
    server can make ``http.client`` raise ``HTTPException`` subclasses
    (``BadStatusLine``, ``LineTooLong``, too many headers, ``InvalidURL``)
    and a bad ``deflate`` body raises ``zlib.error``. An exception
    escaping here would abort the retried render -- on WeasyPrint 70 its
    ``fetch()`` would then also read ``_fail_on_errors`` off this object --
    and turn a completed export into a 500 (#6588). A programming error is
    not hidden: its exception *type* is logged at warning level, which is
    what distinguishes an SSRF refusal from an unreachable resource from a
    bug. The exception *message* is never logged -- ``InvalidURL`` and
    friends embed the raw URL, userinfo included.

    Scope: only failures raised *inside* the fetch are skipped, and the
    body is read inside it: `_BoundedBodyProcessor` reads and decodes every
    http(s) body before the fetch returns. So all of these fail the fetch
    itself: a body cut short (``IncompleteRead``: shorter than its
    ``Content-Length``, or a chunked body that ends early), a corrupt gzip
    or deflate stream, a read timeout part-way through the body, and a body
    over the size limits (`PDFResourceTooLargeError`, also raised before a
    fetch starts once the export's budget is spent).
    """

    # WeasyPrint 70's fetch() context manager reads ``_fail_on_errors`` off
    # the url_fetcher itself when it catches an exception from it. This
    # fetcher never raises, so that branch is dead today; the attribute is
    # kept so a future edit that makes ``__call__`` raise inherits the safe
    # warn-and-skip default instead of an ``AttributeError``, and so both
    # fetchers satisfy the same contract test
    # (``test_fetchers_expose_the_fail_on_errors_contract``). There is
    # deliberately no module-level instance of this class: ``skipped`` is
    # per-render state, and a shared instance would accumulate every
    # skipped URL across requests and users.
    _fail_on_errors = False

    def __init__(self):
        self.skipped: list[str] = []

    def __call__(self, url):
        # Settle the lazy import first, so the empty resource below is built
        # as the installed WeasyPrint expects even when the guard refuses
        # the URL before `_safe_url_fetcher` would have imported it.
        _ensure_weasyprint()
        try:
            return _safe_url_fetcher(url)
        except Exception as exc:
            self.skipped.append(url)
            # Same redaction discipline as the fetcher's own block path: a
            # refused/unavailable URL is adversarial-shaped and may carry
            # credentials in userinfo or secret query tokens. Exception type
            # only (see the class docstring); never logger.exception here,
            # whose traceback would carry both the message and the raw URL.
            logger.warning(
                f"Skipping unavailable resource in PDF rendering "
                f"({type(exc).__name__}): {redact_url_for_log(url)}"
            )
        # Built outside the except block so that, if even the bare
        # URLFetcherResponse construction were refused, the new exception
        # does not chain to the fetch error, whose message can carry the raw
        # URL into a later traceback. `_render_pdf` likewise runs the retry
        # outside its own except, so nothing chains to the first pass either.
        return _empty_resource_for(url)


class MissingPDFDependencyError(RuntimeError):
    """Raised when WeasyPrint system libraries are unavailable.

    Distinct from generic RuntimeError so the web layer can surface this
    message to users without also exposing unrelated RuntimeErrors
    (e.g., pandoc subprocess stderr from ODT export).
    """


def get_weasyprint_install_instructions() -> str:
    """Return platform-specific install instructions for WeasyPrint system deps."""
    system = platform.system()
    if system == "Darwin":
        return (
            "PDF export requires WeasyPrint system libraries (Pango, Cairo, GLib).\n"
            "Install with: brew install weasyprint\n"
            f"See: {_WEASYPRINT_DOCS_URL}#macos"
        )
    if system == "Linux":
        return (
            "PDF export requires WeasyPrint system libraries (Pango, Cairo, GLib).\n"
            f"See: {_WEASYPRINT_DOCS_URL}#linux"
        )
    if system == "Windows":
        return (
            "PDF export requires Pango system libraries.\n"
            f"See: {_WEASYPRINT_DOCS_URL}#windows"
        )
    return (
        "PDF export requires WeasyPrint system libraries (Pango, Cairo, GLib).\n"
        f"See: {_WEASYPRINT_DOCS_URL}"
    )


# Default stylesheet for PDF export. Exposed as a module-level constant so
# tests can assert against the source string (WeasyPrint's CSS object does
# not retain its input).
#
# CJK families are listed as fallbacks so WeasyPrint substitutes a
# glyph-bearing font when the primary stack lacks coverage. Without
# this, Chinese/Japanese/Korean text disappears silently from the
# PDF even though it renders fine in the HTML view (issue #4055).
# Glyphs still require the corresponding system font (e.g.
# fonts-noto-cjk) to actually be installed.
#
# Emoji are deliberately NOT listed in these stacks. They render
# through Pango/fontconfig per-character fallback to whichever emoji
# font is installed (fonts-noto-color-emoji is bundled in the official
# Docker image; docs/faq.md covers other platforms). Listing an emoji
# family explicitly (as #4730 did) makes Pango route every digit 0-9
# plus '#' and '*' — codepoints that carry the Unicode Emoji property
# and have glyphs in Noto Color Emoji — to the emoji font, even though
# earlier families in the stack cover them. The result: all numbers in
# exported reports rendered as wide, square emoji glyphs ("2 0 2 6"
# instead of "2026"). Regression test:
# test_minimal_css_excludes_emoji_font_families.
MINIMAL_CSS = """
@page {
    size: A4;
    margin: 1.5cm;
}

body {
    font-family: Arial, "Noto Sans CJK SC", "Noto Sans CJK TC",
        "Noto Sans CJK JP", "Noto Sans CJK KR", "Noto Sans SC",
        "PingFang SC", "PingFang TC", "Hiragino Sans",
        "Hiragino Kaku Gothic ProN", "Apple SD Gothic Neo",
        "Microsoft YaHei", "Microsoft JhengHei",
        "Yu Gothic", "Malgun Gothic", "SimSun", sans-serif;
    font-size: 10pt;
    line-height: 1.4;
}

table {
    border-collapse: collapse;
    width: 100%;
    margin: 0.5em 0;
}

th, td {
    border: 1px solid #ccc;
    padding: 6px;
    text-align: left;
}

th {
    background-color: #f0f0f0;
}

h1 { font-size: 16pt; margin: 0.5em 0; }
h2 { font-size: 14pt; margin: 0.5em 0; }
h3 { font-size: 12pt; margin: 0.5em 0; }
h4 { font-size: 11pt; margin: 0.5em 0; font-weight: bold; }
h5 { font-size: 10pt; margin: 0.5em 0; font-weight: bold; }
h6 { font-size: 10pt; margin: 0.5em 0; }

code, pre {
    font-family: monospace, "Noto Sans Mono CJK SC",
        "Noto Sans Mono CJK TC", "Noto Sans Mono CJK JP",
        "Noto Sans Mono CJK KR", "Noto Sans CJK SC",
        "PingFang SC", "Hiragino Sans", "Apple SD Gothic Neo",
        "Microsoft YaHei", "SimSun";
    background-color: #f5f5f5;
}

code {
    padding: 1px 3px;
}

pre {
    padding: 8px;
    overflow-x: auto;
}

a {
    color: #0066cc;
    text-decoration: none;
}
"""


class _TableAfterText(Preprocessor):
    """Put a blank line before a table that directly follows a line of text.

    Python-Markdown's ``tables`` extension only finds a table at the start of
    a block, so a table written right under a paragraph reached the PDF as
    pipe text, while the report view (marked, GFM) shows it as a table
    (#8096). A table start is whatever the extension itself accepts as one.
    This runs after ``fenced_code`` has stashed code blocks, so pipes inside
    them are left alone. A line indented by a tab stop or more is never a
    table start: it belongs to an indented code block, or continues the
    paragraph above it.
    """

    def run(self, lines):
        table = self.md.parser.blockprocessors["table"]
        indent = " " * self.md.tab_length
        out = []
        in_table = False
        for i, line in enumerate(lines):
            if not line.strip():
                in_table = False
            elif (
                not in_table
                and not line.startswith(indent)
                and i + 1 < len(lines)
                and table.test(None, f"{line}\n{lines[i + 1]}")
            ):
                if out and out[-1].strip():
                    out.append("")
                in_table = True
            out.append(line)
        return out


class PDFService:
    """Service for converting markdown to PDF using WeasyPrint."""

    def __init__(self):
        """Initialize PDF service with minimal CSS for readability."""
        # Defer-load WeasyPrint (lazy import) before using CSS, then
        # build the stylesheet from the module-level MINIMAL_CSS
        # constant. MINIMAL_CSS has no @import/@color-profile/url()
        # references to fetch, so url_fetcher is passed only for
        # consistency with every other CSS() construction in this
        # module -- not because anything here is reachable.
        _ensure_weasyprint()
        self.minimal_css = CSS(
            string=MINIMAL_CSS, url_fetcher=_safe_url_fetcher
        )

    def markdown_to_pdf(
        self,
        markdown_content: str,
        title: Optional[str] = None,
        metadata: Optional[Dict[str, Any]] = None,
        custom_css: Optional[str] = None,
    ) -> bytes:
        """
        Convert markdown content to PDF.

        Args:
            markdown_content: The markdown text to convert
            title: Optional title for the document
            metadata: Optional metadata dict (author, date, etc.)
            custom_css: Optional CSS string layered on top of the default
                stylesheet. Rules here win on equal specificity via the
                cascade, but the default's CJK font fallbacks and
                page setup are always applied.

        Returns:
            PDF file as bytes

        Note:
            WeasyPrint memory usage can spike with large documents.
            Production deployments should implement:
            - Memory limits (ulimit)
            - Timeouts (30-60 seconds)
            - Worker recycling after 100 requests
        """
        _ensure_weasyprint()
        # One budget for everything this export fetches (see
        # MAX_PDF_EXPORT_RESOURCE_BYTES), reset when it ends.
        budget_token = _export_resource_budget.set(
            _ResourceBudget(MAX_PDF_EXPORT_RESOURCE_BYTES)
        )
        try:
            # Convert markdown to HTML
            html_content = self._markdown_to_html(
                markdown_content, title, metadata
            )

            # url_fetcher blocks SSRF targets reachable via body/citation URLs.
            html_doc = HTML(string=html_content, url_fetcher=_safe_url_fetcher)

            # Always apply the default stylesheet first, then layer any
            # caller-provided custom_css on top. WeasyPrint resolves
            # conflicts by cascade order, so later stylesheets win on
            # equal specificity — this preserves the CJK font fallbacks
            # in MINIMAL_CSS even when a caller supplies their own CSS,
            # while still letting them override any default.
            css_list = [self.minimal_css]
            if custom_css:
                # With no font_config passed to CSS() at this call
                # site, @import and @color-profile src are the
                # references WeasyPrint fetches while parsing a
                # stylesheet -- @font-face src would join them if a
                # FontConfiguration were ever supplied here. Parsing
                # this stylesheet must go through the same SSRF-guarded
                # fetcher as the HTML document. Property-level url()
                # values (e.g. background-image) are not parsed here --
                # WeasyPrint resolves those later, at render time,
                # through html_doc's fetcher above, which is already
                # _safe_url_fetcher regardless of which stylesheet
                # declared them. A refused parse-time fetch (e.g. an
                # @color-profile src WeasyPrint resolves outside the
                # try/except that guards @import) surfaces here as
                # URLFetchingError; a relative @import WeasyPrint
                # cannot resolve without a base_url (since
                # CSS(string=...) has none) raises ValueError directly.
                # Either way: drop the custom stylesheet and keep
                # rendering with the default one rather than aborting
                # the whole export.
                try:
                    css_list.append(
                        CSS(string=custom_css, url_fetcher=_safe_url_fetcher)
                    )
                except (URLFetchingError, ValueError) as exc:
                    # URLFetchingError wraps the fetcher's exception
                    # without `from`, so __cause__ is always None; the
                    # original exception (e.g. the
                    # UnsafePDFResourceURLError a refusal raises) is on
                    # __context__ instead. A plain ValueError (a
                    # stylesheet WeasyPrint could not parse) carries
                    # neither -- unless markdown_to_pdf() is itself
                    # called from inside a caller's `except` block, in
                    # which case Python's implicit chaining sets
                    # __context__ to that in-flight exception rather
                    # than the parse failure. The logged type is
                    # therefore a best-effort diagnostic, not a
                    # guarantee.
                    cause = type(
                        exc.__cause__ or exc.__context__ or exc
                    ).__name__
                    logger.warning(
                        "Custom stylesheet rejected ({}); rendering without it",
                        cause,
                    )

            # Generate PDF
            pdf_bytes = self._render_pdf(html_content, html_doc, css_list)

            logger.info(f"Generated PDF, size: {len(pdf_bytes)} bytes")
            return pdf_bytes

        except Exception:
            logger.exception("Error generating PDF")
            raise
        finally:
            _export_resource_budget.reset(budget_token)

    def _render_pdf(self, html_content, html_doc, css_list) -> bytes:
        """Render, and retry once with refusals skipped rather than raised.

        WeasyPrint does not treat a refused fetch the same way everywhere. An
        `@import` it cannot fetch is skipped and the render completes; an
        `@color-profile` it cannot fetch is not, because the `fetch()` call in
        `weasyprint/css/__init__.py` sits outside the `try`, so the
        `URLFetchingError` escapes `CSS.__init__`, escapes this method, and the
        export route answers a generic 500 (#6466).

        Report text does not reach the renderer as markup:
        `_markdown_to_html` escapes raw HTML, so a `<style>` element cannot
        come from report content, and the export route passes
        `custom_css=None` (a caller-supplied stylesheet is parsed in
        `markdown_to_pdf`, which drops it on the same failure). The retry is
        kept as defence in depth, for any stylesheet that does reach the
        document handed to this method.

        The retry restores the behaviour the other at-rules already have,
        rather than renaming the failure. Nothing about the SSRF guard moves:
        the retry's `_SkippingUrlFetcher` still calls `validate_url` first and
        still performs no fetch for a URL it refuses. Only the normal path
        raises, so an export that works today is rendered exactly once and
        unchanged.
        """
        # The except below names the lazily-populated URLFetchingError
        # global; ensure the import has run before any except clause
        # evaluates it, or a render error surfaces as TypeError (catching
        # None) instead of the real failure (#6588).
        _ensure_weasyprint()

        pdf_buffer = io.BytesIO()
        needs_retry = False
        try:
            html_doc.write_pdf(pdf_buffer, stylesheets=css_list)
        except URLFetchingError:
            # Only record the failure here. The retry runs *after* this
            # block: inside it, any exception the retried render raised
            # would chain (``__context__``) to this first-pass error, whose
            # WeasyPrint-built message embeds the fetch exception's text --
            # which can still carry URL material such as secret query
            # tokens -- and ``logger.exception`` in the callers would print
            # that chain. Userinfo URLs are refused by the guard before
            # urllib parses them (``_has_userinfo``), so an
            # ``InvalidURL: nonnumeric port: '<password>@host'`` can no
            # longer arise for a URL the guard allows.
            needs_retry = True
        if needs_retry:
            logger.warning(
                "PDF render aborted on an unavailable resource; retrying with "
                "unavailable resources skipped"
            )
            pdf_buffer = io.BytesIO()
            # A fresh fetcher per retry: its `skipped` list is exactly what
            # this render dropped, surfaced below so a subtly incomplete
            # export is diagnosable instead of silent (#6588).
            skipping_fetcher = _SkippingUrlFetcher()
            HTML(string=html_content, url_fetcher=skipping_fetcher).write_pdf(
                pdf_buffer, stylesheets=css_list
            )
            skipped = skipping_fetcher.skipped
            if skipped:
                # Capped: a report with thousands of refused resources would
                # otherwise put all of them on one line (each one already
                # has its own per-resource warning).
                shown = [
                    redact_url_for_log(u)
                    for u in skipped[:_SKIPPED_SUMMARY_LIMIT]
                ]
                more = len(skipped) - len(shown)
                suffix = f" (+{more} more)" if more else ""
                logger.warning(
                    f"PDF render completed with {len(skipped)} skipped "
                    f"resource(s): {shown}{suffix}"
                )
        pdf_bytes = pdf_buffer.getvalue()
        pdf_buffer.close()
        return pdf_bytes

    def _markdown_to_html(
        self,
        markdown_content: str,
        title: Optional[str] = None,
        metadata: Optional[Dict[str, Any]] = None,
    ) -> str:
        """
        Convert markdown to HTML with proper structure.

        Uses Python-Markdown with extensions for:
        - Tables
        - Fenced code blocks
        - Table of contents
        - Footnotes

        Raw HTML in the report text is escaped rather than passed through, so
        it reaches the PDF as visible text, as in the on-screen report view.
        """
        # Parse markdown with extensions
        md = markdown.Markdown(
            extensions=[
                "tables",
                "fenced_code",
                "footnotes",
                "toc",
                "nl2br",  # Convert newlines to <br>
                "sane_lists",
                "meta",
            ]
        )

        # Escape raw HTML in the report body, matching the on-screen report
        # view (``ui.renderMarkdown``, #7365). Report text is LLM- and
        # web-derived (the synthesized answer, and source titles the Sources
        # block interpolates as plain text), and Python-Markdown passes raw
        # HTML straight through by default. Deregistering its raw-HTML
        # handlers makes any tags in the body come out as escaped text
        # instead, so report content cannot add stylesheets, attachments,
        # metadata or other markup to the exported PDF. Ordinary markdown --
        # including the ``![](url)`` images and ``[](url)`` links Markdown
        # itself generates -- is unaffected, and so is the pipeline's own
        # HTML (the ``<title>``, ``<meta>`` and footer assembled after
        # conversion below).
        #
        # ``strict=True`` (the default): if a future Markdown release renames
        # these handlers the export fails loudly here, rather than silently
        # reopening raw-HTML passthrough. Both names are stable across
        # Markdown 3.x.
        md.preprocessors.deregister("html_block")
        md.inlinePatterns.deregister("html")
        # Priority 20: after fenced_code (25), see _TableAfterText.
        md.preprocessors.register(_TableAfterText(md), "table_after_text", 20)

        html_body = md.convert(markdown_content)

        # Build complete HTML document
        html_parts = ["<!DOCTYPE html><html><head>"]
        html_parts.append('<meta charset="utf-8">')

        if title:
            html_parts.append(f"<title>{escape(title)}</title>")

        if metadata:
            for key, value in metadata.items():
                html_parts.append(
                    f'<meta name="{escape(str(key))}" content="{escape(str(value))}">'
                )

        html_parts.append("</head><body>")

        # Add the markdown content directly without any extra title or metadata
        html_parts.append(html_body)

        # Add footer with LDR attribution
        html_parts.append("""
            <div style="margin-top: 2em; padding-top: 1em; border-top: 1px solid #ddd; font-size: 9pt; color: #666; text-align: center;">
                Generated by <a href="https://github.com/LearningCircuit/local-deep-research" style="color: #0066cc;">LDR - Local Deep Research</a> | Open Source AI Research Assistant
            </div>
        """)

        html_parts.append("</body></html>")

        return "".join(html_parts)


# Singleton instance
_pdf_service = None


def get_pdf_service() -> PDFService:
    """Get or create the PDF service singleton.

    Raises:
        MissingPDFDependencyError: If WeasyPrint system libraries are not
            available, with platform-specific installation instructions.
    """
    _ensure_weasyprint()
    if not WEASYPRINT_AVAILABLE:
        raise MissingPDFDependencyError(get_weasyprint_install_instructions())
    global _pdf_service
    if _pdf_service is None:
        _pdf_service = PDFService()
    return _pdf_service