Skip to content

Instantly share code, notes, and snippets.

@aashish
Last active June 5, 2024 19:58
Show Gist options
  • Save aashish/f472f62ce0202eae8a33c2531649f5d6 to your computer and use it in GitHub Desktop.
Save aashish/f472f62ce0202eae8a33c2531649f5d6 to your computer and use it in GitHub Desktop.

Revisions

  1. aashish revised this gist Jun 5, 2018. No changes.
  2. aashish created this gist Jun 5, 2018.
    23 changes: 23 additions & 0 deletions insert_image_on_pdf.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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)