wicked_pdf - 显示用于生成PDF但不生成PDF的html.erb模板中的链接

时间:2012-05-31 00:29:04

标签: pdf-generation ruby-on-rails-3.2 wicked-pdf

已经取得进展,以便在Rails 3.2.3中生成wicked_pdf生成PDF。但是我希望能够将我的页面上的链接作为HTML从.html.erb文件呈现为屏幕,但是当我查看从该模板生成的PDF时,我不希望看到这些链接。

我曾试图关注Ryan Bates在PDFKit Railscast 220中所做的事情,但它在Rails 3.2.3下对我不起作用,在Ruby 1.9.3上。

以下是我查看代码的简略部分:

<h2>Client Setup (Only when Patients module is not available)</h2>
<p>
The setup program installs your Clients module using default settings.  After the installation, you can use this program to customize settings to meet your particular needs.
</p>
<p>
  <%= pdf_image_tag("clients/blank/.png",  alt: "Client Setup (Only when Patients Module is not available) - not-populated") %>
<table>
  <thead>
    <tr>
      <th>Form Item</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Default Value Added Tax (Percent)</td>
      <td>
        The package offers a default Value Added Tax, expressed as a percentage of the invoice, to be added to the invoice.  Numbers from 0 to 999.99 are allowed.
      </td>
    </tr>
  </tbody>
</table>
</p>

<hr />
<form class="form-inline">
  <a href="#to_top" class="btn btn-to_top btn-mini">Back to Top</a>
  <%= link_to "Genie Help Index", help_path, class: "btn btn-main-menu pdf_link" %>
  <p id="pdf_link"><%= link_to "Client Help Index", static_page_path("clients/index"), class: "btn btn-help" %></p>
  <%= link_to "Download PDF", static_page_path(:format => :pdf), class: "btn btn-pdf" %>
  <%= link_to image_tag("file_type_pdf.png", height: "24px", width: "24px" , alt: "Download page as PDF"), static_page_path(:format => :pdf) %>
</form>

<p><%= link_to "Client Help Index", static_page_path("clients/index") %></p>
<p><%= link_to "Download as PDF", static_page_path(:format => "pdf"), class: "pdf_link" %></p>
<p id="pdf_link"><%= link_to "Download as PDF", static_page_path(:format => :pdf) %></p>


<% if request.try(:format).to_s == 'pdf' %>
  <%= link_to "Download this PDF", static_page_path(:format => "pdf") %>
<% end %>

#<% if params[:media] = 'all' %>
#  <%= link_to "Download me as a PDF", static_page_path(:format => "pdf") %>
#<% end %>

<div id="pdf-no"><%= link_to "Get me as a PDF file", static_page_path(:format => "pdf") %></div>

控制器是:(显示页面是要呈现的页面的名称)

class StaticPages::GenieHelpController < ApplicationController
  def static_page
    respond_to do |format|
      format.html do
        render :template => show_page,
               :layout => 'application'
      end
      format.pdf do
        render :pdf => show_page,
               :layout => 'generic',
               :template => "#{show_page}.html.erb",
               :handlers => :erb,
               :disable_external_links => true,
               :print_media_type => true
      end
    end
  end
end

views / layouts / generic.pdf.erb文件中的布局文件如下:

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= wicked_pdf_stylesheet_link_tag    "static_pages/genie_v23_help", :refer_only => true %>
    <!--     <%#= wicked_pdf_stylesheet_link_tag    "static_pages/pdf" %>    --> 
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>  
  </head>
  <body>
    <div class="container">
      <%= yield %>
      <%= debug(params) if Rails.env.development? %>
    </div>
  </body>
</html>

旧位置的相应css文件public / stylesheets / static_pages / genie_help.css:

@media print {
    body { background-color: LightGreen; }
    #container {
                  width: auto;
                  margin: 0;
                  padding: 0;
                  border: 2px;
    }
  #pdf_link {
    display: none;
  }
}
  .pdf_link {
    display: none;
  }

#pdf-no {
    display:none;
}

当我呈现html页面时,当格式为html时,底部的链接显示(按预期)。

Rendered PDF but with links still showing - these are what I want to remove BUT How?

我对此做错了什么。我假设如果它可以通过PDFKit的中间件完成,那么它在wkhtmltopdf下受支持,因为PDFKit和wicked_pdf都基于此。

由于

标记

1 个答案:

答案 0 :(得分:0)

原来,我通过混合和匹配基于lib帮助文件的wicked_helper.pdf文件中的四个方法解决了这个问题。

wicked_pdf_image_tag已更改为在拉取请求等待提交中使用的pdf_image_tag - mkoentopf

  

多用途wicked_pdf_helper“你好,我已经为wicked_pdf_helper方法添加了一些代码。现在他们提供了完整的pa ...”

  def wicked_pdf_image_tag(img, options={})
    if request.try(:format).to_s == 'application/pdf'
      image_tag "file://#{Rails.root.join('public', 'images', img)}", options rescue nil
    else
      image_tag img.to_s, options rescue nil
    end
  end

我不理解的是在Rails 3.2下我们是否还在使用公共目录,因为sprockets和assests被预编译并公开放置?

我当天早些时候使用的解决方案是在html布局中添加额外的页眉/页脚部分,但不在pdf生成的布局中添加。

这是我正在使用的帮助文件:

module WickedPdfHelper
  def wicked_pdf_stylesheet_link_tag(*sources)
    options = sources.extract_options!
    if request.try(:format).to_s == 'application/pdf'
      #css_dir = Rails.root.join('public','stylesheets')
      css_dir = Rails.root.join('app','assets', 'stylesheets')
      refer_only = options.delete(:refer_only)
      sources.collect { |source|
        source.sub!(/\.css$/o,'')
        if refer_only
          stylesheet_link_tag "file://#{Rails.root.join('public','stylesheets',source+'.css')}", options
        else
          "<style type='text/css'>#{File.read(css_dir.join(source+'.css'))}</style>"
        end
      }.join("\n").html_safe
    else
      sources.collect { |source|
        stylesheet_link_tag(source, options)
      }.join("\n").html_safe
    end
  end

  def pdf_image_tag(img, options={})
    if request.try(:format).to_s == 'application/pdf'
      image_tag "file://#{Rails.root.join('app', 'assets', 'images', img)}", options rescue nil
    else
      image_tag img.to_s, options rescue nil
    end
  end

  def wicked_pdf_javascript_src_tag(jsfile, options={})
    if request.try(:format).to_s == 'application/pdf'
      jsfile.sub!(/\.js$/o,'')
      javascript_src_tag "file://#{Rails.root.join('public','javascripts',jsfile + '.js')}", options
    else
      javascript_src_tag jsfile, options
    end
  end

  def wicked_pdf_javascript_include_tag(*sources)
    options = sources.extract_options!
    sources.collect{ |source| wicked_pdf_javascript_src_tag(source, options) }.join("\n").html_safe
  end
end

了解有关资产管道如何使用的更多信息会很好。以及如何确定@media。它与MIME类型有关吗?如果是这样,为什么我们不需要/或它们是否未在wick_pdf中定义?

我还没有利用页面编号和破解,但现在有一个我将单独提出的一个悬而未决的问题。