使用PDFKit创建PDF取得了一些成功,但是图像没有从active_storage和资源资源中加载。使用show.pdf.erb创建PDF时,每次都会失败。我使用的是默认的image_tag,资产已预编译用于生产。如何获取PDFKit以查找图像的预编译资产文件夹,并找到应该在show.pdf.erb视图中加载的active_storage图像?在正常的show.html.erb操作中,它工作得很好。我正在使用Windows 10 Pro x64。所有样式都嵌入在页面本身的样式html标签/块中。
服务器开发日志
Rendered books/show.pdf.erb (338.2ms)
Loading pages (1/6)
Warning: Failed to load file:///rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBHQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--4528619d26f56f1272afd5cd7641c666137e55e3/book-wallpaper.jpg (ignore)
Warning: Failed to load file:///assets/book_assets/book_white_pendant_50x50-8bf2d07feef7eeec22156e327c618aa02b531c2b7df4eb42308121ce55749b12.png (ignore)
Warning: Failed to load file:///rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBHUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--bb21bd1f8363f10691f76d305829078e20c66c13/cookies.jpg (ignore)
Warning: Failed to load file:///assets/book_assets/user_photo_white40x40-2b2a6064512b3fc938bd24dc94f813bc96a259c9c68e53e6fc101cb7db758aa0.png (ignore)
Warning: Failed to load file:///assets/book_assets/published-icon-888d2e09f6bd152718c2b03ee0ad753e766a2fc767fea6f932f539185a1a29ad.png (ignore)
Warning: Failed to load file:///assets/book_assets/unpublished_icon_white_40x40-e05d35d6e8818e7a06aad4c86c0a6f213d7d1c1461905ce4f612e8562a6995eb.png (ignore)
Warning: Failed to load file:///assets/book_assets/book_search_icon_50x50-184ff833a61c48ebd7fc5e728c0de77aaee09720ebae47ab86535850eb03c350.png (ignore)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
Rendering text template
Rendered text template (0.0ms)
Sent data Jamie-book-pjxr6Fu4sJC1.pdf (1.0ms)
Completed 200 OK in 7351ms (Views: 0.6ms | ActiveRecord: 466.2ms)
图像标签
<%= image_tag "book_assets/user_photo_white40x40" %>
active_storage
<%= image_tag url_for(@book.profile_photo), class: 'd-flex align-self-start mr-3 img-fluid' %>
books_controller.rb
def show
impressionist(@book)
respond_to do |format|
format.html
format.pdf do
html = render_to_string(:action => "show.pdf.erb")
kit = PDFKit.new(html)
send_data(kit.to_pdf, :filename => "book-info.pdf", :type => 'application/pdf', :disposition => "inline")
end
end
end
initializers / pdfkit.rb
PDFKit.configure do |config|
if ["development"].include?(Rails.env)
config.wkhtmltopdf = ENV['WKHTMLTOPDF_PATH'] || "C:\\wkhtml_setup\\wkhtmltopdf\\bin\\wkhtmltopdf.exe"
else
#if your site is hosted on heroku or any other hosting server which is 64bit
config.wkhtmltopdf = Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s
end
config.verbose = true
config.default_options = {
:encoding=>"UTF-8",
:page_size=>"A4", #or "Letter" or whatever needed
:margin_top=>"0in",
:margin_right=>"0in",
:margin_bottom=>"0in",
:margin_left=>"0in",
:disable_smart_shrinking => false
}
end
答案 0 :(得分:0)
解决此问题的最快方法是使用初始化方法。
initializers / pdfkit.rb
PDFKit.configure do |config|
if ["development"].include?(Rails.env)
config.wkhtmltopdf = ENV['WKHTMLTOPDF_PATH'] || "C:\\wkhtml_setup\\wkhtmltopdf\\bin\\wkhtmltopdf.exe"
end
config.root_url = "#{Rails.root.join('public')}/"
config.verbose = true
config.default_options = {
:print_media_type => true,
dpi: 400
}
end