1

I am trying to create a feature that creates a PDF and image with data given by the user.

For the PDF I successfully converted my template **.html.erb to PDF using the Wicked PDF gem. However, I cannot find a similar gem or way to create an image out of my ERB file after a few hours of searching.

This is how I converted my template to PDF using Wicked PDF.

def linesheet_pdf
  # variables used to create the pdf
  @items = params[:items]
  @contact = params[:contact]

  # send pdf file back to front end
  response_to do |format|
    format.pdf do 
      render pdf: "filename.pdf",
      template: "path/to/html/erb/file/XYZ.html.erb",
      type: 'application/pdf', page_size: 'Letter'
    end
  end
end

By doing this I was able to use my HTML file with the styling from my custom CSS file.

I initially thought about converting the PDF to an image as a workaround but I found the image file converted from the PDF with multiple pages will not be a single image with long scroll.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ryan Byon
  • 11
  • 1

1 Answers1

0

I think what you are looking for is IMGKIT.

Once you are set up you could use something like:

def show
    respond_to do |format|
      format .html
      format.png do
        kit = IMGKit.new render_to_string, width: 1080, height: 1080
        send_data kit.to_png, type: "image/png", disposition: "inline"
      end
    end
  end

then link like this:

<%= link_to your_path(@yourmodel, format: "png", s: 'linkeding') do %>
<% end %>
double-beep
  • 5,031
  • 17
  • 33
  • 41
Joe Bloggos
  • 889
  • 7
  • 24