Wicked PDF - If I add any html to the header or the footer of my PDF, it turns the PDF blank white

104 Views Asked by At

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: Screenshot 2023-09-19 at 2 50 21 PM

but if I remove all html it generates the PDF just fine (pdf_footer.html.erb):

This is the html footer...

PDF generated fine: Screenshot 2023-09-19 at 2 48 10 PM

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. Screenshot 2023-09-19 at 2 41 40 PM

Using simpler way from above and saving the header and footer with pdf.erb: Header and footer show up just fine:

Screenshot 2023-09-19 at 2 56 52 PM Screenshot 2023-09-19 at 2 57 03 PM

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)):
1

There are 1 best solutions below

0
Jeremy Gradisher On

I just figured this out - I am going to close the issue.

I am keeping the header and footer files with the format pdf.erb and I just needed to add formats: [:pdf] within both the header and the footer parts within the controller.

header: {
        html: {
          template: "blank/pdf_header",
          formats: [:pdf]
        }
      },
      footer: {
          html: {
            template: "blank/pdf_footer",
            formats: [:pdf]
          }
        }

For reference: Screenshot 2023-09-19 at 3 34 43 PM