You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
docuseal/vendor/bundle/ruby/4.0.0/gems/hexapdf-1.7.0/examples/023-images.rb

31 lines
1.0 KiB

# # Images
#
# This example shows how to embed images into a PDF document, directly on a
# page's canvas and through the high-level [HexaPDF::Composer].
#
# Usage:
# : `ruby images.rb`
#
require 'hexapdf'
file = File.join(__dir__, 'machupicchu.jpg')
doc = HexaPDF::Document.new
# Image only added to PDF once though used multiple times
canvas = doc.pages.add.canvas
canvas.image(file, at: [100, 500]) # auto-size based on image size
canvas.image(file, at: [100, 300], width: 100) # height based on w/h ratio
canvas.image(file, at: [300, 300], height: 100) # width based on w/h ratio
canvas.image(file, at: [100, 100], width: 300, height: 100)
HexaPDF::Composer.create('images.pdf') do |composer|
composer.image(file) # fill current rectangular region
composer.image(file, width: 100) # height based on w/h ratio
composer.image(file, height: 100) # width based on w/h ratio
composer.image(file, width: 300, height: 100)
# Add the page created above as second page
composer.document.pages << composer.document.import(doc.pages[0])
end