Permalink
Browse files

Merge branch 'master' of https://github.com/nathanstitt/wicked_pdf in…

…to nathanstitt-master
  • Loading branch information...
2 parents c8b8afa + 3f77d85 commit fd36c4f52f26d29a51e95dc2f6363f505b90d1b9 @unixmonkey unixmonkey committed Apr 17, 2011
Showing with 18 additions and 8 deletions.
  1. +14 −5 lib/pdf_helper.rb
  2. +1 −1 lib/wicked_pdf.rb
  3. +3 −2 lib/wicked_pdf_tempfile.rb
View
@@ -1,9 +1,10 @@
module PdfHelper
require 'wicked_pdf'
-
+ require 'wicked_pdf_tempfile'
def self.included(base)
base.class_eval do
alias_method_chain :render, :wicked_pdf
+ after_filter :clean_temp_files
end
end
@@ -18,6 +19,13 @@ def render_with_wicked_pdf(options = nil, *args, &block)
end
private
+ def clean_temp_files
+ if defined?(@hf_tempfiles)
+ @hf_tempfiles.each do | tf |
+ tf.close!
+ end
+ end
+ end
def make_pdf(options = {})
html_string = render_to_string(:template => options[:template], :layout => options[:layout])
w = WickedPdf.new(options[:wkhtmltopdf])
@@ -45,12 +53,13 @@ def make_and_send_pdf(pdf_name, options = {})
def prerender_header_and_footer(options)
[:header, :footer].each do |hf|
if options[hf] && options[hf][:html] && options[hf][:html][:template]
- WickedPdfTempfile.open("wicked_pdf.html") do |f|
- f << render_to_string(:template => options[hf][:html][:template],
+ @hf_tempfiles = [] if ! defined?(@hf_tempfiles)
+ @hf_tempfiles.push( tf=WickedPdfTempfile.new("wicked_#{hf}_pdf.html") )
+ tf.write render_to_string(:template => options[hf][:html][:template],
:layout => options[:layout])
+ tf.flush
options[hf][:html].delete(:template)
- options[hf][:html][:url] = "file://#{f.path}"
- end
+ options[hf][:html][:url] = "file://#{tf.path}"
end
end
View
@@ -7,6 +7,7 @@
require 'active_support/core_ext/class/attribute_accessors'
require 'wicked_pdf_railtie'
+require 'wicked_pdf_tempfile'
class WickedPdf
@@config = {}
@@ -23,7 +24,6 @@ def initialize(wkhtmltopdf_binary_path = nil)
def pdf_from_string(string, options={})
command_for_stdin_stdout = "#{@exe_path} #{parse_options(options)} -q - - " # -q for no errors on stdout
- p "*"*15 + command_for_stdin_stdout + "*"*15 unless defined?(Rails) and Rails.env != 'development'
pdf, err = begin
Open3.popen3(command_for_stdin_stdout) do |stdin, stdout, stderr|
stdin.write(string)
@@ -2,8 +2,9 @@
class WickedPdfTempfile < Tempfile
# Replaces Tempfile's +make_tmpname+ with one that honors file extensions.
- def make_tmpname(basename, n)
- extension = File.extname(basename)
+ def make_tmpname(basename, n )
+ n||=0
+ extension = File.extname(basename)
sprintf("%s_%d_%d%s", File.basename(basename, extension), $$, n, extension)
end
end

0 comments on commit fd36c4f

Please sign in to comment.