2

I am trying to convert a html page that displays images from facebook cdn to pdf using pdfkit. I am using rails 4.2, pdfkit 0.6.2 and 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)

example facebook cdn image urls are 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

When i send a pdf request, I get following output on the server log

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)

The pdf is getting generated but the local images(app/assets/images) and the cdn images arent rendered in the pdf. Also, the stylesheet isnt applied in the pdf. What am I missing?

html page with images from facebook cdn

generated pdf

Have created a sample repository for the above problem. Here: https://github.com/prasadsurase/topdf

Prasad Surase
  • 6,486
  • 6
  • 39
  • 58

2 Answers2

0

I finally managed to fix the images issue. I removed the wkhtmltopdf-binary gem from the Gemfile and installed the wkhtmltopdf library(version 0.9.6) on the box as

sudo apt-get install wkhtmltopdf

Unfortunately, I am not able to install the same version on the server. A better approach would be to download the binary and keep it in your application in the bin folder and specify the binary's relative location in the pdfkit initializer. This would remove the version issues. FYI, the stylesheet issue still persists( PDFkit *.css stylesheets not being applied)

Community
  • 1
  • 1
Prasad Surase
  • 6,486
  • 6
  • 39
  • 58
0

This issue usually caused by the SSL library of QT. If updating wkthmltopdf doesn't help, finding a suitable libssl-dev version may help to solve the issue.

Hank Phung
  • 2,059
  • 1
  • 23
  • 38