Created
July 3, 2009 22:38
-
-
Save rlivsey/140359 to your computer and use it in GitHub Desktop.
Simplified code to show what the issue was
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# broked | |
def render_pdf(html, name) | |
in_file = "#{name}.xhtml" | |
out_file = "#{name}.pdf" | |
# if the file name contains accents, they are changed to '??' | |
File.open(in_file, 'w') do |f| | |
f.write html | |
end | |
# call java to build the PDF | |
# this fails because the file written doesn't have the same name as we send java | |
system("java generate_pdf #{in_file} #{out_file}") | |
send_file out_file | |
end | |
# fixed | |
def render_pdf(html, name) | |
tmp_name = Digest::SHA1.hexdigest(name) | |
in_file = "#{tmp_name}.xhtml" | |
out_file = "#{tmp_name}.pdf" | |
# this now saves the name with the hash instead (no accents guaranteed) | |
File.open(in_file, 'w') do |f| | |
f.write html | |
end | |
# so this now works | |
system("java generate_pdf #{in_file} #{out_file}") | |
# and we set the filename so the end user sees the right thing | |
send_file(out_file, { | |
:filename => "#{name}.pdf" | |
}) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment