rlivsey (owner)

Revisions

gist: 140359 Download_button fork
public
Description:
Simplified code to show what the issue was
Public Clone URL: git://gist.github.com/140359.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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