gettalong / hexapdf

Versatile PDF creation and manipulation for Ruby
https://hexapdf.gettalong.org
Other
1.22k stars 69 forks source link

How to flatten PDF fields that have javascript widgets associated? #172

Closed mdonagh closed 2 years ago

mdonagh commented 2 years ago

example.pdf See example PDF here - this has the fields populated, I cannot figure out how to get them to flatten.

I've been able to get the fields populated with the below:

 field = obj.document.acro_form.field_by_name(obj[:T])
 field.field_value = string_to_write

However, when I flatten the PDF fields with the below, they are all empty.

document.pages.each do |page|
  page.flatten_annotations
end

Also, when I flatten the fields this way rather than our legacy method, strings = PDF::Inspector::Text.analyze(pdf).strings cannot read the strings. Why is that?

Legacy method below:

        style = HexaPDF::Layout::Style.new(font: canvas.font, fill_color: color, stroke_color: color, align: :left, valign: :center)
        fragment = HexaPDF::Layout::TextFragment.create(str, style)
        layouter = HexaPDF::Layout::TextLayouter.new(style)
        layouter.fit([fragment], w, h).draw(canvas, x1, y2)

Your help would be greatly appreciated here @gettalong Thomas, thanks in advance

gettalong commented 2 years ago

I have looked at it but can't reproduce the problem (I ran into a bug while trying various things that will be fixed with the next release, though).

I tried the following script:

require 'hexapdf'

doc = HexaPDF::Document.open('/tmp/example.pdf')
doc.acro_form.flatten(create_appearances: false)
doc.write('/tmp/flattened.pdf')

The resulting file shows the form fields when inspecting visually: image

I also tried modifying a field inserting this line before flattening:

doc.acro_form.field_by_name("ADDRESS 1").field_value = "Some Address Here 1"

This also works: image

Could you provide a short example script where this problem appears? Thank you!

gettalong commented 2 years ago

@mdonagh I just tried this again to make sure it still doesn't work for me with the latest code and, alas, I still can't reproduce the problem. Could you provide a sample script?

gettalong commented 2 years ago

@mdonagh Since you haven't responded and I can't reproduce the problem, I will close the issue. If this is still a problem, please let me know and I will re-open the issue.