Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS and wicked_pdf not working

I'm not sure what i'm missing, but my css doesn't seem to be working along side wicked_pdf. I have an image linked in my file, which works, but the styles are missing.

Gemfile

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'

Controller

format.pdf do
  render pdf: "day_report", 
  template: 'day/day_report',
  page_size: 'A4'
end

application.html.erb

<head>
 <title>Page Title</title>
 <%= wicked_pdf_stylesheet_link_tag "styles" %>
 <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
 <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>

day_report.pdf.erb

<%= wicked_pdf_image_tag( 'logo.jpg', height: '100', width: '100') %>

<div class="page-header">
    Day Report
</div>

styles.css

.page-header {
  padding: 40px 0;
  background-color: red;
  text-color: #fff;
}
like image 657
DollarChills Avatar asked Aug 17 '18 06:08

DollarChills


2 Answers

Create a different layout for pdf as below. e.g pdf.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Page title</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 
  'reload' %>
  <%= wicked_pdf_stylesheet_link_tag "styles" %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'%>
</head>
<body>
  <%= yield %>
</body>
</html>

Change controller action as:

format.pdf do
  layout:'pdf.html',
  render pdf: "day_report", 
  template: 'day/day_report',
  page_size: 'A4',
  encoding:"UTF-8"
end

Change styles.css to styles.scss and add it assets.rb.

Rails.application.config.assets.precompile += %w(styles.scss)

Hope this will help.

Cheers

like image 85
Abid Iqbal Avatar answered Nov 19 '22 06:11

Abid Iqbal


Can you please rename styles.css to styles.css.scss and add this file to precomiple list Rails.application.config.assets.precompile += %w(styles.css) and try.

like image 22
Prosenjit Saha Avatar answered Nov 19 '22 05:11

Prosenjit Saha