【发布时间】:2015-06-03 20:01:03
【问题描述】:
我正在尝试使用 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 图片网址是 https://scontent.xx.fbcdn.net/hphotos-prn2/v/t1.0-9/s720x720/560041_10200752471482799_613254552_n.jpg?oh=900fe52ecc9b93e044cae4917f538626&oe=559F41E9 和 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
【问题讨论】:
-
同样的问题,你有解决方法吗?