【问题标题】:How to convert a view to an image如何将视图转换为图像
【发布时间】:2020-02-29 01:36:33
【问题描述】:

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

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

这就是我使用 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 转换的图像文件不会是长滚动的单个图像。

【问题讨论】:

标签: ruby-on-rails ruby image


【解决方案1】:

我想你要找的是IMGKIT

设置完成后,您可以使用以下内容:

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

然后像这样链接:

<%= link_to your_path(@yourmodel, format: "png", s: 'linkeding') do %>
<% end %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 2023-04-06
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多