开发者

Using ruby to modify a PDF

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

What is the best (and most simple) solution for this?

开发者_开发知识库

So exactly what I need is something that would allow me to modify an existing PDF file (a form) and allow the user to print it. Probably their name and stuff.

Help will be appreciated :)


you can try pdf-stamper. I am working on fill 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 enhanced pdf-stamper add a method "extract_fields". and use database record to manage pdf documents. hope helpful for you.


If you are looking for a ruby library for this then you can think of prawn

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜