45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
"""Export pipeline: full-save + a pypdf pass that sets /NeedAppearances.
|
|
|
|
NOTE: ``pypdf`` is intentionally imported lazily inside the function. It is not a
|
|
declared gateway dependency and may only be resolvable via the roaming
|
|
site-packages path appended below, so importing it at module load would prevent
|
|
the whole app from starting on machines without it. Behavior here is preserved
|
|
verbatim from the original ``routers.documents.export_document``; if pypdf is
|
|
later added to ``pyproject.toml`` dependencies, the sys.path augmentation can be
|
|
removed and the import hoisted to module scope.
|
|
"""
|
|
|
|
|
|
def apply_need_appearances(bytes_data: bytes) -> bytes:
|
|
"""Round-trip the PDF through pypdf to set AcroForm /NeedAppearances=true."""
|
|
import os
|
|
import sys
|
|
|
|
roaming_path = os.path.join(
|
|
os.environ.get("APPDATA", "C:\\Users\\azeem\\AppData\\Roaming"),
|
|
"Python",
|
|
"Python312",
|
|
"site-packages",
|
|
)
|
|
if roaming_path not in sys.path:
|
|
sys.path.append(roaming_path)
|
|
|
|
import io
|
|
|
|
import pypdf
|
|
|
|
reader = pypdf.PdfReader(io.BytesIO(bytes_data))
|
|
writer = pypdf.PdfWriter()
|
|
writer.append(reader)
|
|
|
|
acro_form = writer.root_object.get("/AcroForm")
|
|
if acro_form is not None:
|
|
acro_form_dict = acro_form.get_object()
|
|
acro_form_dict[pypdf.generic.NameObject("/NeedAppearances")] = pypdf.generic.BooleanObject(
|
|
True
|
|
)
|
|
|
|
out_stream = io.BytesIO()
|
|
writer.write(out_stream)
|
|
return out_stream.getvalue()
|