回答
我會與wicked_pdf
紅寶石的寶石,它的自由結合使用wkhtmltopdf
shell工具 ,並使用QtWebKit的渲染你的HTML到PDF。例如,還爲圖表執行JavaScript。你可以找到有關安裝的詳細信息:https://github.com/mileszs/wicked_pdf
謝謝。我正在使用wicked_pdf rails插件。不過,我創建了一個新帖子。基本上我的問題是與頁眉和頁腳選項。我在Windows平臺上使用rails 2.3.8。 – maxiperez
我有一個已經使用PrinceXML生產了幾年的一個Rails應用程序。這是一個昂貴的 - 服務器許可證大約4K美元 - 但在PDF文件中呈現HTML + CSS做得非常好。我沒有看過較新的解決方案,因爲這個解決方案付費並且工作得很好。
對於它的價值,這裏的一些代碼,我適應from Subimage Interactive進行轉換簡單:
的lib/prince.rb
# Prince XML Ruby interface.
# http://www.princexml.com
#
# Library by Subimage Interactive - http://www.subimage.com
#
#
# USAGE
# -----------------------------------------------------------------------------
# prince = Prince.new()
# html_string = render_to_string(:template => 'some_document')
# send_data(
# prince.pdf_from_string(html_string),
# :filename => 'some_document.pdf'
# :type => 'application/pdf'
# )
#
class Prince
attr_accessor :exe_path, :style_sheets, :log_file
# Initialize method
#
def initialize()
# Finds where the application lives, so we can call it.
@exe_path = '/usr/local/bin/prince'
case Rails.env
when 'production', 'staging'
# use default hard-coded path
else
if File.exist?(@exe_path)
# use default hard-coded path
else
@exe_path = `which prince`.chomp
end
end
@style_sheets = ''
@log_file = "#{::Rails.root}/log/prince.log"
end
# Sets stylesheets...
# Can pass in multiple paths for css files.
#
def add_style_sheets(*sheets)
for sheet in sheets do
@style_sheets << " -s #{sheet} "
end
end
# Returns fully formed executable path with any command line switches
# we've set based on our variables.
#
def exe_path
# Add any standard cmd line arguments we need to pass
@exe_path << " --input=html --server --log=#{@log_file} "
@exe_path << @style_sheets
return @exe_path
end
# Makes a pdf from a passed in string.
#
# Returns PDF as a stream, so we can use send_data to shoot
# it down the pipe using Rails.
#
def pdf_from_string(string)
path = self.exe_path()
# Don't spew errors to the standard out...and set up to take IO
# as input and output
path << ' --silent - -o -'
# Show the command used...
#logger.info "\n\nPRINCE XML PDF COMMAND"
#logger.info path
#logger.info ''
# Actually call the prince command, and pass the entire data stream back.
pdf = IO.popen(path, "w+")
pdf.puts(string)
pdf.close_write
output = pdf.gets(nil)
pdf.close_read
return output
end
end
的lib/pdf_helper.rb
module PdfHelper
require 'prince'
private
def make_pdf(template_path, pdf_name, stylesheets = [], skip_base_pdf_stylesheet = false)
# application notices should never be included in PDFs, pull them from session here
notices = nil
if !flash.now[:notice].nil?
notices = flash.now[:notice]
flash.now[:notice] = nil
end
if !flash[:notice].nil?
notices = '' if notices.nil?
notices << flash[:notice]
flash[:notice] = nil
end
prince = Prince.new()
# Sets style sheets on PDF renderer.
stylesheet_base = "#{::Rails.root}/public/stylesheets"
prince.add_style_sheets(
"#{stylesheet_base}/application.css",
"#{stylesheet_base}/print.css"
)
prince.add_style_sheets("#{stylesheet_base}/pdf.css") unless skip_base_pdf_stylesheet
if 0 < stylesheets.size
stylesheets.each { |s| prince.add_style_sheets("#{stylesheet_base}/#{s}.css") }
end
# Set RAILS_ASSET_ID to blank string or rails appends some time after
# to prevent file caching, messing up local - disk requests.
ENV['RAILS_ASSET_ID'] = ''
html_string = render_to_string(:template => template_path, :layout => 'application')
# Make all paths relative, on disk paths...
html_string.gsub!("src=\"", "src=\"#{::Rails.root}/public")
html_string.gsub!("src=\"#{::Rails.root}/public#{::Rails.root}", "src=\"#{::Rails.root}")
# re-insert any application notices into the session
if !notices.nil?
flash[:notice] = notices
end
return prince.pdf_from_string(html_string)
end
def make_and_send_pdf(template_path, pdf_name, stylesheets = [], skip_base_pdf_stylesheet = false)
send_data(
make_pdf(template_path, pdf_name, stylesheets, skip_base_pdf_stylesheet),
:filename => pdf_name,
:type => 'application/pdf'
)
end
end
樣本控制器動作
include PdfHelper
def pdf
index
make_and_send_pdf '/ads/index', "#{filename}.pdf"
end
您可以直接使用acts_as_flying_saucer library.For頭現有的HTML轉換爲PDF和頁腳,你可以參考 https://github.com/amardaxini/acts_as_flying_saucer/wiki/PDF-Header-Footer
- 1. 將html呈現爲pdf
- 2. 如何讓Rails爲PDFKit呈現PDF特定的HTML?
- 3. 如何呈現HTML Django頁面爲PDF?
- 4. UIPrintPageRenderer呈現爲PDF(字體呈現)
- 5. Rails的QR碼呈現爲HTML表格
- 6. Rails將html呈現爲散列值
- 7. Rails的呈現在HTML
- 8. 將報告呈現爲pdf
- 9. pdf.js呈現爲PDF與base64
- 10. 從HTML中的其他域呈現PDF
- 11. 從定製Html呈現定製PDF
- 12. Rails:在一個請求中呈現html和js
- 13. 呈現多個HTML表
- 14. Rails:將HTML轉換爲PDF?
- 15. Rails 2.3.8問題
- 16. Rails 2.3.8 + mongrel
- 17. 如何將HTML呈現爲jQuery.tmpl()中呈現的HTML?
- 18. 將HTML呈現爲PNG/JPEG
- 19. 將HTML呈現爲TIFF
- 20. 使用PhantomJS在DOMContentLoaded/Document Ready上將HTML呈現爲PDF
- 21. 在Java中提取PDF文件並呈現爲HTML
- 22. 當將HTML轉換爲PDF時頁腳無法呈現
- 23. 使用可變數據呈現HTML並將其轉換爲PDF
- 24. 獲取jspdf將html呈現爲PDF需要幫助
- 25. 在rails中呈現HTML文件
- 26. 呈現HTML而不是JSON:Ruby on Rails
- 27. rails:404.html不在IE中呈現
- 28. jQuery從Rails視圖呈現html
- 29. 呈現多個文件 - rails
- 30. 呈現一個html元素數組
蝦使用wkhtmltopdf這是一個偉大的圖書館,即使是在待機獨自一人:自由而直挺。 – apneadiving