Skip to content

Commit

Permalink
Handle invalid text field appearance streams posing as other PDF objects
Browse files Browse the repository at this point in the history
There seem to be PDFs in the wild that have appearance streams with a
valid '/Subtype /Form' entry but invalid /Type entry. Such invalid
objects are not correctly recognized by HexaPDF and thus not assigned
the correct class.

This change amends the previous fix to handle this additional case.
  • Loading branch information
gettalong committed Sep 6, 2024
1 parent 390a69e commit abdbc4d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* [HexaPDF::Type::AcroForm::SignatureField#field_value] to always return a
correctly wrapped object
* [HexaPDF::Writer] to remove /Type entry from trailer
* [HexaPDF::Type::AcroForm::AppearanceGenerator#create_text_appearances] to
handle invalid appearance streams that are not correct Form XObjects


## 0.46.0 - 2024-08-11
Expand Down
7 changes: 5 additions & 2 deletions lib/hexapdf/type/acro_form/appearance_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,11 @@ def create_text_appearances

form = (@widget[:AP] ||= {})[:N] ||= @document.add({Type: :XObject, Subtype: :Form})
# Wrap existing object in Form class in case the PDF writer didn't include the /Subtype
# key; we can do this since we know this has to be a Form object
form = @document.wrap(form, type: :XObject, subtype: :Form) unless form[:Subtype] == :Form
# key or the type of the object is wrong; we can do this since we know this has to be a
# Form object
unless form.type == :XObject && form[:Subtype] == :Form
form = @document.wrap(form, type: :XObject, subtype: :Form)
end
form.value.replace({Type: :XObject, Subtype: :Form, BBox: [0, 0, width, height],
Matrix: matrix, Resources: HexaPDF::Object.deep_copy(default_resources)})
form.contents = ''
Expand Down
6 changes: 6 additions & 0 deletions test/hexapdf/type/acro_form/test_appearance_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ def check_rotation(angle, width, height, matrix)
assert_equal(form, @widget[:AP][:N])
refute(form.key?(:key))
assert_match(/test1/, form.contents)

form.delete(:Type)
@widget[:AP][:N] = @doc.wrap(form, type: HexaPDF::Type::Annotation)
@field[:V] = 'test2'
@generator.create_appearances
assert_match(/test2/, form.contents)
end

describe "takes the rotation into account" do
Expand Down

0 comments on commit abdbc4d

Please sign in to comment.