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?
Have created a sample repository for the above problem. Here: https://github.com/prasadsurase/topdf
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)
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.
Related
I have set the disposition property of the method to inline,so i am able to view pdf file in browser but same i want to be done for docx file.But instead of viewing in separate tab the file is downloading.
def download_emp
send_file #employee_document.document.path,
filename: #employee_document.document,
type: #employee_document.document_content_type,
disposition: 'inline'
end
But i want the docx attachment to be viewed in browser instead of downloading.
The link where you use the above method, try adding target: "_blank". Below is an example,
link_to "View", 'url', target: "_blank"
But downloading a doc file is the default feature of a browser, which cannot be replaced. But giving a try to the above code is worth.
I have an action in my controller #applications that renders a html page of my websites application page. When I specify the path for this action with the :pdf format it will render the page as a pdf (which works correctly).
I am writing another action #applications_print_version that needs to contain the rendered view from #applications plus some other pdfs (from a list of urls). Using combine_pdf I have been able to get the list of pdfs via urls working, but I can not get the #applications view to be added to the combined pdf in the #applications_print_version working.
Here is what I have so far.
def applications_print_version
html = render_to_string(action: :applications)
pdf = WickedPdf.new.pdf_from_string(html)
new_pdf = CombinePDF.new
new_pdf << CombinePDF.parse(pdf)
#List of pdfs I got from somewhere else
#pdf_attachments.each { |att| new_pdf << CombinePDF.parse( Net::HTTP.get( URI.parse( att.url ) ) ) }
send_data new_pdf.to_pdf, :disposition => 'inline', :type => "application/pdf"
end
This solution does have all the data, but the pdf variable has no styling. I can't seem to get this to work.
Thanks to a local community support I have managed to get it to work. There were a few things I needed to fix.
The layout that I was using to render #applications was using my standard pdf layout that I used for PDFKit. I duplicated the layout and replaced
<%= stylesheet_link_tag 'application' %>
with
<%= wicked_pdf_stylesheet_link_tag 'application' %>
One I did that I could render the #applications action with the layout the WickedPDF needed.
html = render_to_string(action: :applications, layout: 'wicked_pdf')
I ran into another issue. There is a known issue with WickedPDF https://github.com/mileszs/wicked_pdf/issues/470
So I had to remove any instances of #import "bootstrap" in my stylesheets which is not ideal, but I could not resolve the above issue.
Any now the #applications_print_version works correctly!
If anyone can do better, please let me know, I would like to know :)
Using rails_admin gem, I want to create a clickable link to rails_admin dashboard's left hand side bar.
Path of that link should be according to the rails path. I don't want to add any model for the same.
Please help me in adding the link.
You can add in your rails admin initializer:
config.navigation_static_links = {
'Dashboard' => '/admin' #or whatever you used to mount RailsAdmin in your routes file
}
Used firebug and inspected Spree default logo is coming from
/assets/store/logo.png
Checked in spree 2.1.3 Version Source code and found default logo is coming from /logo section.
path :
core/app/models/spree/app_configuration.rb
preference :logo, :string, default: 'logo/spree_50.png'
Trying to Override, the default logo using Spree:Config option.
Spree::Config.set(logo: "store/logo.png")
But it's still redirecting to the old image(spree_50.png) and I am unable to replace the default logo.
In application.rb:
Spree.config do |config|
# Example:
# Uncomment to override the default site name.
# config.site_name = "Spree Demo Site"
config.logo = "logo.png"
end
Then add your logo to public/images/ and the logo will show up.
You can place your image at this path.
Inside: app/assets/images/logo/"Your Image"
for eg. app/assets/images/logo/hi5_logo.png**
Inside: config/initializers/spree.rb
Spree.config do |config|
config.logo = "logo/hi5_logo.png" #for default store logo
config.admin_interface_logo = "logo/hi5_logo.png" #for admin logo
end
Environment: heroku (cedar)
Rails: 4.0
Ruby: 2.0
In my production.rb I have
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
And for image_tag helpers etc in my main layout, everything is pointing to the correct asset location (using as3).
The issue is that my css file: scroll-up-down.css.erb doesn't appear to use the correct asset path.
.nav_up{
background: #fff url(<%= asset_path 'scroller-arrow_up.png' %>) no-repeat 50% 50%;
left:30px;
}
which is resolving as
'/assets/scroller-arrow_up.png'
in the compiled css file?
Any ideas? Everything else works great.
Cleaned assets and pushed again after another precompile. It seems to work now.