Skip to content

Instantly share code, notes, and snippets.

@aashish
Last active February 21, 2024 17:18
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aashish/f472f62ce0202eae8a33c2531649f5d6 to your computer and use it in GitHub Desktop.
Save aashish/f472f62ce0202eae8a33c2531649f5d6 to your computer and use it in GitHub Desktop.
Insert image on a existing PDF having content with hexapdf gem
require 'hexapdf'
doc = HexaPDF::Document.open("/home/xxxx/Downloads/OoPdfFormExample.pdf")
page = doc.pages[0]
canvas = page.canvas(type: :overlay)
canvas.translate(0, 20) do
canvas.fill_color(0.3, 0.7, 0.7)
canvas.rectangle(50, 0, 80, 80, radius: 80)
canvas.fill
solid = canvas.graphic_object(:solid_arc, cx: 190, cy: 40, inner_a: 20, inner_b: 15,
outer_a: 40, outer_b: 30, start_angle: 10, end_angle: 130)
canvas.line_width(0.5)
canvas.opacity(fill_alpha: 0.5, stroke_alpha: 0.2) do
canvas.image(File.join(__dir__, 'cloud.png'), at: [350, 0], height: 80)
canvas.image(File.join(__dir__, 'freepik.jpg'), at: [50, 400], height: 80)
end
end
doc.write('/home/xxxx/Downloads/graphics.pdf', optimize: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment