问题
I'm having some success with creating PDFs with PDFKit, but the images are not loading from active_storage and assets resources. It fails every single time that I create the PDF, using show.pdf.erb. I'm using the default image_tag and assets are precompiled for production. How can I get PDFKit to find the precompiled assets folder for the images, and also find active_storage images that are supposed to load within the show.pdf.erb view? It works perfectly fine in the normal show.html.erb action. I'm using Windows 10 Pro x64. All styles are embedded in the style html tag/block on the page itself.
server development log
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 tags
<%= 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
回答1:
The quickest method to solving this problem is using the initializer approach.
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
来源:https://stackoverflow.com/questions/55329282/how-to-load-images-precompiled-assets-with-pdfkit-wkhtmltopdf-rails-5-2