邪恶的 PDF header 没有显示

Wicked PDF header not showing up

在检查了 Rails Gem 存储库中的类似问题以及 Stack Overflow 之后,我找不到问题的答案。

我正在尝试在 Rails 控制器中使用 wicked_pdf 呈现 pdf,但是 header 没有显示,无论我做什么或推荐的解决方案我关注的类似问题。

首先,这是开发控制台输出:

***************WICKED***************
  Rendering biddings/show.pdf.html.haml within layouts/pdf
  Rendered biddings/show.pdf.html.haml within layouts/pdf (0.7ms)
  Rendering biddings/header_pdf.html.haml within layouts/pdf_header
  Rendered biddings/header_pdf.html.haml within layouts/pdf_header (1.9ms)
"***************[\"/home/tommy/.rvm/gems/ruby-
 2.5.1@igalbids/bin/wkhtmltopdf\", \"-q\", \"--encoding\", \"UTF-8\",
  \"--javascript-delay\", \"500\", 
  \"--disable-internal-links\", \"--disable-external-links\", 
  \"--orientation\", \"Portrait\", \"--margin-top\", 
  \"50\", \"--margin-bottom\", \"25\", \"--header-html\", 
  \"file:////tmp/wicked_header_pdf20180801-27285-b8y5sg.html\", 
  \"--footer-right\", \"Página [page] de [topage]\",
  \"file:////tmp/wicked_pdf20180801-27285-1jfgdd7.html\", 
  \"/tmp/wicked_pdf_generated_file20180801-27285-1bkrvhx.pdf\"]***************"
  Rendering text template
  Rendered text template (0.1ms)
Sent data Licitación_2524.pdf (0.6ms)
Completed 200 OK in 2334ms (Views: 0.5ms | ActiveRecord: 64.4ms)

如您所见,header 布局及其内容都在渲染和处理中,但是它们没有生成最终输出 PDF,我不知道为什么!看:

所以,这是我的控制器代码:

class Api::V1::Biddings::PdfBiddingsController < PdfController
  # JWT Authentication enforced
  before_action :authenticate_user!

  # GET /biddings/:id/pdf
  def show
    @bidding = scoped_collection.find(params[:id])

    authorize [:biddings, :pdf, @bidding]
    respond_to do |format|
      format.pdf do
        render(
          pdf: "#{Bidding.model_name.human}_#{@bidding.code}",
          disposition: "inline",
          orientation: "Portrait",
          template: 'biddings/show.pdf.html.haml',
          header: {
            html: {
              template: "biddings/header_pdf.html.haml",
              handlers: [:haml],
              layout: "pdf_header",
              formats: [:haml, :html]
            }
          },
          footer: {
            html: {
              handlers: [:haml],
              layout: "pdf",
              formats: [:haml, :html],
              encoding: 'UTF-8'
            },
            right: "#{I18n.t('pdf.page')} [page] #{I18n.t('pdf.of')} [topage]"
          },
          margin: { :top => 50, :bottom => 25},
          handlers: [:haml],
          layout: "pdf",
          javascript_delay: 500,
          encoding: 'UTF-8',
          show_as_html: false,
          disable_internal_links: true,
          disable_external_links: true) and return
      end
    end
  end

  protected
  def self.model
    Bidding
  end

  private
  def scoped_collection
    policy_scope([:biddings, :pdf, Bidding]).includes(:bidding_type, :client, :payment_condition, :price_list, :real_payment_condition, :sales_man, :user)
  end

  def records_per_page
    params[:per_page] || 10
  end
end

没什么特别的,在那里你可以看到所有的配置选项,非常标准。不用说,带页码的页脚工作正常(屏幕截图太长无法显示,但请相信我)。不能对 header.

说同样的话

这是 PDF header 布局文件:

pdf_header.html.haml:

!!! 5
%html
  %head
    %meta{:content => "text/html; charset=utf-8", "http-equiv" => "content-type"}/
    = wicked_pdf_stylesheet_link_tag "bidding_pdf", media: :all
    = csrf_meta_tags
  %body.pdf
    = yield

这里是 header "contents" 本身的内容 :

header_pdf.html.haml:

Test text

纯文本。我有一个 Linux 16.04 x64 OS、wicked_pdf (1.1.0)、wkhtmltopdf-binary (0.12.4)。我该如何调试?

对于到达此处的任何其他人...这是一个 CSS 问题。 header 在那里,但 "invisible" 并且无论我在渲染选项上设置的余量如何,这都是一个 CSS 问题。从头开始CSS后,header出现了!我无法使用标志 show_as_html: true 调试它,因为 header 和页脚未在该模式下呈现,只有 body.

如果有人读到这篇文章并且恰好遇到相同情况,请使用 PDF 文档中的搜索工具查找 header 中您知道的单词。如果它找到了一些东西但它是不可见的,那么你就知道你遇到了 CSS 问题。 另外不要忘记 检查您是否包含在header 的html 和<!DOCTYPE html> 中。感谢@joaolell。

另一件要检查的事情是,您的 wkhtmltopdf 库(0.12.4 及更高版本)的 qt 补丁版本支持 header 和页脚。以前的版本不会

对于遇到此问题的任何人,由于 OP 的回答不是太精确,我的工作是在您的 header/footer 中包含一个 DOCTYPE HTML 标签。从不可见 header(使用搜索工具可以找到文本)到完全呈现。

将 wkhtmltopdf 二进制文件升级到至少版本“0.12.4(带有修补的 qt)”。刚花了半天时间排错,因为我的0.12.1版本不支持页眉页脚

参考: