Skip to content

Commit

Permalink
Change optimization task to not remove optional /Type entries contain…
Browse files Browse the repository at this point in the history
…ing default values

An optional /Type field is currently removed like any other optional
field if it contains its default value.

Since this can lead to problems this change makes the optimization task
leave the /Type entries alone. For example, some Factur-X validators
need /Type /EmbeddedFile entries for embedded files because if this
entry is missing they declare the PDF non-conforming.

It is possible to work around this by not optimizing the file or
optimizing it and then adding the necessary keys back. But keeping the
/Type entry also has other benefits like automatic type wrapping in the
general case when such a file is loaded by HexaPDF again.
  • Loading branch information
gettalong committed Aug 25, 2024
1 parent 41203ac commit c869274
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
a standard default appearance string for a variable text field if none is
found

### Changed

* [HexaPDF::Task::Optimize] to not remove optional /Type entries containing
default values

### Fixed

* [HexaPDF::Layout::TableBox] to correctly calculcate and distribute row
Expand Down
8 changes: 4 additions & 4 deletions lib/hexapdf/task/optimize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ def self.process_xref_streams(doc, method)
end
end

# Deletes field entries of the object that are optional and currently set to their default
# value.
# Deletes field entries (except for /Type) of the object that are optional and currently set
# to their default value.
def self.delete_fields_with_defaults(obj)
return unless obj.kind_of?(HexaPDF::Dictionary) && !obj.null?
obj.each do |name, value|
if (field = obj.class.field(name)) && !field.required? && field.default? &&
value == field.default
if name != :Type && (field = obj.class.field(name)) && !field.required? &&
field.default? && value == field.default
obj.delete(name)
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/hexapdf/task/test_optimize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class TestType < HexaPDF::Dictionary

define_type :Test
define_field :Type, type: Symbol, default: type
define_field :Optional, type: Symbol, default: :Optional

end
Expand Down Expand Up @@ -46,6 +47,7 @@ def assert_no_xrefstms
end

def assert_default_deleted
assert(@doc.object(1).key?(:Type))
refute(@doc.object(1).key?(:Optional))
end

Expand Down

0 comments on commit c869274

Please sign in to comment.