如何使用Rails填充可填写PDF表单?基于表单/数据库数据的实现方案咨询
Absolutely possible! You don’t have to rely on HTML-to-PDF tools like Prawn or Wicked PDF to populate existing fillable PDFs—there are dedicated Ruby libraries that let you directly map your database/form data to PDF form fields. Here are two reliable approaches I’ve used in production:
Option 1: pdf-forms (Based on PDFtk)
This gem wraps PDFtk, a popular command-line tool for manipulating PDFs. It’s straightforward and works great if you can install PDFtk on your system.
Step 1: Set Up Dependencies
First, install PDFtk:
- On Ubuntu/Debian:
sudo apt-get install pdftk-java - On macOS:
brew install pdftk-java - On Windows: Grab the official installer (I haven’t tested this personally, but it’s widely supported)
Then install the gem:
gem install pdf-forms
Step 2: Fill the Form with Data
Here’s a quick example using data from your database or form submission:
require 'pdf_forms' # Initialize the PDFtk wrapper (adjust the path to pdftk if your system uses a different location) pdftk = PdfForms.new('/usr/bin/pdftk') # Paths to your fillable template and the output filled PDF template_path = './templates/client_intake_form.pdf' output_path = './filled_forms/client_jane_smith.pdf' # Data hash—keys MUST match the exact field names in your PDF template form_data = { 'client_full_name' => 'Jane Smith', 'client_email' => 'jane.smith@example.com', 'client_phone' => '555-7890', 'service_requested' => 'Web Development', 'project_budget' => '$15,000' } # Fill the form and save the result pdftk.fill_form(template_path, output_path, form_data)
Pro Tip: Get Exact PDF Field Names
To avoid mismatches between your data keys and the PDF’s field names, run this command to list all fields in your template:
pdftk ./templates/client_intake_form.pdf dump_data_fields
Or fetch fields programmatically with the gem:
fields = pdftk.get_fields(template_path) fields.each { |field| puts "Field name: #{field[:name]}" }
Option 2: hexapdf (Pure Ruby, No External Dependencies)
If you run into issues installing PDFtk (like on newer macOS versions), HexaPDF is a pure-Ruby PDF processing library that handles form filling without external tools. It’s a great alternative for cross-platform projects.
Step 1: Install the Gem
gem install hexapdf
Step 2: Fill the Form
Here’s how to implement it:
require 'hexapdf' template_path = './templates/client_intake_form.pdf' output_path = './filled_forms/client_jane_smith.pdf' form_data = { 'client_full_name' => 'Jane Smith', 'client_email' => 'jane.smith@example.com', 'service_requested' => 'Web Development' } HexaPDF::Document.open(template_path) do |doc| acro_form = doc.acro_form form_data.each do |field_name, value| # Locate the field by name and set its value field = acro_form.field(field_name) next unless field # Skip if the field doesn't exist in the template field.value = value # Optional: Mark fields as read-only to prevent post-filling edits field.read_only = true end # Save the completed PDF doc.write(output_path) end
Key Notes
- Make sure your PDF template is fillable (not flattened). If you only have a static PDF, convert it to a fillable form first using tools like Adobe Acrobat or LibreOffice.
- For checkboxes or radio buttons, the required value might be specific (e.g., 'Yes' for a checked box). Test with your template to find the correct trigger values.
- If you want to flatten the filled PDF (so users can’t edit it later), use
pdftk.fill_form(..., flatten: true)withpdf-forms, or adddoc.acro_form.flattenbefore writing the file with HexaPDF.
内容的提问来源于stack exchange,提问作者RIzu




