Question about handling tests for PDF flattening #3643
-
|
Hello! For learning purposes I'm building an utilities API in which I have an automatic PDF filling feature with form data and I'm implementing unit and integration testing. I have the following unit test: def test_fill_pdf_flattening():
with open("tests/assets/Sample-Fillable-PDF.pdf", "rb") as file:
pdf_file_bytes = io.BytesIO(fill_pdf(file, input_field_values, flatten=True))
pdf_file_fields = PdfReader(pdf_file_bytes).get_fields()
assert pdf_file_fields is None, "The filled PDF should not have fields."Where I'm testing this def fill_pdf(pdf_file: BinaryIO, data: dict[str, str], flatten: bool = True) -> bytes:
pdf = PdfWriter(pdf_file)
if pdf.get_fields() is None:
raise exceptions.PdfHasNoFieldsError("Input PDF has no fillable fields")
pdf.update_page_form_field_values(
page=None, fields=data, auto_regenerate=False, flatten=flatten)
if flatten:
# Required for full conversion of form field contents to regular PDF content
# More info: https://pypdf.readthedocs.io/en/stable/user/forms.html#filling-out-forms
pdf.remove_annotations(subtypes="/Widget")
with BytesIO() as bytes_stream:
pdf.write(bytes_stream)
return bytes_stream.getvalue()However, I'm getting an error with that test case. My own logic tells me that if I'm flattening a PDF file, then the resulting file won't have fillable forms, but I have a feeling that I am missing something fundamental and that I should do this differently. I would like to know essentially how the flattening works with this library to know if I'm doing the correct approach for the testing and what should I really expect. This is the error: If needed, I'm attaching the PDF I'm using for these tests: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Please note that my experience with PDF forms is rather limited, thus going into detail might be harder for me. In the parameter docs for
If you follow the code flow, you will see that pypdf attempts to add an appearance stream to the field accordingly. At first sight, your test looks correct - the failure seems to be a bug/robustness issue from our side (PRs welcome). Depending on your use case, you might want to additionally check the actual rendered output with different applications against an expected output to ensure everything works correctly. |
Beta Was this translation helpful? Give feedback.
Please note that my experience with PDF forms is rather limited, thus going into detail might be harder for me.
In the parameter docs for
flatten, we can read:If you follow the code flow, you will see that pypdf attempts to add an appearance stream to the field accordingly.
At first sight, your test looks correct - the failure seems to be a bug/robustness issue from our side (PRs welcome). Depending on your use case, you might want to additionally check the actual rendered output with different applications ag…