【问题标题】:Display images with WickedPDF on an RoR App在 RoR 应用程序上使用 WickedPDF 显示图像
【发布时间】:2021-07-26 05:11:41
【问题描述】:

我实际上是在我的应用程序上使用 WickedPDF 生成 PDF。现在我只想在我的 PDF 的标题中包含一个图像“logo.jpg”。

我尝试了很多东西,我认为我的问题来自资产。

我的控制器:

def generate_pdf
     pdf = WickedPdf.new.pdf_from_string(
         render_to_string(
           template: 'pdf/pdf_view.pdf.erb',
           layout: 'layouts/application.pdf.erb'))
     send_data(pdf,
     filename: 'file_name.pdf',
     type: 'application/pdf',
     disposition: 'attachment')
end

layouts/application.pdf.erb:

<!doctype html>
<html>
  <head>
    <meta charset='utf-8' />
  </head>
  <body onload='number_pages'>
    <div id="content">
      <%= yield %>
    </div>
  </body>
</html>

pdf/pdf_view.pdf.erb:

<div>
    <p>Just a simple text on my pdf that is correctly displayed</p>
  <%= wicked_pdf_image_tag "images/logo.jpg", :width=>"250", :height=>"200" %>
</div>

在我的控制台中:

Rendering pdf/pdf_view.pdf.erb within layouts/application.pdf.erb
  Rendered pdf/pdf_view.pdf.erb within layouts/application.pdf.erb (2.7ms)
[wicked_pdf]: ["/home/vincent/.rvm/gems/ruby-2.7.1/bin/wkhtmltopdf", "file:////tmp/wicked_pdf20210503-14279-o2q5ol.html", "/tmp/wicked_pdf_generated_file20210503-14279-1om4oam.pdf"]
source=rack-timeout id=cfb572c3-5919-47c1-b32c-cc3bf724b9bf timeout=15000ms service=1000ms state=active
  Rendering text template
  Rendered text template (0.1ms)
Sent data file_name.pdf (1.5ms)
Completed 200 OK in 1140ms (Views: 1.1ms | ActiveRecord: 1.1ms)

所以我的问题是:您还有其他方法可以在生成的 pdf 上显示图像吗?还是我的方法不对?

【问题讨论】:

  • 您使用的是哪个 Rails 版本。我有同样的问题,可能会有所帮助。

标签: html ruby-on-rails erb wicked-pdf generate


【解决方案1】:

问题已解决,在 pdf 上显示我的图像的正确命令是:

image_tag wicked_pdf_asset_base64

【讨论】:

    猜你喜欢
    • 2012-07-19
    • 2014-07-31
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多