ruby-on-railswicked-pdf

wicked pdf using application layout, not intended pdf layout


gem includes and installs

gem 'wkhtmltopdf-binary', '~> 0.12.6.6'
gem 'wicked_pdf', '~> 2.7'
gem 'mobility', '~> 1.2', '>= 1.2.9'

An initializer defines (based on prior experience):

WickedPdf.config = {
  exe_path: '/Users/main/.rbenv/shims/wkhtmltopdf'

assets.rb should precompile as follows

Rails.application.config.assets.precompile += %w( initialise.css foundation.css zapp.css )

A layout pdf.html.erb was created with a couple of options to test for font rendering:

<!DOCTYPE html>
<html>
<head>
<title></title>
    <meta charset='utf-8' />
    <%= wicked_pdf_stylesheet_link_tag "initialise" %>
    <%= wicked_pdf_stylesheet_link_tag "foundation" %>
    <%= wicked_pdf_stylesheet_link_tag "zapp" %>
    <%# stylesheet_link_tag wicked_pdf_asset_base64("initialise") %>
    <%# stylesheet_link_tag wicked_pdf_asset_base64("foundation") %>
    <%# stylesheet_link_tag wicked_pdf_asset_base64("zapp") %>
</head>
<body>
    <%= yield %>
</body>
</html>

The controller action defines respond_to blocks

    respond_to do |format|
      format.html
      format.pdf do
        render pdf: 
          "#{@individual.name_last}_#{@individual.name_first}_t_up",
           layout: 'pdf', 
           encoding: 'utf8'
     end
    end

the link rendering <%= link_to fa_icon("file-pdf-o", class: 'fa-lg'), t_up_individual_path(@individual, format: :pdf), target: '_pdf' %> renders
href="/individuals/94/t_up.pdf?locale=en"

However, the net result is styles are not applied. Double-check by removing the pdf.html.erb layout from its folder and the error for lack of a layout is returned. SO the proper layout is being used. it seems the absolute paths are amiss. Also attempted, with the same results, an absolute path to an external server: wicked_pdf_stylesheet_link_tag "[fully_verifiable_url]"

What is wrong/missing with the stylesheet definitions?

incidentally, how can precompilation be asserted?


Solution

  • As per official documentation you should name the file pdf.pdf.erb. Change the html extension to pdf.

    class ThingsController < ApplicationController
      def show
        respond_to do |format|
          format.html
          format.pdf do
            render pdf:    'file_name',
                   ...
                   layout: 'pdf', # for a pdf.pdf.erb file
                   ...
          end
        end
      end
    end
    

    Read more: https://github.com/mileszs/wicked_pdf#advanced-usage-with-all-available-options