On Tue, 2010-03-09 at 02:12 +0100, John Wu wrote:
> Thanks for response.
> 
> Can you give me an example for this? Can prawn edit existing pdf file?
> 
> 
> Craig White wrote:
> > On Tue, 2010-03-09 at 01:29 +0100, John Wu wrote:
> >> Hi,
> >> 
> >> I am new to rails. I need to edit an existing pdf file and fill in some
> >> data and send it to browser, how to implement this? Is there an example
> >> online I can look at it?
> > ----
> > I do this using pdftk and using rails to output the fdf data file and
> > then using pdftk to merge the data to the PDF template.
> > 
> > You might be able to use prawn to do that.
----
don't know about prawn - you might want to check out the project page.

as for an example, not sure how helpful this will actually be but...

  def guardian_fdf_form
    @client = Client.find(params[:id])
    @fdfobj = Fdf.new
    @fdfobj.url = "http://SOME_WEB_SERVER/GuardianAgreement.pdf";
    myfdf = @fdfobj.create_header
    myfdf += @fdfobj.add_data({"guardian",@client.clwholename})
    myfdf += @fdfobj.specify_pdf(@fdfobj.url)
    myfdf += @fdfobj.create_footer
    send_data(myfdf, {:type => "application/vnd.fdf", :stream =>
false, :filename => "Guardian_Agreement_out.fdf"})
  end

and I have an FDF model (fdf.rb) that looks like this...(watch for line
breaks)

# Provides basic for generating FDF type data files (related to PDF)
class Fdf < ActiveRecord::BaseWithoutTable

  attr_accessor :data, :url

  def add_data(fdfdata)
    return format_data(fdfdata)
  end

  def hide_data(field)
    return self.data = self.data.gsub(/(<<\/T\(#{field}\)\/V\()(\w*
\)>>\s)/, '')
  end

  def create_header
    return "%FDF-1.2\x0d%\xe2\xe3\xcf\xd3\x0d\x0a 1 0 obj\x0d<< \x0d/FDF
<< /Fields [ "
  end

  def create_footer
    return "] \x0d >> \x0d >> \x0dendobj\x0d trailer\x0d<<\x0d/Root 1 0
R \x0d\x0d>>\x0d %%EOF\x0d\x0a"
  end

  def specify_pdf(pdf_url)
    return "] \x0d /F (" << pdf_url << ") /ID [ <" <<
Digest::SHA1.hexdigest(Time.now.to_i.to_s) << ">"
  end

  def format_data(fdfdata)
    data = ''
    fdfdata.each { |key,value|
    if value.class == Hash
      value.each { |sub_key,sub_value|
        data += '<</T(' + key + '_' + sub_key + ')'
        data += '/V(' + escape_data(sub_value) + ')>> '
        }
    else
      data += '<</T(' + key + ')/V(' + escape_data(value) + ')>> '
    end
    }
    return data
  end

  def escape_data(fdfdata)
    fdfdata = fdfdata.to_s.strip.gsub(/[\(]/, '\\(')
    fdfdata = fdfdata.to_s.gsub(/[\)]/, '\\)')
    return fdfdata
  end 

end

Craig


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to