forked from reingart/pyfpdf
-
Notifications
You must be signed in to change notification settings - Fork 331
implement interactive PDF form fields (AcroForms) #1706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kaos599
wants to merge
7
commits into
py-pdf:master
Choose a base branch
from
Kaos599:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2d4b880
implement interactive PDF form fields (AcroForms)
Kaos599 1fa4963
docs: add documentation and changelog for interactive forms
Kaos599 3bdb7d6
cleanup AcroForms implementation and improve test suite
Kaos599 7c704ff
address review comments on AcroForms implementation
Kaos599 3645410
fix: improve AcroForm rendering compatibility in Adobe Acrobat
Kaos599 6a7cfac
fix: improve AcroForm rendering compatibility in Adobe Acrobat
Kaos599 0b3fac4
Merge branch 'master' of https://github.com/Kaos599/fpdf2
Kaos599 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # Interactive Forms (AcroForms) | ||
|
|
||
| `fpdf2` supports creating interactive PDF forms that users can fill out directly in their PDF viewer. This is implemented using the AcroForm standard. | ||
|
|
||
| Currently supported field types: | ||
| * **Text Fields**: Single-line, multi-line, and password inputs. | ||
| * **Checkboxes**: Toggleable buttons. | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| To add form fields, use the `text_field()` and `checkbox()` methods. | ||
|
|
||
| ```python | ||
| from fpdf import FPDF | ||
|
|
||
| pdf = FPDF() | ||
| pdf.add_page() | ||
| pdf.set_font("Helvetica", size=12) | ||
|
|
||
| # Add a label and a text field | ||
| pdf.text(10, 20, "First Name:") | ||
| pdf.text_field(name="first_name", x=40, y=15, w=50, h=10, value="John") | ||
|
|
||
| # Add a checkbox | ||
| pdf.checkbox(name="subscribe", x=10, y=30, size=5, checked=True) | ||
| pdf.text(17, 34, "Subscribe to newsletter") | ||
|
|
||
| pdf.output("form.pdf") | ||
| ``` | ||
|
|
||
| ## Text Fields | ||
|
|
||
| The `text_field()` method supports several customization options: | ||
|
|
||
| | Parameter | Description | | ||
| | --- | --- | | ||
| | `name` | Unique identifier for the field. | | ||
| | `value` | Initial text content. | | ||
| | `multiline` | If `True`, the field allows multiple lines of text. | | ||
| | `password` | If `True`, characters are masked (e.g., with bullets). | | ||
| | `max_length` | Maximum number of characters allowed. | | ||
| | `font_size` | Size of the text in the field. | | ||
| | `font_color_gray` | Gray level (0-1) for the text. | | ||
| | `background_color` | RGB tuple (0-1) for the field background. | | ||
| | `border_color` | RGB tuple (0-1) for the field border. | | ||
|
|
||
| ### Example: Multiline Text Area | ||
|
|
||
| ```python | ||
| pdf.text_field( | ||
| name="comments", | ||
| x=10, | ||
| y=50, | ||
| w=100, | ||
| h=30, | ||
| multiline=True, | ||
| value="Enter your comments here..." | ||
| ) | ||
| ``` | ||
|
|
||
| ## Checkboxes | ||
|
|
||
| The `checkbox()` method creates a toggleable button. | ||
|
|
||
| | Parameter | Description | | ||
| | --- | --- | | ||
| | `name` | Unique identifier for the checkbox. | | ||
| | `checked` | Initial state of the checkbox. | | ||
| | `size` | Width and height of the checkbox. | | ||
| | `check_color_gray` | Gray level (0-1) for the checkmark. | | ||
|
|
||
| ## Field Properties | ||
|
|
||
| Both field types support common properties: | ||
|
|
||
| * `read_only`: If `True`, the user cannot modify the field value. | ||
| * `required`: If `True`, the field must be filled before the form can be submitted. | ||
|
|
||
| ## Compatibility | ||
|
|
||
| `fpdf2` generates **Appearance Streams** for all form fields. This ensures that the fields are visible and rendered correctly across almost all PDF readers, including: | ||
| * Adobe Acrobat Reader | ||
| * Chrome / Firefox / Edge built-in viewers | ||
| * Sumatra PDF | ||
| * Mobile PDF viewers | ||
|
|
||
| Note: Form fields require PDF version 1.4 or higher. `fpdf2` will automatically set the document version to 1.4 if any form fields are added. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.