pdfkit 不从 cdn + rails 4.2 + pdfkit 渲染图像

pdfkit not rendering images from cdn + rails 4.2 + pdfkit

我正在尝试使用 pdfkit 将显示图像的 html 页面从 facebook cdn 转换为 pdf。我正在使用 rails 4.2、pdfkit 0.6.2 和 wkhtmltopdf-binary 0.9.9.3.

# Gemfile
gem 'pdfkit'
gem 'wkhtmltopdf-binary'

# controller
def generate_pdf
  @booklet = Booklet.find params[:id]
  @cover = Image.last
  @images = @booklet.images.sort_by(&:uploaded_at)
  respond_to do |format|
    format.html
    format.pdf do
      html = render_to_string(layout: true , action: "generate_pdf.html.haml")
      kit = PDFKit.new(html, page_size: 'A4', print_media_type: true)
      kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/application.scss"
      pdf = kit.to_pdf
      send_data pdf, filename: 'booklet.pdf', type: 'application/pdf', disposition: 'inline'
    end
  end
end

# application.scss
@import 'bootstrap';                                                                                                                                           
@import 'custom';
@import 'jquery.booklet';
@import 'bootstrap-datepicker3';

# haml
= link_to 'Download Booklet', generate_pdf_booklet_path(@booklet, format: 'pdf'), class: 'btn btn-primary'

# config/application.rb
require 'pdfkit'
config.middleware.use PDFKit::Middleware

# config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf unless Mime::Type.lookup_by_extension(:pdf)

示例 facebook cdn 图片 url 是 https://scontent.xx.fbcdn.net/hphotos-prn2/v/t1.0-9/s720x720/560041_10200752471482799_613254552_n.jpg?oh=900fe52ecc9b93e044cae4917f538626&oe=559F41E9 and https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xap1/t31.0-8/s720x720/906468_10201023370815113_668460846_o.jpg

当我发送 pdf 请求时,我在服务器日志中得到以下输出

Rendered booklets/generate_pdf.html.haml within layouts/application   (671.3ms)
QSslSocket: cannot resolve SSLv2_client_method
QSslSocket: cannot resolve SSLv2_server_method
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-e-a.akamaihd.net"
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-e-a.akamaihd.net"
QSslSocket::connectToHostEncrypted() called when already  connecting/connected
QSslSocket::connectToHostEncrypted() called when already connecting/connected
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-b-a.akamaihd.net"
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-b-a.akamaihd.net"
QSslSocket::connectToHostEncrypted() called when already connecting/connected
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-f-a.akamaihd.net"
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "fbcdn-sphotos-c-a.akamaihd.net"
Rendered text template (0.0ms)
Sent data booklet.pdf (1.4ms)

正在生成 pdf,但本地图像 (app/assets/images) 和 cdn 图像未在 pdf 中呈现。此外,样式表未在 pdf 中应用。我错过了什么?

已为上述问题创建了示例存储库。这里:https://github.com/prasadsurase/topdf

我终于设法解决了图像问题。我从 Gemfile 中删除了 wkhtmltopdf-binary gem 并在盒子上安装了 wkhtmltopdf 库(版本 0.9.6)作为

sudo apt-get install wkhtmltopdf

很遗憾,我无法在服务器上安装相同的版本。更好的方法是下载二进制文件并将其保存在应用程序的 bin 文件夹中,并在 pdfkit 初始化程序中指定二进制文件的相对位置。这将消除版本问题。仅供参考,样式表问题仍然存在(PDFkit *.css stylesheets not being applied

这个问题一般是由QT的SSL库引起的。如果更新 wkthmltopdf 没有帮助,找到合适的 libssl-dev 版本可能有助于解决问题。