如何将视图转换为图像

时间:2020-02-29 01:36:33

标签: ruby-on-rails ruby image

我正在尝试创建一个功能,该功能使用用户提供的数据创建PDF和图像。

对于PDF,我使用Wicked PDF gem成功地将模板**.html.erb转换为PDF。但是,经过几个小时的搜索,我找不到从我的ERB文件中创建图像的类似gem或方法。

这就是我使用Wicked PDF将模板转换为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

这样做,我可以将HTML文件与自定义CSS文件中的样式一起使用。

最初,我考虑过将PDF转换为图像的一种解决方法,但是我发现从PDF转换为多页的图像文件将不是长滚动的单个图像。

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找的是IMGKIT

设置完成后,您可以使用类似的内容:

myFunction()

def myFunction():
    print "Hello World!"

然后像这样的链接:

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
相关问题