Edit existing PDF as template

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?

Thanks!

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.

Craig


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

Thanks for response.

Can you give me an example for this? Can prawn edit existing pdf file?

Craig W. 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.

Craig


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

Thank you so much!

I tried the code and got
uninitialized constant HomeController::Fdf

On Tue, 2010-03-09 at 02:12 +0100, John Wu wrote:

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.

On Tue, 2010-03-09 at 04:18 +0100, John Wu wrote:

Thank you so much!

I tried the code and got
uninitialized constant HomeController::Fdf


I think you might want to put the Fdf class into its own module and not
try to just the use the code that I provided to you. In fact, that is
just one way of doing it, I also played with PDF-stamper (google for it,
it’s around) and perhaps the way to do it now is with prawn but I was
doing this long before prawn was around.

Craig


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