首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 2.3.8将html呈现为pdf

Rails 2.3.8将html呈现为pdf
EN

Stack Overflow用户
提问于 2011-07-06 03:53:22
回答 3查看 900关注 0票数 0

我正在使用虾pdf库,但我正在做一个复杂的设计,所以我需要一个快速的解决方案,以转换为一个html到pdf文件。

提前感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-07-06 05:22:00

我会使用wkhtmltopdf外壳工具和wicked_pdf ruby gem,它是免费的,并使用qtwebkit将您的html呈现为pdf。也会执行javascript,例如图表。您可以找到有关安装的更多信息:https://github.com/mileszs/wicked_pdf

票数 1
EN

Stack Overflow用户

发布于 2011-07-06 04:28:26

我有一个Rails应用程序,它在生产中使用PrinceXML已经有几年了。这是昂贵的-大约4K美元的服务器许可证-但在渲染HTML+CSS文件中做得很好。我还没有考虑过更新的解决方案,因为这个方案是付费的,而且工作得很好。

为了使转换更简单,这里有一些我改编的from Subimage Interactive代码:

lib/prince.rb

代码语言:javascript
复制
# 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

代码语言:javascript
复制
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

示例控制器操作

代码语言:javascript
复制
include PdfHelper
def pdf
  index
  make_and_send_pdf '/ads/index', "#{filename}.pdf"
end
票数 0
EN

Stack Overflow用户

发布于 2011-09-10 15:16:15

您可以使用acts_as_flying_saucer library.For页眉和页脚直接将现有的HTML转换为https://github.com/amardaxini/acts_as_flying_saucer/wiki/PDF-Header-Footer

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6588109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档