【问题标题】:what gem/method to use to generate PDF's from static hosted pages using thoughtbot high-voltage使用thoughtbot高压从静态托管页面生成PDF的gem/方法
【发布时间】:2012-05-13 18:04:27
【问题描述】:

我有一组静态帮助页面,我在使用 Thoughbots 高压 gem 的 Rails 3.2 应用程序中提供这些帮助页面。我只是以“香草”的方式使用它,而不通过控制器提供 html 页面。

背景

我最初尝试自己调整 Michael Hartl 关于静态页面的教程 - 即我有一组带有自己的控制器的静态页面,我试图在静态页面视图下创建一个子目录,但可以没有让我的路由正常工作,因此 Google 搜索显示了 Thoughbots High-Voltage gem。

志向

我想要的是推荐使用 Thoughtbots High-Voltage gem 生成 PDF 文件的最佳 gem 或方法。

有人做过吗?

我希望能够在 heroku 上托管这个,所以如果有任何 gotacha's 我想提前知道这些。

我当前的实现是一个基本的 Rails 3.2 应用程序,安装了 High-Voltage gem 并在 pages 子目录下有许多视图。

  • 页面/帮助/用户
  • 页面/帮助/产品
  • 页面/帮助/订单

我的 html 页面中有图片,不确定这是否会导致并发症。

编辑:根据提供的答案添加了控制器,因为 Rails 3.2.3 上的 wicked_pdf 和 Lion 上的 ruby​​ 1.9.3-p125 仍然存在问题

class PagesController < HighVoltage::PagesController
def show
  respond_to do |format|
    format.html do
      super
    end
    format.pdf do
      #render :pdf => "pdf_file" # wicked_pdf syntax here
      render :pdf => :id,
             :layout => 'application',
             #:template => 'help/products/product_tolerance.html.erb',
             :template => 'pages/#{:id}.html.erb',
             :show_as_html => params[:debug],
             :footer => {
                :left => "Generated on @now",
                :centre => "Centre",
                :right => "Page # of page(s)"
             }        
      end
    end
  end
end

路由文件包含:

match "/pages/*id" =&gt; 'pages#show', :as =&gt; :page, :via =&gt; :get, :format =&gt; false

:format 应该是真的吗?在控制器中和高电压中?

【问题讨论】:

  • 感谢刚刚在查看另一个问题时意识到。抱歉,如果我显得粗鲁或无知。这是一个很棒的论坛,有时我在谷歌上搜索了几个小时才能解决问题后,有时迫切需要帮助

标签: ruby-on-rails-3 rubygems pdf-generation ruby-on-rails-plugins


【解决方案1】:

如下所述覆盖高压页面控制器: https://github.com/thoughtbot/high_voltage#override

然后安装pdfkitwicked_pdf(html 到 pdf 转换器) 并将它们连接到该控制器以创建 PDF 版本:

class PagesController < HighVoltage::PagesController
  def show
    respond_to do |format|
      format.html do
        super
      end
      format.pdf do
        render :pdf => "pdf_file" # wicked_pdf syntax here
      end
    end
  end
end

【讨论】:

  • 感谢 Unixmonkey。目前无法让覆盖运行。在路由文件中有match "/pages/*id" =&gt; 'pages#show', :as =&gt; :page , :via =&gt; :get, :format =&gt; false 并得到以下错误:No such page: help/products/index url 是http://localhost:3009/pages/help/products/index
  • 网址是http://localhost:3009/pages/help/products/index 工作了2天。恢复为在没有thoughbot gem 的情况下手动创建页面,遇到了 wicked_pdf 的问题(不创建图像)。导轨 3.2.3,红宝石 1.9.3。 wkhtmltopdf @cmd 行渲染整个页面。可以通过脚本从 Rails 应用程序中简单地调用此 cmd,但目前我的知识有限。
  • 哦,super 是做什么的?它现在呈现以前没有的 html.erb 文件。 Super 不在thoughbot 网站上的示例中。那是一个错误吗?现在,当我尝试使用以下链接运行 pdf 时出现路由错误:&lt;li&gt;&lt;%= link_to "Product Tolerances", page_path("help/products/product_tolerances") %&gt;&lt;%= link_to "Create/Download as PDF", page_path("help/products/product_tolerances", :format =&gt; :pdf) %&gt; &lt;/li&gt; 当我查看 rvm 文件夹中的 wicked_pdf 和 high_voltage gems 时,根据我目前的理解,我认为 high_voltage 仅支持 html
  • @user1149642 super 调用 HighVoltage::PagesController 中的原始 show 方法 (github.com/thoughtbot/high_voltage/blob/master/app/controllers/…)。我认为将:format =&gt; false 带出您的路线将使您可以使用格式键,而未显示的图像是由于 wkhtmltopdf 的工作原理。按照自述文件中的说明使用 wicked_pdf_image_tag 帮助程序。
  • 现在在删除 :format 语句后出现渲染问题。 (试过这个但也拿走了:via =&gt; :get。现在有:No such page: help/products/product_tolerance Rake 路线看起来正确。网址是localhost:3009/pages/help/products/product_tolerance?format=pdf 如果我更改为:template =&gt; '#{:id}.html.erb,我会收到一条丢失的模板消息Missing template /#{:id}.html with {:locale=&gt;[:en], :formats=&gt;[:pdf], :handlers=&gt;[:erb, :builder, :coffee]}. Searched in: * "/Users/railsdev/Development/railsprojects/Learning/thoughtbot/inc_high_voltage/app/views"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-26
相关资源
最近更新 更多