Wicked PDF - 如果我向 PDF 的页眉或页脚添加任何 html,它会将 PDF 变成空白白色

问题描述 投票:0回答:1

如果我将任何 html 添加到 Wicked_PDF 生成的 PDF 的页眉或页脚中,它会将 PDF 变成空白白色。它可能与 render_to_string() 有关。

我正在使用 Ruby on Rails 7 应用程序生成包含以下内容的 PDF:

我的控制器:

def generate_pdf
    # generation logic
    # Assume the PDF content is in variable pdf_content
    pdf_filename = generate_pdf_filename
    
    s3 = Aws::S3::Resource.new(region: ENV['S3_REGION'])
    obj = s3.bucket(ENV['S3_BUCKET_NAME']).object(pdf_filename)
    obj.put(body: render_to_string(
      pdf: pdf_filename,
      template: "blank/_lorem",
      formats: [:html,:pdf],
      layout: "pdf_layout",
      page_size: 'Letter',
      encoding: 'utf8',
      javascript_delay: 600,
      footer: {
          html: {
            template: "blank/pdf_footer"
          }
        }
    ))
  
    # Save a record in the database
    GeneratedPdf.create(user: current_user, s3_key: pdf_filename)
  
    # Provide feedback to the user or redirect
    redirect_to blank_index_path, notice: 'PDF generated successfully!'
  end

因此,如果我像这样使用页脚(pdf_footer.html.erb),一切都会变成空白白色:

<div class="ajg-pdf-footer" style="width:100%">
    <hr style="clear:both;">
    <%= wicked_pdf_image_tag asset_url('/images/affy-logo-github.png'), width: 50 %>
    <span class="move-right">Page <span class="page"></span> of <span class="topage"></span></span>
</div>

显示空白 PDF: Screenshot 2023-09-19 at 2 50 21 PM

但是如果我删除所有 html,它会生成 PDF(pdf_footer.html.erb):

This is the html footer...

PDF 生成良好: Screenshot 2023-09-19 at 2 48 10 PM

我相信它与渲染为字符串有关 - 或者命名文件 html.erb 或 pdf.erb 之间的区别。 - 这是一个单独的示例,我可以用它以更简单的方式生成 PDF。请注意,在此我必须使用 pdf.erb 格式的页眉和页脚文件,而不是 html.erb - 如果我不这样做,则会引发缺少模板错误。

我的控制器用于更简单的生成

class TestController < ApplicationController
  def index
    authorize :page, :affygility?
    respond_to do |format|
      format.html
      format.pdf do
        render pdf: "pdf-test-#{DateTime.now.strftime('%Y-%m-%d-%H-%M-%S')}", 
        save_to_file: Rails.root.join('public/pdfs', "pdf-test-#{DateTime.now.strftime('%Y-%m-%d-%H-%M-%S')}.pdf"),
        template: "test/index", 
        formats: [:html], 
        layout: "pdf_layout",
        page_size: 'Letter',
        encoding: 'utf8',
        javascript_delay: 600,
        header: {
          html: {
            template: "test/test_header"
          }
        },
        footer: {
            html: {
              template: "test/test_footer"
            }
          }
      end
    end
  end
end

页眉和页脚被保存为 pdf.erb 而不是 html.erb - 如果我不这样做,它会抛出丢失模板错误。

Screenshot 2023-09-19 at 2 41 40 PM

使用上面更简单的方法并使用 pdf.erb 保存页眉和页脚:

页眉和页脚显示得很好: Screenshot 2023-09-19 at 2 56 52 PM Screenshot 2023-09-19 at 2 57 03 PM

有人可以告诉我我做错了什么吗?谢谢!

Ruby on Rails 7

wicked_pdf gem 版本(wicked_pdf (2.7.0)):

wkhtmltopdf 版本(wkhtmltopdf 0.12.6(带有修补的 qt)):

ruby-on-rails wkhtmltopdf wicked-pdf
1个回答
0
投票

我刚刚弄清楚这一点 - 我将关闭该问题。我保留页眉和页脚文件的格式为 pdf.erb,我只需要在页眉和页脚中添加格式:[:pdf]。

header: {
        html: {
          template: "blank/pdf_header",
          formats: [:pdf]
        }
      },
      footer: {
          html: {
            template: "blank/pdf_footer",
            formats: [:pdf]
          }
        }

供参考: Screenshot 2023-09-19 at 3 34 43 PM

© www.soinside.com 2019 - 2024. All rights reserved.