PDFkit * .css stylesheets not applicable

I am trying to convert an 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', orientation: 'Landscape') `sass vendor/assets/stylesheets/bootstrap.scss tmp/bootstrap.css` `sass vendor/assets/stylesheets/custom.scss tmp/custom.css` kit.stylesheets << "#{Rails.root}/tmp/bootstrap.css" kit.stylesheets << "#{Rails.root}/tmp/custom.css" pdf = kit.to_pdf send_data pdf, filename: 'booklet.pdf', type: 'application/pdf' end end end # application.scss @import 'bootstrap'; @import 'custom'; # 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) 

A PDF is created and facebook cdn images are displayed, but the stylesheets arent attribute is applied. What am I missing?

I created a test repository for the above problem: https://github.com/prasadsurase/topdf

FYI, bootstrap.css and custom.css are hosted in providers / assets, and extensions are renamed to scss. In sass, assets (fonts and images) were transferred using the "font-path" and "image-url" rails helper. The assets were precompiled and the application -.... css was copied to pdf.css and the assets transferred from the root path (/)

+6
source share
2 answers

Does Bootstrap work correctly for other pages in your application? - The goal is to make sure that you have correctly configured the meta tags to include Bootstrap files, and that you are pointing to Bootstrap files (which seem to be in / tmp?)

Also, what OS are you coding for? Can I recommend replacing "#{Rails.root}/tmp/bootstrap.css" with Rails.root.join('tmp','bootstrap.css')

Let me know if they help at all - or not.

0
source

Switch to wicked_pdf gem. The asset assistants included work fine and the gem is actually better organized and documented.

0
source

Source: https://habr.com/ru/post/984661/


All Articles