How to Add a Resized Image to an Existing PDF Using Ruby on Rails?

51 views Asked by At

I'm working on a Ruby on Rails project, and my goal is to add a resized image, specifically a signature, to an existing PDF. I want to emphasize that I'm looking to add a simple image, not a complex signature with advanced features.

Here is the code I've implemented:

def add_image_to_pdf
  # Get the original PDF and the image
  original_pdf_path = Rails.root.join("public", "uploads", "document", "pdf", "original.pdf").to_s
  image_path = Rails.root.join("public", "uploads", "images", "signature_image.jpeg").to_s

  # Output path for the new PDF
  output_pdf_path = Rails.root.join("public", "uploads", "document", "pdf", "document_with_image.pdf").to_s

  # Temporary path for the image converted to PDF
  temp_pdf_path = Rails.root.join("public", "uploads", "temp_image.pdf").to_s

  # Resize the image using MiniMagick and convert it to PDF
  image = MiniMagick::Image.open(image_path)
  image.resize "1000x1000"
  image.format "pdf"
  image.write temp_pdf_path

  # Load the original PDF and the resized image
  original_pdf = CombinePDF.load(original_pdf_path)
  image_page = CombinePDF.load(temp_pdf_path).pages[0]

  # Combine the original PDF with the resized image
  pdf = CombinePDF.new
  pdf << original_pdf
  pdf.pages.each { |page| page << image_page }
  pdf.save output_pdf_path

  # Delete the temporary file
  File.delete(temp_pdf_path) if File.exist?(temp_pdf_path)

  render json: { status: "Success", message: "Image added to PDF successfully", output_pdf_path: output_pdf_path }
end

Despite my efforts, the resized image is not being accurately positioned within the existing PDF. I'm seeking advice on how to ensure precise placement of the image at the specified position in the PDF.

Specific Questions:

  1. How can I correctly position the resized image within the existing PDF?
  2. Are there any additional adjustments needed in the code to achieve the desired result?

I appreciate any guidance or suggestions on enhancing the accuracy of adding the image to the PDF. Thank you!

1

There are 1 answers

0
gettalong On

[N.B. I'm the author of HexaPDF and for commercial use you would need to buy a license].

You would be better off using either HexaPDF or Prawn together with the prawn-templates gem.

From your code I think you want to add an existing image on top of each page of the source PDF. With HexaPDF the code would look something like this:

doc = HexaPDF::Document.open(original_pdf_path)
image = doc.images.add(image_path)
doc.pages.each do |page|
  page.canvas(type: :overlay).image(image, at: [0, 0])
end
doc.write(output_pdf_path, optimize: true)

As for your question regarding the positioning of the image, you would modify the at part with (0, 0) being the lower-left corner of each page. You can also resize the image by providing the width and/or height keyword arguments with the #image method.

See https://hexapdf.gettalong.org/documentation/api/HexaPDF/Content/Canvas.html#method-i-image for details on the #image method