简体   繁体   中英

Ruby on rails wkhtmltopdf css style sheets not rendering on production (linux) server

In local development environment(windows) pdf with Css styles generated successfully. but in hosting linux server environment pdf generated without applying css styles.Below is my wkhtmltopdf (WickedPdf) config

WickedPdf.config = {
#:wkhtmltopdf => "#{RAILS_ROOT}/pdfbin/wkhtmltopdf-amd64",
:exe_path => "/home/software/.gems/bin/wkhtmltopdf",
:layout => "layouts/pdf.html.erb",
:margin => {    :top=> 40,
                :bottom => 20,
                :left=> 30,
                :right => 30},
:header => {:html => { :template=> 'layouts/pdf_header.html.erb'}},
:footer => {:html => { :template=> 'layouts/pdf_footer.html.erb'}}
#:exe_path => '/usr/bin/wkhtmltopdf'

}

for additional info :

this is my dir structure i am on linux rails hosting app\\views\\layouts , inside layouts i am having pdf.html.erb , pdf_footer.html.erb , pdf_header.html.erb The above stuff works perfectly on my local windows development environment , but in production pdf generated without styles.so guys please help me to produce pdf with CSS styles

app/views/layouts/pdf.html.erb

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html dir="<%= (rtl?) ? 'rtl' : 'ltr' %>">
  <head>
    <% @direction = (rtl?) ? 'rtl/' : '' %>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <%= stylesheet_link_tag([@direction+'application', @direction+'_styles/ui.all'])%>
    <%= stylesheet_link_tag(*get_stylesheets) %>
    <%= stylesheet_link_tag  @direction+"_layouts/pdf" %>
    <link rel="stylesheet" type="text/css" href="<%="#{RAILS_ROOT}/public/stylesheets/#{@direction}_layouts/pdf.css" %>" media="all" />
    <link rel="stylesheet" type="text/css" href="<%="#{RAILS_ROOT}/public/stylesheets/#{get_stylesheets}.css"%>" media="all" />
    <link rel="stylesheet" type="text/css" href="<%= "#{RAILS_ROOT}/public/stylesheets/#{@direction}_styles/ui.all.css"%>" media="all" />


  </head>
  <body>

    <%= yield %>

  </body>
</html>

pdf.html.erb contains all styling information for rendering pdf , in hosting environment these styles are not fetching by wkhtmltopdf . so guys please help me

I had a similar issue with PDFKit when I was deploying my app on Heroku. Now, it's working fine (I've included the binary to the project).

In my layout:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Page title</title>
  <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <%= csrf_meta_tags %>
  <style type="text/css" media="all">
    <%= Rails.application.assets['my_css'].to_s.gsub(/\/assets/,"#{root_url}assets") %>
  </style>
</head>
<body>
  <%= yield %>
</body>
</html>

In the initializers:

# config/initializers/pdfkit.rb
PDFKit.configure do |config|
  if Rails.env.staging? || Rails.env.production?
    config.wkhtmltopdf = Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s
  else
    config.wkhtmltopdf = '/Users/my_user_name/.rvm/gems/ruby-1.9.2-p0@my_project/bin/wkhtmltopdf'
  end
  config.default_options = {
    :page_size => 'A4',
    :margin_top => '0in',
    :margin_right => '0in',
    :margin_bottom => '0in',
    :margin_left => '0in',
    :orientation => 'Portrait'
  }
  # Use only if your external hostname is unavailable on the server.
  config.root_url = "http://localhost:3000" unless Rails.env.staging? || Rails.env.production?
end

Maybe that could help you... or someone else...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM