# Edit existing PDF as template

**URL:** <https://www.ruby-forum.com/t/edit-existing-pdf-as-template/184814>\
**Category:** Rails\
**Created:** [March 9, 2010, 1:29am UTC](https://www.ruby-forum.com/t/edit-existing-pdf-as-template/184814 "2010-03-09T01:29:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 9, 2010, 1:29am UTC](https://www.ruby-forum.com/t/edit-existing-pdf-as-template/184814/1 "2010-03-09T01:29:10Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 9, 2010, 1:58am UTC](https://www.ruby-forum.com/t/edit-existing-pdf-as-template/184814/2 "2010-03-09T01:58:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 9, 2010, 2:12am UTC](https://www.ruby-forum.com/t/edit-existing-pdf-as-template/184814/3 "2010-03-09T02:12:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 9, 2010, 4:18am UTC](https://www.ruby-forum.com/t/edit-existing-pdf-as-template/184814/4 "2010-03-09T04:18:33Z")

</div>

Thank you so much!

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

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 9, 2010, 2:31am UTC](https://www.ruby-forum.com/t/edit-existing-pdf-as-template/184814/5 "2010-03-09T02:31:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 9, 2010, 3:06pm UTC](https://www.ruby-forum.com/t/edit-existing-pdf-as-template/184814/6 "2010-03-09T15:06:45Z")

</div>

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.
