1

Using chartkick with wicked-pdf.

app/layout/pdf.html.erb

<!doctype html>
<html>
<head>
 <meta charset='utf-8' />
 <%= wicked_pdf_stylesheet_link_tag 'application', media: 'all' %>
 <%= wicked_pdf_javascript_include_tag 'application', 'data-turbolinks-track' => true %>
 <%= javascript_include_tag "http://www.google.com/jsapi"%>
 <%= wicked_pdf_javascript_include_tag 'chartkick'%>
 </head>
 <body onload='number_pages'>
 <div id="header">
  <h2>Report</h2>
</div>
<div id="content">
  <%= yield %>
</div>

But it gives an error ON THIS LINE

<%= wicked_pdf_stylesheet_link_tag 'application', media: 'all' %>

Error

  undefined method `start_with?' for nil:NilClass

How I remove this? Error

ActionView::Template::Error (undefined method `start_with?' for nil:NilClass):
2: <html>
3:   <head>
4:     <meta charset='utf-8' />
5:     <%= wicked_pdf_stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
6:     <%= wicked_pdf_javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7:     <%= javascript_include_tag "http://www.google.com/jsapi"%>
8:     <%= wicked_pdf_javascript_include_tag 'chartkick'%>
 app/views/layouts/pdf.html.erb:5:in `_app_views_layouts_pdf_html_erb___3130978149028204091_70042742447040'
 app/controllers/campaigns_controller.rb:104:in `block (2 levels) in survey_reporting'
 app/controllers/campaigns_controller.rb:101:in `survey_reporting'
Lucas Moulin
  • 2,450
  • 3
  • 32
  • 41
Haseeb Ahmad
  • 7,914
  • 12
  • 55
  • 133
  • 1
    Can you post more of the stacktrace. – j-dexx Feb 23 '16 at 15:25
  • 1
    That error is telling you that you are calling start_with? on something which you expect to have a value but is actually equal to `nil`, possibly because it's an instance variable which you think you have defined but you actually haven't. The stack trace will tell you specifically where this is happening. Look for the first line which is in your project folder rather than a gem. – Max Williams Feb 23 '16 at 15:28
  • @j-dexx which file u need? – Haseeb Ahmad Feb 23 '16 at 15:28
  • I update my question with some stacktrace – Haseeb Ahmad Feb 23 '16 at 15:33
  • @HaseebAhmad What is in the `survey_reporting` method in the campaigns controller? – j-dexx Feb 23 '16 at 15:38
  • 1
    @HaseebAhmad - If you're seeing this error in your browser, please click "full stacktrace" and post what you'll get. – BroiSatse Feb 23 '16 at 15:41
  • 1
    @HaseebAhmad - You didn't happen to override `WickedPdfHelper::Assets::ASSET_URL_REGEX` constant, did you? https://github.com/mileszs/wicked_pdf/blob/master/lib/wicked_pdf/wicked_pdf_helper/assets.rb#L21 Can't think of scenario that could return nil. – BroiSatse Feb 23 '16 at 15:56
  • 1
    There is an official issue open for this problem: https://github.com/mileszs/wicked_pdf/issues/470 – infused Feb 23 '16 at 17:37
  • @BroiSatse what mean you didn't happen to override WickedPdfHelper::Assets::ASSET_URL_REGEX. I can't override anything like that – Haseeb Ahmad Feb 23 '16 at 21:20
  • 1
    @HaseebAhmad - It's ruby, you can override everything here. :) That was rather a long shot though, so ignore it. – BroiSatse Feb 23 '16 at 21:39

5 Answers5

5

Problem is resolved!

replace

<%= wicked_pdf_stylesheet_link_tag "application" %>

with <%= stylesheet_link_tag wicked_pdf_asset_base64("pdf") %>

4

I solved this problem by creating a specif pdf.css.scss file and imported my custom style (estilo.css) and font-awesome style.

/*# pdf.css.scss 

@import "font-awesome";
@import "estilo";

I was unable to import the bootstrap.css file there because the glyphicon's src-url method. It's broke wicked_pdf.

As a workaround, I simply copied all bootstrap.css files in my pdf.css.scss file and erased all about glyphicon since I'm not using it at all in my generated PDF.



Update

Instead of coping and pasting all bootstrap.css source in your pdf.css.scss file, do this:

  1. Follow these steps here;

  2. Add a line for you bootstrap.css file location in your config/application.rb. It should look like this:

    /*# config/application.rb
    
    config.assets.paths << "#{Rails.root}/app/assets/stylesheets/font-awesome-4.2.0/css/" 
    config.assets.paths << "#{Rails.root}/app/assets/stylesheets/bootstrap/" 
    config.assets.paths << "#{Rails.root}/vendor/assets/fonts"
    
  3. Finally add a import for bootstrap in your pdf.css.scss file. It should look like this:

    /*# pdf.css.scss
    
    @import "bootstrap";
    @import "font-awesome";
    @import "estilo";
    

By doing all this, wicked_pdf reading src-urls method will work!

Community
  • 1
  • 1
aranhaqg
  • 89
  • 1
  • 9
1

Adding @import "bootstrap-sprockets" before @import "bootstrap":

@import "bootstrap-sprockets"
@import "bootstrap"

check this link

elmerfreddy
  • 141
  • 1
  • 5
1

To handle this issue, create a file app/assets/stylesheet/bootstrap.css Then copy-paste the bootstrap css from any CDN. Now include this file in the layout you are using. ex: pdf.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>PDF</title>
  <%= stylesheet_link_tag wicked_pdf_asset_base64("bootstrap") %>
</head>
<body>
<div class='container'>
  <%= yield %>
</div>
</body>
</html>

and the script at the controller side is like:

  def job_invoice_view

    respond_to do |format|
      format.html { render :layout => 'tukaweb' }
      format.pdf do
        render pdf: "Your_filename",
               template: "users/job_invoice_view.html.erb",
               layout: 'pdf',
               viewport_size: '1280x1000'
      end
    end
  end
vidur punj
  • 5,019
  • 4
  • 46
  • 65
1

I had this same issue. My problem was that my application.css.scss was including the lightbox2 npm package. The problem with this package is that assets paths seem to be broken in it which causes the arrows not to display. I guess the wicked_pdf_stylesheet_link_tag doesn't ignore the fact that it can't find the asset like the normal stylesheet_link_tag does and causes the error. Creating a new css file specifically for the PDF and removing the lightbox2 reference fixed the error for us.