在 RoR 应用程序上使用 WickedPDF 显示图像

问题描述

我实际上在我的应用程序上使用 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 上显示图像吗?还是我的方法不对?

解决方法

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

image_tag wicked_pdf_asset_base64