我试图将使用pdfkit将显示图像从facebook cdn转换为pdf的html页面。我使用rails 4.2,pdfkit 0.6.2和wkhtmltopdf-binary 0.9.9.3。PDFkit * .css样式表未应用
# 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)
pdf正在生成和facebook cdn图像显示,但没有应用样式表。我错过了什么?
我创建了一个样本库为上述问题就在这里:https://github.com/prasadsurase/topdf
仅供参考,bootstrap.css和custom.css被放置在供应商/资产,并有扩展已更名为SCSS。在sass中,资产(字体和图像)已经使用'font-path'和'image-url'栏辅助引用。资源已预编译,应用程序-.... css被复制到pdf.css,并且资源从根路径引用(/)
由于Asset管道的原因,这似乎是Rails 3.1和PDF工具包的一个运行问题。我在这里发现了一个类似的问题,希望能有所帮助。 http://stackoverflow.com/questions/8044659/pdfkit-does-not-style-pdfs – cmw2379 2015-04-08 05:05:13
@ cmw2379我试过解决方案,但它没有奏效。你能否克隆回购并试一试? – 2015-04-08 05:42:46
如果我今晚得到一些时间,我会试试看。 – cmw2379 2015-04-08 14:30:41