How to edit or write on existing PDF with Ruby?
you have to definitely check out Prawn gem, by which you can generate any custom pdf files. You can actually use prawn to write in text into existing pdfs by treating the existing PDF as a template for your new Prawn document.
For example:
filename = "#{Prawn::DATADIR}/pdfs/multipage_template.pdf"
Prawn::Document.generate("full_template.pdf", :template => filename) do
text "THis content is written on the first page of the template", :align => :center
end
This will write text onto the first page of the old pdf.
See more here:
http://prawn.majesticseacreature.com/manual.pdf
Edit existing pdf file metadata with ruby (Apply Password protection)
template
was removed in version 0.13.0
because it was too buggy :
Support for templates was dropped in Prawn 0.13.0, disabled by default in 0.14.0, and extracted in 0.15.0.
This gem includes the extracted templates code, which is completely unsupported, but provides the old functionality that was in Prawn 0.12.0 for years.
source : https://github.com/prawnpdf/prawn-templates
As he said, you can try to add the library to your current Prawn installation.
Otherwise you can use pdftk
with Open3
module (http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/) :
require 'open3'
file_name = 'hello_world_1.pdf' # input
file_name_output = 'hello_world_2.pdf' # output
usr = 'foo'
pwd = 'bar'
pdftek = './pdftk.exe' # tested on windows
Open3.popen3("#{pdftek} #{file_name} output #{file_name_output} owner_pw #{pwd} user_pw #{usr}") do |stdin,stdout,stderr|
# ...
end
There is also a wrapper for ruby but I haven't test it yet : https://github.com/tcocca/active_pdftk
Add text to existing pdf using ruby
This solution worked well for me...
Prawn::Document.generate("output.pdf", :template => "/path/to/template.pdf") do
text "This is a text in a copied pdf.", :align => :center
end
How to replace a word in an existing PDF using Ruby Prawn?
1) I'm a star wars fan and all, but damn.
2) Your "simple task" is not even remotely simple. It's not that hard to COVER the existing text... but REPLACING IT is another mater entirely. And forget reflowing existing text. All-but-impossible.
If covering it will suffice, then you need only draw a rectangle filled with the background color over your first string, then draw your second string on top of it. I'm not familiar with Prawn PDF, so I'll leave the implementation details as an Exercise For the Reader.
Related Topics
Ruby Differences Between += and << to Concatenate a String
Why Would One Use the Unary Operator on a Property in Ruby? I.E &:First
Best Way to Add Comments in Erb
Rails 3. Creating a Production Database
403 Forbidden on Rails App W/ Nginx, Passenger
How to Dynamically Call a Math Operator in Ruby
Installing Vim with Ruby Support (+Ruby)
How to Get Class Instances in Ruby
Ruby: How to Install a Specific Version of a Ruby Gem
How to Replace a Hash Key with Another Key
How to Calculate the Day of the Week of a Date in Ruby
Rails 2 to Rails 3:Using Link_To Instead of Link_To_Remote (Including Remote and Update)