Skip to content

Instantly share code, notes, and snippets.

@tarunc
Forked from aashish/insert_image_on_pdf.rb
Created August 1, 2021 05:16
Show Gist options
  • Save tarunc/6cc11d800527b8e4034019607284df6c to your computer and use it in GitHub Desktop.
Save tarunc/6cc11d800527b8e4034019607284df6c 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