@farazirfan/costar-server-executor 1.7.37 → 1.7.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/agent.d.ts +90 -0
- package/dist/agent/agent.d.ts.map +1 -1
- package/dist/agent/agent.js +606 -0
- package/dist/agent/agent.js.map +1 -1
- package/dist/agent/pi-embedded-runner/run.d.ts.map +1 -1
- package/dist/agent/pi-embedded-runner/run.js +2 -1
- package/dist/agent/pi-embedded-runner/run.js.map +1 -1
- package/dist/agent/pi-embedded-runner/system-prompt.d.ts.map +1 -1
- package/dist/agent/pi-embedded-runner/system-prompt.js +16 -37
- package/dist/agent/pi-embedded-runner/system-prompt.js.map +1 -1
- package/dist/agent/pi-embedded-runner/tools.d.ts +4 -1
- package/dist/agent/pi-embedded-runner/tools.d.ts.map +1 -1
- package/dist/agent/pi-embedded-runner/tools.js +3 -1
- package/dist/agent/pi-embedded-runner/tools.js.map +1 -1
- package/dist/agent/pi-embedded-runner/types.d.ts +4 -0
- package/dist/agent/pi-embedded-runner/types.d.ts.map +1 -1
- package/dist/cli/env-loader.d.ts.map +1 -1
- package/dist/cli/env-loader.js +1 -0
- package/dist/cli/env-loader.js.map +1 -1
- package/dist/cli/setup.js +2 -2
- package/dist/cli/setup.js.map +1 -1
- package/dist/cron/normalize.d.ts +31 -0
- package/dist/cron/normalize.d.ts.map +1 -0
- package/dist/cron/normalize.js +211 -0
- package/dist/cron/normalize.js.map +1 -0
- package/dist/cron/scheduler.d.ts +33 -3
- package/dist/cron/scheduler.d.ts.map +1 -1
- package/dist/cron/scheduler.js +253 -48
- package/dist/cron/scheduler.js.map +1 -1
- package/dist/heartbeat/runner.d.ts +27 -12
- package/dist/heartbeat/runner.d.ts.map +1 -1
- package/dist/heartbeat/runner.js +82 -104
- package/dist/heartbeat/runner.js.map +1 -1
- package/dist/infra/heartbeat-events-filter.d.ts +29 -0
- package/dist/infra/heartbeat-events-filter.d.ts.map +1 -0
- package/dist/infra/heartbeat-events-filter.js +80 -0
- package/dist/infra/heartbeat-events-filter.js.map +1 -0
- package/dist/infra/index.d.ts +9 -0
- package/dist/infra/index.d.ts.map +1 -0
- package/dist/infra/index.js +9 -0
- package/dist/infra/index.js.map +1 -0
- package/dist/infra/system-events.d.ts +58 -2
- package/dist/infra/system-events.d.ts.map +1 -1
- package/dist/infra/system-events.js +80 -14
- package/dist/infra/system-events.js.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +6 -1
- package/dist/server.js.map +1 -1
- package/dist/services/platform-keys.d.ts +19 -0
- package/dist/services/platform-keys.d.ts.map +1 -0
- package/dist/services/platform-keys.js +74 -0
- package/dist/services/platform-keys.js.map +1 -0
- package/dist/subagent/registry.d.ts +96 -0
- package/dist/subagent/registry.d.ts.map +1 -0
- package/dist/subagent/registry.js +180 -0
- package/dist/subagent/registry.js.map +1 -0
- package/dist/tools/complete-turn.d.ts +2 -2
- package/dist/tools/complete-turn.js +10 -10
- package/dist/tools/complete-turn.js.map +1 -1
- package/dist/tools/contacts.d.ts +13 -0
- package/dist/tools/contacts.d.ts.map +1 -0
- package/dist/tools/contacts.js +80 -0
- package/dist/tools/contacts.js.map +1 -0
- package/dist/tools/cron.d.ts +17 -2
- package/dist/tools/cron.d.ts.map +1 -1
- package/dist/tools/cron.js +117 -35
- package/dist/tools/cron.js.map +1 -1
- package/dist/tools/google-maps.d.ts +6 -6
- package/dist/tools/google-maps.d.ts.map +1 -1
- package/dist/tools/google-maps.js +207 -262
- package/dist/tools/google-maps.js.map +1 -1
- package/dist/tools/index.d.ts +17 -7
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +40 -9
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/phone-call.d.ts +11 -0
- package/dist/tools/phone-call.d.ts.map +1 -0
- package/dist/tools/phone-call.js +151 -0
- package/dist/tools/phone-call.js.map +1 -0
- package/dist/tools/sessions-spawn.d.ts +33 -0
- package/dist/tools/sessions-spawn.d.ts.map +1 -0
- package/dist/tools/sessions-spawn.js +164 -0
- package/dist/tools/sessions-spawn.js.map +1 -0
- package/dist/tools/spotify.d.ts +12 -0
- package/dist/tools/spotify.d.ts.map +1 -0
- package/dist/tools/spotify.js +251 -0
- package/dist/tools/spotify.js.map +1 -0
- package/dist/tools/subagents.d.ts +23 -0
- package/dist/tools/subagents.d.ts.map +1 -0
- package/dist/tools/subagents.js +209 -0
- package/dist/tools/subagents.js.map +1 -0
- package/dist/tools/whatsapp.d.ts +13 -0
- package/dist/tools/whatsapp.d.ts.map +1 -0
- package/dist/tools/whatsapp.js +215 -0
- package/dist/tools/whatsapp.js.map +1 -0
- package/dist/tools/youtube.d.ts +12 -0
- package/dist/tools/youtube.d.ts.map +1 -0
- package/dist/tools/youtube.js +218 -0
- package/dist/tools/youtube.js.map +1 -0
- package/dist/utils/asterizk-auth.d.ts +43 -0
- package/dist/utils/asterizk-auth.d.ts.map +1 -0
- package/dist/utils/asterizk-auth.js +125 -0
- package/dist/utils/asterizk-auth.js.map +1 -0
- package/dist/web-server.d.ts.map +1 -1
- package/dist/web-server.js +132 -0
- package/dist/web-server.js.map +1 -1
- package/dist/workspace/index.d.ts +3 -4
- package/dist/workspace/index.d.ts.map +1 -1
- package/dist/workspace/index.js +3 -4
- package/dist/workspace/index.js.map +1 -1
- package/dist/workspace/templates.d.ts +8 -7
- package/dist/workspace/templates.d.ts.map +1 -1
- package/dist/workspace/templates.js +18 -127
- package/dist/workspace/templates.js.map +1 -1
- package/dist/workspace/workspace.d.ts +2 -4
- package/dist/workspace/workspace.d.ts.map +1 -1
- package/dist/workspace/workspace.js +7 -16
- package/dist/workspace/workspace.js.map +1 -1
- package/package.json +1 -1
- package/public/index.html +231 -0
- package/skills/docx/SKILL.md +468 -0
- package/skills/docx/scripts/__init__.py +1 -0
- package/skills/docx/scripts/accept_changes.py +181 -0
- package/skills/docx/scripts/comment.py +347 -0
- package/skills/docx/scripts/helpers/__init__.py +0 -0
- package/skills/docx/scripts/helpers/merge_runs.py +231 -0
- package/skills/docx/scripts/helpers/simplify_redlines.py +240 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
- package/skills/docx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
- package/skills/docx/scripts/ooxml/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
- package/skills/docx/scripts/ooxml/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
- package/skills/docx/scripts/ooxml/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
- package/skills/docx/scripts/ooxml/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
- package/skills/docx/scripts/ooxml/schemas/mce/mc.xsd +75 -0
- package/skills/docx/scripts/ooxml/schemas/microsoft/wml-2010.xsd +560 -0
- package/skills/docx/scripts/ooxml/schemas/microsoft/wml-2012.xsd +67 -0
- package/skills/docx/scripts/ooxml/schemas/microsoft/wml-2018.xsd +14 -0
- package/skills/docx/scripts/ooxml/schemas/microsoft/wml-cex-2018.xsd +20 -0
- package/skills/docx/scripts/ooxml/schemas/microsoft/wml-cid-2016.xsd +13 -0
- package/skills/docx/scripts/ooxml/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
- package/skills/docx/scripts/ooxml/schemas/microsoft/wml-symex-2015.xsd +8 -0
- package/skills/docx/scripts/ooxml/scripts/pack.py +159 -0
- package/skills/docx/scripts/ooxml/scripts/unpack.py +29 -0
- package/skills/docx/scripts/ooxml/scripts/validate.py +106 -0
- package/skills/docx/scripts/ooxml/scripts/validation/__init__.py +15 -0
- package/skills/docx/scripts/ooxml/scripts/validation/base.py +1023 -0
- package/skills/docx/scripts/ooxml/scripts/validation/docx.py +519 -0
- package/skills/docx/scripts/ooxml/scripts/validation/pptx.py +315 -0
- package/skills/docx/scripts/ooxml/scripts/validation/redlining.py +284 -0
- package/skills/docx/scripts/pack.py +166 -0
- package/skills/docx/scripts/templates/comments.xml +3 -0
- package/skills/docx/scripts/templates/commentsExtended.xml +3 -0
- package/skills/docx/scripts/templates/commentsExtensible.xml +3 -0
- package/skills/docx/scripts/templates/commentsIds.xml +3 -0
- package/skills/docx/scripts/templates/people.xml +3 -0
- package/skills/docx/scripts/unpack.py +134 -0
- package/skills/longform-video-generation/SKILL.md +298 -0
- package/skills/longform-video-generation/references/advanced_techniques.md +474 -0
- package/skills/longform-video-generation/references/google_api_guide.md +288 -0
- package/skills/longform-video-generation/scripts/video_generator.py +579 -0
- package/skills/pdf/FORMS.md +305 -0
- package/skills/pdf/REFERENCE.md +612 -0
- package/skills/pdf/SKILL.md +293 -0
- package/skills/pdf/scripts/check_bounding_boxes.py +70 -0
- package/skills/pdf/scripts/check_fillable_fields.py +12 -0
- package/skills/pdf/scripts/convert_pdf_to_images.py +35 -0
- package/skills/pdf/scripts/create_validation_image.py +41 -0
- package/skills/pdf/scripts/extract_form_field_info.py +152 -0
- package/skills/pdf/scripts/extract_form_structure.py +124 -0
- package/skills/pdf/scripts/fill_fillable_fields.py +116 -0
- package/skills/pdf/scripts/fill_pdf_form_with_annotations.py +136 -0
- package/skills/pptx/SKILL.md +171 -0
- package/skills/pptx/editing.md +205 -0
- package/skills/pptx/pptxgenjs.md +377 -0
- package/skills/pptx/scripts/add_slide.py +225 -0
- package/skills/pptx/scripts/clean.py +309 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
- package/skills/pptx/scripts/ooxml/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
- package/skills/pptx/scripts/ooxml/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
- package/skills/pptx/scripts/ooxml/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
- package/skills/pptx/scripts/ooxml/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
- package/skills/pptx/scripts/ooxml/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
- package/skills/pptx/scripts/ooxml/schemas/mce/mc.xsd +75 -0
- package/skills/pptx/scripts/ooxml/schemas/microsoft/wml-2010.xsd +560 -0
- package/skills/pptx/scripts/ooxml/schemas/microsoft/wml-2012.xsd +67 -0
- package/skills/pptx/scripts/ooxml/schemas/microsoft/wml-2018.xsd +14 -0
- package/skills/pptx/scripts/ooxml/schemas/microsoft/wml-cex-2018.xsd +20 -0
- package/skills/pptx/scripts/ooxml/schemas/microsoft/wml-cid-2016.xsd +13 -0
- package/skills/pptx/scripts/ooxml/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
- package/skills/pptx/scripts/ooxml/schemas/microsoft/wml-symex-2015.xsd +8 -0
- package/skills/pptx/scripts/ooxml/scripts/pack.py +159 -0
- package/skills/pptx/scripts/ooxml/scripts/unpack.py +29 -0
- package/skills/pptx/scripts/ooxml/scripts/validate.py +106 -0
- package/skills/pptx/scripts/ooxml/scripts/validation/__init__.py +15 -0
- package/skills/pptx/scripts/ooxml/scripts/validation/base.py +1023 -0
- package/skills/pptx/scripts/ooxml/scripts/validation/docx.py +519 -0
- package/skills/pptx/scripts/ooxml/scripts/validation/pptx.py +315 -0
- package/skills/pptx/scripts/ooxml/scripts/validation/redlining.py +284 -0
- package/skills/pptx/scripts/pack.py +168 -0
- package/skills/pptx/scripts/thumbnail.py +318 -0
- package/skills/pptx/scripts/unpack.py +86 -0
- package/skills/xlsx/SKILL.md +291 -0
- package/skills/xlsx/recalc.py +247 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: pdf
|
|
3
|
+
description: Comprehensive PDF manipulation toolkit for extracting text and tables, creating new PDFs, merging/splitting documents, and handling forms. When you need to fill in a PDF form or programmatically process, generate, or analyze PDF documents at scale.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# PDF Processing Guide
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
This guide covers essential PDF processing operations using Python libraries and command-line tools. For advanced features, JavaScript libraries, and detailed examples, see REFERENCE.md. If you need to fill out a PDF form, read FORMS.md and follow its instructions.
|
|
11
|
+
|
|
12
|
+
## Quick Start
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
from pypdf import PdfReader, PdfWriter
|
|
16
|
+
|
|
17
|
+
# Read a PDF
|
|
18
|
+
reader = PdfReader("document.pdf")
|
|
19
|
+
print(f"Pages: {len(reader.pages)}")
|
|
20
|
+
|
|
21
|
+
# Extract text
|
|
22
|
+
text = ""
|
|
23
|
+
for page in reader.pages:
|
|
24
|
+
text += page.extract_text()
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Python Libraries
|
|
28
|
+
|
|
29
|
+
### pypdf - Basic Operations
|
|
30
|
+
|
|
31
|
+
#### Merge PDFs
|
|
32
|
+
```python
|
|
33
|
+
from pypdf import PdfWriter, PdfReader
|
|
34
|
+
|
|
35
|
+
writer = PdfWriter()
|
|
36
|
+
for pdf_file in ["doc1.pdf", "doc2.pdf", "doc3.pdf"]:
|
|
37
|
+
reader = PdfReader(pdf_file)
|
|
38
|
+
for page in reader.pages:
|
|
39
|
+
writer.add_page(page)
|
|
40
|
+
|
|
41
|
+
with open("merged.pdf", "wb") as output:
|
|
42
|
+
writer.write(output)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### Split PDF
|
|
46
|
+
```python
|
|
47
|
+
reader = PdfReader("input.pdf")
|
|
48
|
+
for i, page in enumerate(reader.pages):
|
|
49
|
+
writer = PdfWriter()
|
|
50
|
+
writer.add_page(page)
|
|
51
|
+
with open(f"page_{i+1}.pdf", "wb") as output:
|
|
52
|
+
writer.write(output)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### Extract Metadata
|
|
56
|
+
```python
|
|
57
|
+
reader = PdfReader("document.pdf")
|
|
58
|
+
meta = reader.metadata
|
|
59
|
+
print(f"Title: {meta.title}")
|
|
60
|
+
print(f"Author: {meta.author}")
|
|
61
|
+
print(f"Subject: {meta.subject}")
|
|
62
|
+
print(f"Creator: {meta.creator}")
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
#### Rotate Pages
|
|
66
|
+
```python
|
|
67
|
+
reader = PdfReader("input.pdf")
|
|
68
|
+
writer = PdfWriter()
|
|
69
|
+
|
|
70
|
+
page = reader.pages[0]
|
|
71
|
+
page.rotate(90) # Rotate 90 degrees clockwise
|
|
72
|
+
writer.add_page(page)
|
|
73
|
+
|
|
74
|
+
with open("rotated.pdf", "wb") as output:
|
|
75
|
+
writer.write(output)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### pdfplumber - Text and Table Extraction
|
|
79
|
+
|
|
80
|
+
#### Extract Text with Layout
|
|
81
|
+
```python
|
|
82
|
+
import pdfplumber
|
|
83
|
+
|
|
84
|
+
with pdfplumber.open("document.pdf") as pdf:
|
|
85
|
+
for page in pdf.pages:
|
|
86
|
+
text = page.extract_text()
|
|
87
|
+
print(text)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### Extract Tables
|
|
91
|
+
```python
|
|
92
|
+
with pdfplumber.open("document.pdf") as pdf:
|
|
93
|
+
for i, page in enumerate(pdf.pages):
|
|
94
|
+
tables = page.extract_tables()
|
|
95
|
+
for j, table in enumerate(tables):
|
|
96
|
+
print(f"Table {j+1} on page {i+1}:")
|
|
97
|
+
for row in table:
|
|
98
|
+
print(row)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Advanced Table Extraction
|
|
102
|
+
```python
|
|
103
|
+
import pandas as pd
|
|
104
|
+
|
|
105
|
+
with pdfplumber.open("document.pdf") as pdf:
|
|
106
|
+
all_tables = []
|
|
107
|
+
for page in pdf.pages:
|
|
108
|
+
tables = page.extract_tables()
|
|
109
|
+
for table in tables:
|
|
110
|
+
if table: # Check if table is not empty
|
|
111
|
+
df = pd.DataFrame(table[1:], columns=table[0])
|
|
112
|
+
all_tables.append(df)
|
|
113
|
+
|
|
114
|
+
# Combine all tables
|
|
115
|
+
if all_tables:
|
|
116
|
+
combined_df = pd.concat(all_tables, ignore_index=True)
|
|
117
|
+
combined_df.to_excel("extracted_tables.xlsx", index=False)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### reportlab - Create PDFs
|
|
121
|
+
|
|
122
|
+
#### Basic PDF Creation
|
|
123
|
+
```python
|
|
124
|
+
from reportlab.lib.pagesizes import letter
|
|
125
|
+
from reportlab.pdfgen import canvas
|
|
126
|
+
|
|
127
|
+
c = canvas.Canvas("hello.pdf", pagesize=letter)
|
|
128
|
+
width, height = letter
|
|
129
|
+
|
|
130
|
+
# Add text
|
|
131
|
+
c.drawString(100, height - 100, "Hello World!")
|
|
132
|
+
c.drawString(100, height - 120, "This is a PDF created with reportlab")
|
|
133
|
+
|
|
134
|
+
# Add a line
|
|
135
|
+
c.line(100, height - 140, 400, height - 140)
|
|
136
|
+
|
|
137
|
+
# Save
|
|
138
|
+
c.save()
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
#### Create PDF with Multiple Pages
|
|
142
|
+
```python
|
|
143
|
+
from reportlab.lib.pagesizes import letter
|
|
144
|
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
|
|
145
|
+
from reportlab.lib.styles import getSampleStyleSheet
|
|
146
|
+
|
|
147
|
+
doc = SimpleDocTemplate("report.pdf", pagesize=letter)
|
|
148
|
+
styles = getSampleStyleSheet()
|
|
149
|
+
story = []
|
|
150
|
+
|
|
151
|
+
# Add content
|
|
152
|
+
title = Paragraph("Report Title", styles['Title'])
|
|
153
|
+
story.append(title)
|
|
154
|
+
story.append(Spacer(1, 12))
|
|
155
|
+
|
|
156
|
+
body = Paragraph("This is the body of the report. " * 20, styles['Normal'])
|
|
157
|
+
story.append(body)
|
|
158
|
+
story.append(PageBreak())
|
|
159
|
+
|
|
160
|
+
# Page 2
|
|
161
|
+
story.append(Paragraph("Page 2", styles['Heading1']))
|
|
162
|
+
story.append(Paragraph("Content for page 2", styles['Normal']))
|
|
163
|
+
|
|
164
|
+
# Build PDF
|
|
165
|
+
doc.build(story)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Command-Line Tools
|
|
169
|
+
|
|
170
|
+
### pdftotext (poppler-utils)
|
|
171
|
+
```bash
|
|
172
|
+
# Extract text
|
|
173
|
+
pdftotext input.pdf output.txt
|
|
174
|
+
|
|
175
|
+
# Extract text preserving layout
|
|
176
|
+
pdftotext -layout input.pdf output.txt
|
|
177
|
+
|
|
178
|
+
# Extract specific pages
|
|
179
|
+
pdftotext -f 1 -l 5 input.pdf output.txt # Pages 1-5
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### qpdf
|
|
183
|
+
```bash
|
|
184
|
+
# Merge PDFs
|
|
185
|
+
qpdf --empty --pages file1.pdf file2.pdf -- merged.pdf
|
|
186
|
+
|
|
187
|
+
# Split pages
|
|
188
|
+
qpdf input.pdf --pages . 1-5 -- pages1-5.pdf
|
|
189
|
+
qpdf input.pdf --pages . 6-10 -- pages6-10.pdf
|
|
190
|
+
|
|
191
|
+
# Rotate pages
|
|
192
|
+
qpdf input.pdf output.pdf --rotate=+90:1 # Rotate page 1 by 90 degrees
|
|
193
|
+
|
|
194
|
+
# Remove password
|
|
195
|
+
qpdf --password=mypassword --decrypt encrypted.pdf decrypted.pdf
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### pdftk (if available)
|
|
199
|
+
```bash
|
|
200
|
+
# Merge
|
|
201
|
+
pdftk file1.pdf file2.pdf cat output merged.pdf
|
|
202
|
+
|
|
203
|
+
# Split
|
|
204
|
+
pdftk input.pdf burst
|
|
205
|
+
|
|
206
|
+
# Rotate
|
|
207
|
+
pdftk input.pdf rotate 1east output rotated.pdf
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Common Tasks
|
|
211
|
+
|
|
212
|
+
### Extract Text from Scanned PDFs
|
|
213
|
+
```python
|
|
214
|
+
# Uses pre-installed: pytesseract, pdf2image
|
|
215
|
+
import pytesseract
|
|
216
|
+
from pdf2image import convert_from_path
|
|
217
|
+
|
|
218
|
+
# Convert PDF to images
|
|
219
|
+
images = convert_from_path('scanned.pdf')
|
|
220
|
+
|
|
221
|
+
# OCR each page
|
|
222
|
+
text = ""
|
|
223
|
+
for i, image in enumerate(images):
|
|
224
|
+
text += f"Page {i+1}:\n"
|
|
225
|
+
text += pytesseract.image_to_string(image)
|
|
226
|
+
text += "\n\n"
|
|
227
|
+
|
|
228
|
+
print(text)
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Add Watermark
|
|
232
|
+
```python
|
|
233
|
+
from pypdf import PdfReader, PdfWriter
|
|
234
|
+
|
|
235
|
+
# Create watermark (or load existing)
|
|
236
|
+
watermark = PdfReader("watermark.pdf").pages[0]
|
|
237
|
+
|
|
238
|
+
# Apply to all pages
|
|
239
|
+
reader = PdfReader("document.pdf")
|
|
240
|
+
writer = PdfWriter()
|
|
241
|
+
|
|
242
|
+
for page in reader.pages:
|
|
243
|
+
page.merge_page(watermark)
|
|
244
|
+
writer.add_page(page)
|
|
245
|
+
|
|
246
|
+
with open("watermarked.pdf", "wb") as output:
|
|
247
|
+
writer.write(output)
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Extract Images
|
|
251
|
+
```bash
|
|
252
|
+
# Using pdfimages (poppler-utils)
|
|
253
|
+
pdfimages -j input.pdf output_prefix
|
|
254
|
+
|
|
255
|
+
# This extracts all images as output_prefix-000.jpg, output_prefix-001.jpg, etc.
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Password Protection
|
|
259
|
+
```python
|
|
260
|
+
from pypdf import PdfReader, PdfWriter
|
|
261
|
+
|
|
262
|
+
reader = PdfReader("input.pdf")
|
|
263
|
+
writer = PdfWriter()
|
|
264
|
+
|
|
265
|
+
for page in reader.pages:
|
|
266
|
+
writer.add_page(page)
|
|
267
|
+
|
|
268
|
+
# Add password
|
|
269
|
+
writer.encrypt("userpassword", "ownerpassword")
|
|
270
|
+
|
|
271
|
+
with open("encrypted.pdf", "wb") as output:
|
|
272
|
+
writer.write(output)
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Quick Reference
|
|
276
|
+
|
|
277
|
+
| Task | Best Tool | Command/Code |
|
|
278
|
+
|------|-----------|--------------|
|
|
279
|
+
| Merge PDFs | pypdf | `writer.add_page(page)` |
|
|
280
|
+
| Split PDFs | pypdf | One page per file |
|
|
281
|
+
| Extract text | pdfplumber | `page.extract_text()` |
|
|
282
|
+
| Extract tables | pdfplumber | `page.extract_tables()` |
|
|
283
|
+
| Create PDFs | reportlab | Canvas or Platypus |
|
|
284
|
+
| Command line merge | qpdf | `qpdf --empty --pages ...` |
|
|
285
|
+
| OCR scanned PDFs | pytesseract | Convert to image first |
|
|
286
|
+
| Fill PDF forms | pdf-lib or pypdf (see FORMS.md) | See FORMS.md |
|
|
287
|
+
|
|
288
|
+
## Next Steps
|
|
289
|
+
|
|
290
|
+
- For advanced pypdfium2 usage, see REFERENCE.md
|
|
291
|
+
- For JavaScript libraries (pdf-lib), see REFERENCE.md
|
|
292
|
+
- If you need to fill out a PDF form, follow the instructions in FORMS.md
|
|
293
|
+
- For troubleshooting guides, see REFERENCE.md
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
import json
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Script to check that the `fields.json` file that you create when analyzing PDFs
|
|
7
|
+
# does not have overlapping bounding boxes. See FORMS.md.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class RectAndField:
|
|
12
|
+
rect: list[float]
|
|
13
|
+
rect_type: str
|
|
14
|
+
field: dict
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# Returns a list of messages that are printed to stdout for you to read.
|
|
18
|
+
def get_bounding_box_messages(fields_json_stream) -> list[str]:
|
|
19
|
+
messages = []
|
|
20
|
+
fields = json.load(fields_json_stream)
|
|
21
|
+
messages.append(f"Read {len(fields['form_fields'])} fields")
|
|
22
|
+
|
|
23
|
+
def rects_intersect(r1, r2):
|
|
24
|
+
disjoint_horizontal = r1[0] >= r2[2] or r1[2] <= r2[0]
|
|
25
|
+
disjoint_vertical = r1[1] >= r2[3] or r1[3] <= r2[1]
|
|
26
|
+
return not (disjoint_horizontal or disjoint_vertical)
|
|
27
|
+
|
|
28
|
+
rects_and_fields = []
|
|
29
|
+
for f in fields["form_fields"]:
|
|
30
|
+
rects_and_fields.append(RectAndField(f["label_bounding_box"], "label", f))
|
|
31
|
+
rects_and_fields.append(RectAndField(f["entry_bounding_box"], "entry", f))
|
|
32
|
+
|
|
33
|
+
has_error = False
|
|
34
|
+
for i, ri in enumerate(rects_and_fields):
|
|
35
|
+
# This is O(N^2); we can optimize if it becomes a problem.
|
|
36
|
+
for j in range(i + 1, len(rects_and_fields)):
|
|
37
|
+
rj = rects_and_fields[j]
|
|
38
|
+
if ri.field["page_number"] == rj.field["page_number"] and rects_intersect(ri.rect, rj.rect):
|
|
39
|
+
has_error = True
|
|
40
|
+
if ri.field is rj.field:
|
|
41
|
+
messages.append(f"FAILURE: intersection between label and entry bounding boxes for `{ri.field['description']}` ({ri.rect}, {rj.rect})")
|
|
42
|
+
else:
|
|
43
|
+
messages.append(f"FAILURE: intersection between {ri.rect_type} bounding box for `{ri.field['description']}` ({ri.rect}) and {rj.rect_type} bounding box for `{rj.field['description']}` ({rj.rect})")
|
|
44
|
+
if len(messages) >= 20:
|
|
45
|
+
messages.append("Aborting further checks; fix bounding boxes and try again")
|
|
46
|
+
return messages
|
|
47
|
+
if ri.rect_type == "entry":
|
|
48
|
+
if "entry_text" in ri.field:
|
|
49
|
+
font_size = ri.field["entry_text"].get("font_size", 14)
|
|
50
|
+
entry_height = ri.rect[3] - ri.rect[1]
|
|
51
|
+
if entry_height < font_size:
|
|
52
|
+
has_error = True
|
|
53
|
+
messages.append(f"FAILURE: entry bounding box height ({entry_height}) for `{ri.field['description']}` is too short for the text content (font size: {font_size}). Increase the box height or decrease the font size.")
|
|
54
|
+
if len(messages) >= 20:
|
|
55
|
+
messages.append("Aborting further checks; fix bounding boxes and try again")
|
|
56
|
+
return messages
|
|
57
|
+
|
|
58
|
+
if not has_error:
|
|
59
|
+
messages.append("SUCCESS: All bounding boxes are valid")
|
|
60
|
+
return messages
|
|
61
|
+
|
|
62
|
+
if __name__ == "__main__":
|
|
63
|
+
if len(sys.argv) != 2:
|
|
64
|
+
print("Usage: check_bounding_boxes.py [fields.json]")
|
|
65
|
+
sys.exit(1)
|
|
66
|
+
# Input file should be in the `fields.json` format described in FORMS.md.
|
|
67
|
+
with open(sys.argv[1]) as f:
|
|
68
|
+
messages = get_bounding_box_messages(f)
|
|
69
|
+
for msg in messages:
|
|
70
|
+
print(msg)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from pypdf import PdfReader
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
# Script for you to run to determine whether a PDF has fillable form fields. See FORMS.md.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
reader = PdfReader(sys.argv[1])
|
|
9
|
+
if (reader.get_fields()):
|
|
10
|
+
print("This PDF has fillable form fields")
|
|
11
|
+
else:
|
|
12
|
+
print("This PDF does not have fillable form fields; you will need to visually determine where to enter data")
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
from pdf2image import convert_from_path
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# Converts each page of a PDF to a PNG image.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def convert(pdf_path, output_dir, max_dim=1000):
|
|
11
|
+
images = convert_from_path(pdf_path, dpi=200)
|
|
12
|
+
|
|
13
|
+
for i, image in enumerate(images):
|
|
14
|
+
# Scale image if needed to keep width/height under `max_dim`
|
|
15
|
+
width, height = image.size
|
|
16
|
+
if width > max_dim or height > max_dim:
|
|
17
|
+
scale_factor = min(max_dim / width, max_dim / height)
|
|
18
|
+
new_width = int(width * scale_factor)
|
|
19
|
+
new_height = int(height * scale_factor)
|
|
20
|
+
image = image.resize((new_width, new_height))
|
|
21
|
+
|
|
22
|
+
image_path = os.path.join(output_dir, f"page_{i+1}.png")
|
|
23
|
+
image.save(image_path)
|
|
24
|
+
print(f"Saved page {i+1} as {image_path} (size: {image.size})")
|
|
25
|
+
|
|
26
|
+
print(f"Converted {len(images)} pages to PNG images")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
if __name__ == "__main__":
|
|
30
|
+
if len(sys.argv) != 3:
|
|
31
|
+
print("Usage: convert_pdf_to_images.py [input pdf] [output directory]")
|
|
32
|
+
sys.exit(1)
|
|
33
|
+
pdf_path = sys.argv[1]
|
|
34
|
+
output_directory = sys.argv[2]
|
|
35
|
+
convert(pdf_path, output_directory)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
from PIL import Image, ImageDraw
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# Creates "validation" images with rectangles for the bounding box information that
|
|
8
|
+
# You create when determining where to add text annotations in PDFs. See FORMS.md.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def create_validation_image(page_number, fields_json_path, input_path, output_path):
|
|
12
|
+
# Input file should be in the `fields.json` format described in FORMS.md.
|
|
13
|
+
with open(fields_json_path, 'r') as f:
|
|
14
|
+
data = json.load(f)
|
|
15
|
+
|
|
16
|
+
img = Image.open(input_path)
|
|
17
|
+
draw = ImageDraw.Draw(img)
|
|
18
|
+
num_boxes = 0
|
|
19
|
+
|
|
20
|
+
for field in data["form_fields"]:
|
|
21
|
+
if field["page_number"] == page_number:
|
|
22
|
+
entry_box = field['entry_bounding_box']
|
|
23
|
+
label_box = field['label_bounding_box']
|
|
24
|
+
# Draw red rectangle over entry bounding box and blue rectangle over the label.
|
|
25
|
+
draw.rectangle(entry_box, outline='red', width=2)
|
|
26
|
+
draw.rectangle(label_box, outline='blue', width=2)
|
|
27
|
+
num_boxes += 2
|
|
28
|
+
|
|
29
|
+
img.save(output_path)
|
|
30
|
+
print(f"Created validation image at {output_path} with {num_boxes} bounding boxes")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
if __name__ == "__main__":
|
|
34
|
+
if len(sys.argv) != 5:
|
|
35
|
+
print("Usage: create_validation_image.py [page number] [fields.json file] [input image path] [output image path]")
|
|
36
|
+
sys.exit(1)
|
|
37
|
+
page_number = int(sys.argv[1])
|
|
38
|
+
fields_json_path = sys.argv[2]
|
|
39
|
+
input_image_path = sys.argv[3]
|
|
40
|
+
output_image_path = sys.argv[4]
|
|
41
|
+
create_validation_image(page_number, fields_json_path, input_image_path, output_image_path)
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
from pypdf import PdfReader
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# Extracts data for the fillable form fields in a PDF and outputs JSON that
|
|
8
|
+
# You use to fill the fields. See FORMS.md.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# This matches the format used by PdfReader `get_fields` and `update_page_form_field_values` methods.
|
|
12
|
+
def get_full_annotation_field_id(annotation):
|
|
13
|
+
components = []
|
|
14
|
+
while annotation:
|
|
15
|
+
field_name = annotation.get('/T')
|
|
16
|
+
if field_name:
|
|
17
|
+
components.append(field_name)
|
|
18
|
+
annotation = annotation.get('/Parent')
|
|
19
|
+
return ".".join(reversed(components)) if components else None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def make_field_dict(field, field_id):
|
|
23
|
+
field_dict = {"field_id": field_id}
|
|
24
|
+
ft = field.get('/FT')
|
|
25
|
+
if ft == "/Tx":
|
|
26
|
+
field_dict["type"] = "text"
|
|
27
|
+
elif ft == "/Btn":
|
|
28
|
+
field_dict["type"] = "checkbox" # radio groups handled separately
|
|
29
|
+
states = field.get("/_States_", [])
|
|
30
|
+
if len(states) == 2:
|
|
31
|
+
# "/Off" seems to always be the unchecked value, as suggested by
|
|
32
|
+
# https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=448
|
|
33
|
+
# It can be either first or second in the "/_States_" list.
|
|
34
|
+
if "/Off" in states:
|
|
35
|
+
field_dict["checked_value"] = states[0] if states[0] != "/Off" else states[1]
|
|
36
|
+
field_dict["unchecked_value"] = "/Off"
|
|
37
|
+
else:
|
|
38
|
+
print(f"Unexpected state values for checkbox `${field_id}`. Its checked and unchecked values may not be correct; if you're trying to check it, visually verify the results.")
|
|
39
|
+
field_dict["checked_value"] = states[0]
|
|
40
|
+
field_dict["unchecked_value"] = states[1]
|
|
41
|
+
elif ft == "/Ch":
|
|
42
|
+
field_dict["type"] = "choice"
|
|
43
|
+
states = field.get("/_States_", [])
|
|
44
|
+
field_dict["choice_options"] = [{
|
|
45
|
+
"value": state[0],
|
|
46
|
+
"text": state[1],
|
|
47
|
+
} for state in states]
|
|
48
|
+
else:
|
|
49
|
+
field_dict["type"] = f"unknown ({ft})"
|
|
50
|
+
return field_dict
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
# Returns a list of fillable PDF fields:
|
|
54
|
+
# [
|
|
55
|
+
# {
|
|
56
|
+
# "field_id": "name",
|
|
57
|
+
# "page": 1,
|
|
58
|
+
# "type": ("text", "checkbox", "radio_group", or "choice")
|
|
59
|
+
# // Per-type additional fields described in FORMS.md
|
|
60
|
+
# },
|
|
61
|
+
# ]
|
|
62
|
+
def get_field_info(reader: PdfReader):
|
|
63
|
+
fields = reader.get_fields()
|
|
64
|
+
|
|
65
|
+
field_info_by_id = {}
|
|
66
|
+
possible_radio_names = set()
|
|
67
|
+
|
|
68
|
+
for field_id, field in fields.items():
|
|
69
|
+
# Skip if this is a container field with children, except that it might be
|
|
70
|
+
# a parent group for radio button options.
|
|
71
|
+
if field.get("/Kids"):
|
|
72
|
+
if field.get("/FT") == "/Btn":
|
|
73
|
+
possible_radio_names.add(field_id)
|
|
74
|
+
continue
|
|
75
|
+
field_info_by_id[field_id] = make_field_dict(field, field_id)
|
|
76
|
+
|
|
77
|
+
# Bounding rects are stored in annotations in page objects.
|
|
78
|
+
|
|
79
|
+
# Radio button options have a separate annotation for each choice;
|
|
80
|
+
# all choices have the same field name.
|
|
81
|
+
# See https://westhealth.github.io/exploring-fillable-forms-with-pdfrw.html
|
|
82
|
+
radio_fields_by_id = {}
|
|
83
|
+
|
|
84
|
+
for page_index, page in enumerate(reader.pages):
|
|
85
|
+
annotations = page.get('/Annots', [])
|
|
86
|
+
for ann in annotations:
|
|
87
|
+
field_id = get_full_annotation_field_id(ann)
|
|
88
|
+
if field_id in field_info_by_id:
|
|
89
|
+
field_info_by_id[field_id]["page"] = page_index + 1
|
|
90
|
+
field_info_by_id[field_id]["rect"] = ann.get('/Rect')
|
|
91
|
+
elif field_id in possible_radio_names:
|
|
92
|
+
try:
|
|
93
|
+
# ann['/AP']['/N'] should have two items. One of them is '/Off',
|
|
94
|
+
# the other is the active value.
|
|
95
|
+
on_values = [v for v in ann["/AP"]["/N"] if v != "/Off"]
|
|
96
|
+
except KeyError:
|
|
97
|
+
continue
|
|
98
|
+
if len(on_values) == 1:
|
|
99
|
+
rect = ann.get("/Rect")
|
|
100
|
+
if field_id not in radio_fields_by_id:
|
|
101
|
+
radio_fields_by_id[field_id] = {
|
|
102
|
+
"field_id": field_id,
|
|
103
|
+
"type": "radio_group",
|
|
104
|
+
"page": page_index + 1,
|
|
105
|
+
"radio_options": [],
|
|
106
|
+
}
|
|
107
|
+
# Note: at least on macOS 15.7, Preview.app doesn't show selected
|
|
108
|
+
# radio buttons correctly. (It does if you remove the leading slash
|
|
109
|
+
# from the value, but that causes them not to appear correctly in
|
|
110
|
+
# Chrome/Firefox/Acrobat/etc).
|
|
111
|
+
radio_fields_by_id[field_id]["radio_options"].append({
|
|
112
|
+
"value": on_values[0],
|
|
113
|
+
"rect": rect,
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
# Some PDFs have form field definitions without corresponding annotations,
|
|
117
|
+
# so we can't tell where they are. Ignore these fields for now.
|
|
118
|
+
fields_with_location = []
|
|
119
|
+
for field_info in field_info_by_id.values():
|
|
120
|
+
if "page" in field_info:
|
|
121
|
+
fields_with_location.append(field_info)
|
|
122
|
+
else:
|
|
123
|
+
print(f"Unable to determine location for field id: {field_info.get('field_id')}, ignoring")
|
|
124
|
+
|
|
125
|
+
# Sort by page number, then Y position (flipped in PDF coordinate system), then X.
|
|
126
|
+
def sort_key(f):
|
|
127
|
+
if "radio_options" in f:
|
|
128
|
+
rect = f["radio_options"][0]["rect"] or [0, 0, 0, 0]
|
|
129
|
+
else:
|
|
130
|
+
rect = f.get("rect") or [0, 0, 0, 0]
|
|
131
|
+
adjusted_position = [-rect[1], rect[0]]
|
|
132
|
+
return [f.get("page"), adjusted_position]
|
|
133
|
+
|
|
134
|
+
sorted_fields = fields_with_location + list(radio_fields_by_id.values())
|
|
135
|
+
sorted_fields.sort(key=sort_key)
|
|
136
|
+
|
|
137
|
+
return sorted_fields
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def write_field_info(pdf_path: str, json_output_path: str):
|
|
141
|
+
reader = PdfReader(pdf_path)
|
|
142
|
+
field_info = get_field_info(reader)
|
|
143
|
+
with open(json_output_path, "w") as f:
|
|
144
|
+
json.dump(field_info, f, indent=2)
|
|
145
|
+
print(f"Wrote {len(field_info)} fields to {json_output_path}")
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
if __name__ == "__main__":
|
|
149
|
+
if len(sys.argv) != 3:
|
|
150
|
+
print("Usage: extract_form_field_info.py [input pdf] [output json]")
|
|
151
|
+
sys.exit(1)
|
|
152
|
+
write_field_info(sys.argv[1], sys.argv[2])
|