如何使用PDFKit/wkhtmltopdf加载图像/预编译资源? Rails 5.2 [英] How to load images/precompiled assets with PDFKit/wkhtmltopdf? Rails 5.2

查看:221
本文介绍了如何使用PDFKit/wkhtmltopdf加载图像/预编译资源? Rails 5.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用PDFKit创建PDF方面取得了一些成功,但是图像没有从active_storage和资源资源中加载.使用show.pdf.erb创建PDF时,每次都会失败.我使用的是默认的image_tag,并且资产已预编译用于生产.如何获取PDFKit以查找图像的预编译资产文件夹,并找到应该在show.pdf.erb视图中加载的active_storage图像?在正常的show.html.erb操作中,它工作得很好.我正在使用Windows 10 Pro x64.所有样式都嵌入在页面本身的样式html标签/块中.

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.

服务器开发日志

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

推荐答案

解决此问题的最快方法是使用初始化方法.

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

这篇关于如何使用PDFKit/wkhtmltopdf加载图像/预编译资源? Rails 5.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆