This module convert a string/controller to a pdf through Pd4ml java library (included in this plugin)-

PD4ML is a powerful PDF generating tool that uses HTML and CSS (Cascading Style Sheets) as page layout and content definition format. Written in 100% pure Java, it allows users to easily add PDF generation functionality to end products.

Remember to install jdk ex: yum install java-1.6.0-openjdk-devel.i386

For generate a pdf you can simply do

    script/generate pdf invoice

then edit your template /app/views/pdf/invoice.html.haml

Then in any of your controllers add some like this:

  def generate_pdf_invoice
    render_pdf :invoice, 'invoice_file.pdf'
  end

Possible options are:

  • landescape, default it‘s false
  • send_data, default it‘s true

Lipsiadmin include a trial fully functional evaluation version, but if you want buy it, go here: pd4ml.com/buy.htm and then put your licensed jar in a directory in your project then simply calling this:

  Lipsiadmin::Utils::PdfBuilder.jars_path = "here/is/my/licensed/pd4ml"
  Lipsiadmin::Utils::PdfBuilder.view_path = "keep/template/in/other/path"

you can use your version without any problem.

By default Lipsiadmin will look into your "vendor/pd4ml" and if:

  • pd4ml.jar
  • ss_css2.jar

are present will use it

Methods
Included Modules
Public Instance methods
render_pdf(template, filename=nil, options={})

# Convert a stream to pdf, the template must be located in app/view/pdf/yourtemplate.pdf.erb

    # File lib/controller/pdf_builder.rb, line 48
48:       def render_pdf(template, filename=nil, options={})
49: 
50:         # path to the pd4ml jarfile
51:         jars_path = Lipsiadmin::Utils::PdfBuilder.jars_path
52:         # path to our templates
53:         view_path = Lipsiadmin::Utils::PdfBuilder.view_path
54:         
55:         options[:landescape]   =  options[:landescape] ? "LANDESCAPE" : "PORTRAIT"
56:         options[:send_data]  ||= !filename.blank?
57:         
58:         # try to find erb extension
59:         ext = File.exist?("#{view_path}/#{template}.html.erb") ? "erb" : "haml"
60:         
61:         # encode the template
62:         input = encode_entities(render(:template => "#{view_path}/#{template}.html.#{ext}", :layout => false))
63:         
64:         # search for stylesheet links and make their paths absolute.
65:         input.gsub!('<link href="/javascripts', '<link href="' + view_path + '/../../../public/javascripts')
66:         input.gsub!('<link href="/stylesheets', '<link href="' + view_path + '/../../../public/stylesheets')   
67: 
68:         # search for images src, append full-path.
69:         input.gsub!('src="/', 'src="' + RAILS_ROOT + '/public/')
70:         input.gsub!('url(','url('+RAILS_ROOT+'/public')
71: 
72:         # write our temp file
73:         t = Tempfile.new("pd4ml.html", "#{Rails.root}/tmp")
74:         t.binmode
75:         t.write(input)
76:         t.flush
77:         
78:         # build the command
79:         class_path = "#{jars_path}/pd4ml.jar:.:#{jars_path}"
80:         class_path = "\"#{jars_path}/pd4ml.jar\";\"#{jars_path}\"" if RUBY_PLATFORM =~ /mswin/
81:         cmd = "cd #{Lipsiadmin::Utils::PdfBuilder.pd4ruby_path} && java -Xmx512m -Djava.awt.headless=true -cp #{class_path} Pd4Ruby --file \"#{t.path}\" --width 950 --orientation #{options[:landescape]} 2>&1"
82:         
83:         # grep the output
84:         output = IO.popen(cmd) { |s| s.read }
85: 
86:         # raise error if process returned false (ie: a java error)
87:         raise PdfError, "An unknonwn error occurred while generating pdf" if $?.exitstatus == 127
88:         
89:         # return raw pdf binary-stream
90:         if options[:send_data]
91:           pdf_options  = { :filename => filename, :type => 'application/pdf' }
92:           pdf_options[:disposition] = "inline" if Rails.env == "development"
93:           send_data(output, pdf_options) 
94:         else
95:           erase_results
96:           output
97:         end
98:       end