2017-06-08 157 views
2

我使用wicked_pdf从html生成pdf。现在,我想删除第一页/封面上的顶部边距。Wicked PDF:如何从COVER页面删除顶部边距?

这是从我的控制器剪断代码:

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} 

enter image description here

正如你可以在上面看到,在我的上边距设置为10的控制器动作。因此,我希望顶部边距,页眉和页脚不会显示在第一页上,而是显示在其他文档页面上。 附件区

回答

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