gettalong / hexapdf

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

Merging page from another pdf but font not mergeable #214

Closed alexey closed 1 year ago

alexey commented 1 year ago

I have app that adding texts for original pdf and generate new pdf. All is good until i have page that contain different font, then target pdf have no glyphs(boxes instead of characters), when source_doc saved, it displays font properly.

I have tried to .add font to target_doc but it did not added(tried before and after import) In the target_doc.fonts I can see font loaded in loaded_fonts_cache and in glyphs

Perhaps something to do with how import work but i did not found way :/

Will appreciate if you can point me in right direction ?

Here is part of code:

target_doc = HexaPDF::Document.new
source_doc = HexaPDF::Document.open("source_to_be_filled.pdf")
page = source_doc.pages[0]
canvas = page.canvas(type: :overlay)
# ... some code filling the doc

font_file = "font.ttf"
source_doc.fonts.add(font_file)
canvas.font font_file
canvas.text(text, at: [x, y])
# back to default font
canvas.font(FONT_FAMILY, size: FONT_SIZE)

source_doc.pages.each { |page| target_doc.pages << target_doc.import(page) }

target_doc.write(output_file)

https://hexapdf.gettalong.org/examples/merging.html

gettalong commented 1 year ago
alexey commented 1 year ago

Thanks @gettalong, this did the trick:

source_doc.dispatch_message(:complete_objects)