mileszs/wicked_pdf — Issue #60: jquery does not execute because wicked_pdf_javascript_include_tag("jquery-1.4.2") does not add ".js" to the path
Author: cfrankct
Created: 2011-11-04 18:21:52+00:00
State: open

I'm only raising this because I just spent a couple of hours tracking it down, hoping to save someone else some time:

Here's my initial code:
```-layout.html.erb
<%= params[:debug].present? ? javascript_include_tag("jquery-1.4.2") : wicked_pdf_javascript_include_tag("jquery-1.4.2") -%>
<%= params[:debug].present? ? javascript_include_tag("protovis") : wicked_pdf_javascript_include_tag("protovis") -%>
```

I could not figure out why jquery was not executing in the .pdf generation, but worked fine when I ran it with `show_as_html`, and furthermore protovis was working just fine.

Ultimately it was because I was using a javascript file with a version number (jquery-1.4.2.js), but only had the `wicked_pdf_javascript_include_tag("jquery-1.4.2")` in my header. For javascript files without version numbers (e.g., `protovis.js`) this works fine. However, when the underlying rails helpers see `jquery-1.4.2` they assume `.2` is the filename extension and don't add the `.js`. Trivial, but hard to track, and made worse because the `javascript_include_tag` part gets it right.

Two suggestions:
a) Edit the README for wicked to suggest using full filenames with the helper functions
b) Add a 'puts(err)' in `pdf_from_string` in wicked_pdf.rb. Right now it fails silently

```
def pdf_from_string(string, options={})
command = "#{@exe_path} #{parse_options(options)} - - " # -q for no errors on stdout
p "*"*15 + command + "*"*15 unless defined?(Rails) and Rails.env != 'development'
pdf, err = Open3.popen3(command) do |stdin, stdout, stderr|
stdin.binmode
stdout.binmode
stderr.binmode
stdin.write(string)
stdin.close
[stdout.read, stderr.read]
end
puts(err)
raise "PDF could not be generated!" if pdf and pdf.rstrip.length == 0
pdf
rescue Exception => e
raise "Failed to execute:\n#{command}\nError: #{e}"
end
```