How to edit or write on existing PDF with Ruby?

前端 未结 7 1140
眼角桃花
眼角桃花 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: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)
    

提交回复
热议问题