Using ruby ​​to modify a PDF file

I'm going to start a project. I would like to be able to edit PDF file (forms) using rails.

What is the best (and easiest) solution for this?

So exactly what I need is what would allow me to modify an existing PDF file (form) and allow the user to print it. Perhaps their name and more.

Help will be appreciated :)

+3
source share
2 answers

you can try pdf-stamper. I am working on filling out the pdf form fields now.here is my solution.

@template = PDF::Stamper.new(@form.pdf.current_path)

fields = @template.extract_fields

@form.form_fields.each do |ff|
  if fields.has_key?(ff.pdf_field)
    val = form_data.get_value(ff)
    render_field(@template, ff, val)
  else
    BindFile.logger.warn "Key '#{ff.pdf_field}' Not Found".center(100, "-")
  end
end

def render_field(templet, form_field, val)
   if val.present?
      case form_field.pdf_field_type
      when "CheckBox"
        if val.present?
          templet.checkbox form_field.pdf_field
        end
      when "RadioButton"
        templet.send("radio_button", form_field.pdf_field, "Yes")
      when "", nil
        templet.send "text", form_field.pdf_field, val
      else
        templet.send(form_field.pdf_field_type.to_s.downcase, form_field.pdf_field, val)
      end
    end
end

here I raised pdf-stamper by adding the method "extract_fields". and use a database record to manage pdf documents. hope useful for you.

0
source

If you are looking for a ruby ​​library for this, you might consider prawn

-2
source

Source: https://habr.com/ru/post/1786807/


All Articles