If I add any HTML to the header or the footer of my Wicked_PDF generated PDF, it turns the PDF blank white. It maybe has something to do with render_to_string().
I am generating a PDF with the following with my Ruby on Rails 7 application:
My controller:
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
So if I use my footer (pdf_footer.html.erb) like this, it all goes blank white:
<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>
Showing blank PDF:
but if I remove all html it generates the PDF just fine (pdf_footer.html.erb):
This is the html footer...
PDF generated fine:
I believe it has something to do with render to string - or the difference between naming the file html.erb or pdf.erb. - Here is a separate example where I can get it to generate the PDF in a simpler way. Notice in this I have to use header and footer files that are formatted with pdf.erb as opposed to html.erb - if I don't it throws a missing template error.
My controller for simpler generation
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
That header and footer were saved as pdf.erb as opposed to html.erb - if I don't it throws a missing template error.
Using simpler way from above and saving the header and footer with pdf.erb: Header and footer show up just fine:
Can someone tell me what I am doing wrong?
Thank you!
Ruby on Rails 7
- wicked_pdf gem version (
wicked_pdf (2.7.0)): - wkhtmltopdf version (
wkhtmltopdf 0.12.6 (with patched qt)):
I just figured this out - I am going to close the issue.
I am keeping the header and footer files with the
format pdf.erband I just needed to addformats: [:pdf]within both the header and the footer parts within the controller.For reference: