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.

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 mailers add some like this:

  def order_invoiced(order)
    recipients my@mail.com
    from       my@server.com
    subject    Your Invoice

    attachment "application/pdf" do |a|
      a.body = render_pdf(:invoice, :invoice => order.invoice, :other => order.invoice.other)
    end

    part "text/plain" do |a|
      a.body = render_message("order_invoiced", :order => order, :body_template => @body_template)
    end
  end

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, body)

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

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