【问题标题】:Wicked PDF: How to remove top margin from COVER page?邪恶的 PDF:如何从 COVER 页面中删除上边距?
【发布时间】:2017-06-08 11:31:39
【问题描述】:

我正在使用 wicked_pdf 从 html 生成 pdf。现在,我想删除首页/封面页的上边距。

这是从我的控制器中截取的代码:

render :pdf => @project.name,
:javascript_delay => 1000,
:disable_external_links => false,
:encoding => 'UTF-8',
:cover => "#{root_url}/projects/#{params[:id]}/pdf_cover",     
:footer => {:html => { :template => 'projects/report_footer.pdf.haml' }, :spacing => 5},
:show_as_html                   => params[:debug].present?, 
:disable_smart_shrinking        => false,
:print_media_type => true,
:no_background => false,
:margin => { :top => 10, :bottom => 18 , :left => 0 , :right => 0}

正如您在上面看到的,在控制器操作中,我将上边距设置为 10 。所以我希望上边距、页眉和页脚不显示在第一页上,而是显示在文档的其余页面上。 附件区

【问题讨论】:

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


    【解决方案1】:

    我看到你也在the Wicked PDF issue tracker上发布了这个

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

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

    Here's an example of how you might do that:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 2018-05-04
      • 1970-01-01
      相关资源
      最近更新 更多