gettalong / hexapdf

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

[Documentation, one example] How to deal with images? #217

Closed rubyFeedback closed 1 year ago

rubyFeedback commented 1 year ago

Hey there and servas gettalong,

Would it be possible to add a simple example to showcase how to embed an image?

The simplest use case I have would be like a foto or some other image, but adorning a header with an image would also be nice.

I had a look at https://hexapdf.gettalong.org/examples/index.html but I was unable to find an image example specifically (that is, to embed an image there). I grepped in examples/ for "Image" but could not find it; I only saw it on the rdoc documentation or so. Would be nice if there is an example too if you ever have some time on some upcoming weekend. Does not have to be too advanced either.

In prawn, looking at my local (outdated) knowledge base, I can do this:

pdf.image(path_to_dir, :width => 197, :height => 91)

Not sure whether that could work for hexapdf.

rubyFeedback commented 1 year ago

I found test code at test/hexapdf/type/test_image.rb but I am not sure which part of that could be used to store an image into a .pdf and then save the .pdf.

gettalong commented 1 year ago

The simplest case would be

require 'hexapdf'

doc = HexaPDF::Document.new
doc.pages.add.canvas.
  image(path_to_file, at: [100, 100], width: 197, height: 91)
doc.write('out.pdf')

This would add the given image at (100, 100) with the specified width and height (leave one of them out to retain the aspect ratio).

The graphics primitives example at https://hexapdf.gettalong.org/examples/graphics.html also shows image embedding.

What do you mean by "adorning a header with an image would also be nice." ?

Yeah, I can add an example similar to the arcs one.

gettalong commented 1 year ago

Done - will be visible with the new release.