How to edit or write on existing PDF with Ruby?

前端 未结 7 1108
眼角桃花
眼角桃花 2020-12-01 10:35

I have a couple of PDF template files with complex content and several blank regions/areas in them. I need to be able to write text in those blank regions and save the resul

相关标签:
7条回答
  • 2020-12-01 10:45

    You don't need to use a combination of gems you can use just one gem!

    Working with PDF's is really challenging in Ruby/Rails (so I have found out!)

    This is the way I was able to add text dynamically to a PDF in rails.

    add this gem to your gem file gem combine_pdf

    and then you can use code like this:

    # get the record from the database to add dynamically to the pdf
    user = User.last
    
    # get the existing pdf
    pdf = CombinePDF.load "#{Rails.root}/public/pdf/existing_pdf.pdf"
    
    # create a textbox and add it to the existing pdf on page 2
    pdf.pages[1].textbox "#{user.first_name} #{user.last_name}", height: 20, width: 70, y: 596, x: 72
    
    # output the new pdf which now contains your dynamic data
    pdf.save "#{Rails.root}/public/pdf/output#{Time.now.to_s}.pdf"
    

    You can find details of the textbox method here: https://www.rubydoc.info/gems/combine_pdf/0.2.5/CombinePDF/Page_Methods#textbox-instance_method

    I spent days on this working through a number of different gems: prawn wicked_pdf pdfkit fillable_pdf

    But this was by far the most smooth solution for me as of 2019.

    I hope this saves someone a lot of time so they don't have to go through all the trial and error I had to with PDF's!!

    0 讨论(0)
  • 2020-12-01 10:47

    PDFLib seems to do the thing you want and has ruby bindings.

    0 讨论(0)
  • 2020-12-01 10:52

    Since Prawn has removed the template feature (it was full of bugs) the easiest way I've found is the following:

    1. Use Prawn to generate a PDF with ONLY the dynamic parts you want to add.
    2. Use PDF::Toolkit (which wraps PDFtk) to combine the Prawn PDF with the original.

    Rough Example:

    require 'prawn'
    require 'pdf/toolkit'
    
    template_filename = 'some/dir/Awesome-Graphics.pdf'
    prawn_filename = 'temp.pdf'
    output_filename = 'output.pdf'
    
    Prawn::Document.generate(prawn_filename) do
      # Generate whatever you want here.
      text_box "This is some new text!", :at => [100, 300]
    end
    
    PDF::Toolkit.pdftk(prawn_filename, "background", template_filename, "output", output_filename)
    
    0 讨论(0)
  • 2020-12-01 10:58

    The best I can think of is Rails-latex, it doesn't allow you to edit existing PDF files but it would allow you to set up template *.tex.erb which you may dynamically modify and compile them into PDF format (along with dvi and a number of others).

    0 讨论(0)
  • 2020-12-01 11:02

    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

    0 讨论(0)
  • 2020-12-01 11:02

    According to my research, Prawn is one of the free and best gems I found. The template functionality isn't working in later version. The latest version I could find to work with templates is 1.0.0.rc2 - March 1, 2013. Couldn't find any later version which works with templates. So be mindful if you are using later versions than this. Check below thread for more info.

    https://groups.google.com/forum/#!searchin/prawn-ruby/prawn$20templates/prawn-ruby/RYGPImNcR0I/7mxtnrEDHeQJ

    PDFtk is another capable tool for PDF manipulation and to work with templates. But it mentions following points,

    • This library is free for personal use, but requires a license if used in production
    • This is a non-ruby command line tool

    For more information please refer the below link http://adamalbrecht.com/2014/01/31/pre-filling-pdf-form-templates-in-ruby-on-rails-with-pdftk/

    0 讨论(0)
提交回复
热议问题