【问题标题】:how to give custom margin for coverpage with wicked_pdf in ruby on rails如何在 ruby​​ on rails 中使用 wicked_pdf 为封面提供自定义边距
【发布时间】:2019-09-23 09:28:18
【问题描述】:

我不想在生成 PDF 时为第一页留出空白

这是生成 pdf 的 ruby​​ 代码

  def files
    respond_to do |format|
      format.html
      format.pdf do
        render pdf: params[:filename],
               page_size: 'A4',
               dpi: '300',
               user_style_sheet: '../assets/stylesheet/quotation.css',
               margin:  {
                top:               0,
                bottom:            0,
                left:              0,
                right:             0
              }
      end
    end
  end

【问题讨论】:

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


    【解决方案1】:

    页眉、页脚和边距对于创建的 PDF 来说是全局的,因此您不能单独调整封面。

    但是,您可以创建两个 PDF,一个只是封面,一个是其余的,然后将它们与 Ghostscript 或 PDFtk 结合起来。

    html_content = render_to_string
    cover_pdf = WickedPdf.new.pdf_from_string(html_content, { footer: { margin: { bottom: 200 })
    body_pdf = WickedPdf.new.pdf_from_string(html_content, { footer: { margin: { bottom: 10 })
    
    cover_src_temp_file = Tempfile.new(['cover_src', '.pdf'])
    cover_src_temp_file.binmode
    cover_src_temp_file.write(cover_pdf)
    cover_src_temp_file.rewind
    cover_temp_file = Tempfile.new(cover_pdf)
    `pdftk #{cover_src_temp_file} cat 1 output #{cover_temp_file.path.to_s}` # first page only
    
    body_src_temp_file = Tempfile.new(['body_src', '.pdf'])
    body_src_temp_file.binmode
    body_src_temp_file.write(cover_pdf)
    body_src_temp_file.rewind
    body_temp_file = Tempfile.new(body_pdf)
    `pdftk #{body_src_temp_file.path} cat 2-end output #{body_temp_file.path}` # everything else
    
    output_temp_file = Tempfile.new(['output', '.pdf'])
    `pdftk #{cover_temp_file.path} #{body_temp_file.path} cat output #{output_temp_file.path}`
    send_file output_temp_file, disposition: 'inline'
    
    [cover_src_temp_file, body_src_temp_file, cover_temp_file, body_temp_file, output_temp_file].each do |tf|
    tf.close
    tf.unlink
    end
    

    来源 - https://github.com/mileszs/wicked_pdf/issues/572#issuecomment-245044036

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-16
      • 2014-07-27
      • 2012-04-03
      • 1970-01-01
      相关资源
      最近更新 更多