pdfkit无法从cdn + rails 4.2 + pdfkit渲染图像

时间:2015-03-30 17:23:23

标签: ruby-on-rails-4 pdfkit

我正在尝试使用pdfkit将显示图像从facebook cdn转换为pdf的html页面。我使用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 image urls是 https://scontent.xx.fbcdn.net/hphotos-prn2/v/t1.0-9/s720x720/560041_10200752471482799_613254552_n.jpg?oh=900fe52ecc9b93e044cae4917f538626&oe=559F41E9https://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。我错过了什么?

html page with images from facebook cdn

generated pdf

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

2 个答案:

答案 0 :(得分:0)

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

sudo apt-get install wkhtmltopdf

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

答案 1 :(得分:0)

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