wicked_pdf
wicked_pdf copied to clipboard
don't show header and footer in pdf
Hi. I used wicked_pdf to implement pdf. But I cannot see header and footer.pls check my code.
format.pdf do
render :pdf => "location.pdf",
:page_size => "Letter",
:template => 'admin/works/location.pdf.haml',
:layout => 'pdf_no_header.pdf',
footer: { html: { template:'layouts/page_footer.html.haml', # use :template OR :url
:spacing => 10,
:margin => {left:0,right:0}
}
},
default_header: false,
margin: {top: 10, bottom: 10, left: 10, right: 10}
end
//--- pdf_no_header.pdf
!!!
%html
%head
%title= content_for?(:title) ? yield(:title) : "PhilipGuston"
%meta{'http-equiv' => 'Content-Type', :content => 'text/html', :charset => "utf-8"}/
= wicked_pdf_stylesheet_link_tag "pdf"
= wicked_pdf_javascript_include_tag "pdf"
:javascript
function getDate() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
today = mm + '/' + dd + '/' + yyyy;
var current_date = document.getElementById("currentdate");
current_date.innerHTML = today;
number_pages();
}
function number_pages() {
var vars={};
var x=document.location.search.substring(1).split('&');
for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = decodeURIComponent(z[1]);}
var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
for(var i in x) {
var y = document.getElementsByClassName(x[i]);
for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
}
}
%body{:onload => "getDate()"}
#content.clearfix
= yield
= yield :scripts
//-- page_footer.html.haml--
#pdf-footer
= "footer sample"
Is there anything to fix?
Does it work if you rename your .html.haml
files to .pdf.haml
or just .haml
? Rails might be ignoring the loading of .html.*
format templates.
Hi @unixmonkey The header and footer are being shown correctly but not when we generate the pdf using sidekiq. Can you please help me with this?
@unixmonkey Hi, I'm having the same problem. My pdf is only rendering the footer. header content is "header here!" with an image footer content is "footer here!" with an image
I'm running rails 6.0.2.1 wicked_pdf 2.0.1 wkhtmltopdf_binary 0.12.5.4
Initalizer:
WickedPdf.config = {
encoding: "utf8",
dpi: "200",
disable_start_shrinking: true,
layout: "layouts/pdf.html.erb",
margin: { top: 20, bottom: 20 },
header: { html: {template: "shared/_pdf_header.haml"}},
footer: { html: {template: "shared/_pdf_footer.haml"}}
}
controller render
respond_to do |format|
format.html
format.pdf do
@letterhead = current_account.letterheads.find(1)
render pdf: "preview"
end
end
Header file
# has "header here!" text with image in content
!= @letterhead.header_content
Footer file
# has "footer here!" text with image in content
!= @letterhead.footer_content
output screenshot: Notice nothing is showing in header
Further investigation:
I checked out wicked_pdf in the logs. the temp html file with --header-html
, i opened up with correct html rendered.
[wicked_pdf]: ["~/.rbenv/versions/2.7.0/bin/wkhtmltopdf", "--encoding", "utf8", "--dpi", "200", "--margin-top", "20", "--margin-bottom", "20", "--header-html", "file:////var/folders/sv/qlfr7wg91l77qc3x85z4gfb00000gn/T/wicked_header_pdf20200317-30262-1e1fqw5.html", "--footer-html", "file:////var/folders/sv/qlfr7wg91l77qc3x85z4gfb00000gn/T/wicked_footer_pdf20200317-30262-xqse2d.html", "file:////var/folders/sv/qlfr7wg91l77qc3x85z4gfb00000gn/T/wicked_pdf20200317-30262-5bwg9w.html", "/var/folders/sv/qlfr7wg91l77qc3x85z4gfb00000gn/T/wicked_pdf_generated_file20200317-30262-19y19tx.pdf"]
@CaptainAwesomeDi Two things to try:
-
Make sure your headers and footers are complete HTML documents including doctype, it may not work otherwise.
-
Try setting your top margin and padding to zero and/or styling your header with css so the background color isn't white. Often a missing header turns out to be the top margin painting on top of you header.
@unixmonkey thanks for the suggestion. I have implement both of your suggestion, but following behaviour appeared:
layout file
<html style="margin-top: 0; padding-top: 0;">
<head>
<%= render 'shared/pdf_styles.html.erb' %>
</head>
<body>
<%= yield %>
</body>
</html>
header haml file Notice: text inside p tag doesn't even render
!!! 5
%html
%head
%body{style:"background-color: red; margin-top: -2em ; padding: 0;"}
%p this is some header text
!= @letterhead.header_content
Now I wonder if the text is there, but is being painted off-page. In your header CSS, try setting a border of a different color. Is the border visible at the top, or is it still being cut off. Play with the margins and padding to see if you can get them visible.
@unixmonkey Thank you for your help. As i dig into it more. Found the problem could be with the binary. there are a lot people saying header not working.
If people are encountering same people as me can look here! https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2561