2015-04-03 43 views
6

我试图将使用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,并且资源从根路径引用(/)

+0

由于Asset管道的原因,这似乎是Rails 3.1和PDF工具包的一个运行问题。我在这里发现了一个类似的问题,希望能有所帮助。 http://stackoverflow.com/questions/8044659/pdfkit-does-not-style-pdfs – cmw2379 2015-04-08 05:05:13

+0

@ cmw2379我试过解决方案,但它没有奏效。你能否克隆回购并试一试? – 2015-04-08 05:42:46

+0

如果我今晚得到一些时间,我会试试看。 – cmw2379 2015-04-08 14:30:41

回答

0

Bootstrap是否可以正确地为应用程序中的其他页面工作? - 目标是确保您正确配置元标记以包含引导程序文件,并且您指向引导程序文件(似乎在/ tmp中?)

另外,在哪些操作系统上你编码?我可以建议用Rails.root.join('tmp','bootstrap.css')替代"#{Rails.root}/tmp/bootstrap.css"

让我知道这些是否有帮助 - 或不。

0

切换到wicked_pdf gem。包括资产助手的工作很好,而且宝石实际上更好地组织和记录。

+0

在发布这个问题后的两天,我很匆忙,所以搬到了wicked_pdf。有效。谢谢。 – 2015-10-08 06:27:47