As a developer, I often find myself working with PDF files in web applications. Recently, I came across a project that required me to fill out PDF forms programmatically using a Ruby on Rails application. In this blog post, I'll share with you the process I followed to accomplish this task using Rails 7, Hotwire, Turbo, the pdf-forms gem, and the pdftk tool. We'll create a full Rails application with Bootstrap, PostgreSQL, and Simple Form to demonstrate the process step by step.
Step 1: Setting Up a New Rails 7 Application with Hotwire and Turbo
First, let's create a new Rails 7 application with Hotwire, Turbo, and PostgreSQL as the database:
$ rails new pdf_form_filler --minimal --database=postgresql $ cd pdf_form_filler
Next, open your Gemfile and add the hotwire-rails gem:
gem 'hotwire-rails'
Run bundle install to install the gem:
$ bundle install
Now, configure your config/database.yml file with the correct PostgreSQL credentials and create the database:
$ rails db:create
Step 2: Installing Bootstrap
To install Bootstrap, add the bootstrap and jquery-rails gems to your Gemfile and run bundle install:
gem 'bootstrap', '~> 5.3.0.alpha1' gem 'jquery-rails'
$ bundle install
Next, import Bootstrap styles in your app/assets/stylesheets/application.scss:
@import "bootstrap";
And add Bootstrap and jQuery to your app/assets/javascripts/application.js:
//= require jquery3 //= require popper //= require bootstrap
Step 3: Installing Simple Form, PDF-Forms, and PDFTK
In this step, we will install Simple Form, the PDF-Forms gem, and PDFTK, which are required to create and fill out PDF forms in our Rails application.
- First, add the simple_form and pdf-forms gems to your Gemfile:ruby
gem 'simple_form' gem 'pdf-forms'
2. Run bundle install to install the new gems:
$ bundle install
3. Now, run the Simple Form generator to configure it with Bootstrap:
$ rails generate simple_form:install --bootstrap
4. Install PDFTK on your system. The installation process depends on your operating system. Here are some examples for popular operating systems:
- For macOS using Homebrew:
$ brew install pdftk-java
- For Ubuntu:
$ sudo apt-get install pdftk
- For Windows, download the installer from the official PDFTK website and follow the installation instructions.
With Simple Form, the PDF-Forms gem, and PDFTK installed, we can now proceed to create a service to fill out PDF forms and build the necessary views and controllers to complete the form-filling functionality.
Step 4: Creating a Sample PDF Form
For this tutorial, I'll assume you have a PDF form that you want to fill out. If you don't have one, you can create a simple form using a tool like Adobe Acrobat or use an existing PDF form available online.
Save the PDF form in your Rails application, e.g., under the public/forms folder.
Step 5: Filling Out the PDF Form
Now, let's create a service to fill out the PDF form using the pdf-forms gem. In this example, I'll create a service called PdfFormFiller:
# app/services/pdf_form_filler.rb require "pdf_forms" class PdfFormFiller def initialize(input_pdf, output_pdf, form_data) @input_pdf = input_pdf @output_pdf = output_pdf @form_data = form_data @pdftk = PdfForms.new(pdftk_path) end def fill_form @pdftk.fill_form(@input_pdf, @output_pdf, @form_data, flatten: true) end private def pdftk_path # Adjust the path according to your system "/usr/local/bin/pdftk" end end
Replace pdftk_path with the path to your installed pdftk executable.
Step 6: Using the PdfFormFiller Service
Now, we can use the PdfFormFiller service to fill out our PDF form.
Step 7: Creating a FormFiller Controller and View
Create a new controller to handle the PDF form filling:
$ rails generate controller FormFiller new create
In your app/controllers/form_filler_controller.rb, update the create action:
def create input_pdf = Rails.root.join("public/forms/my_form.pdf") output_pdf = Rails.root.join("public/forms/filled_form.pdf") form_data = { "name" => params[:pdf_form_data][:name], "email" => params[:pdf_form_data][:email] } PdfFormFiller.new(input_pdf, output_pdf, form_data).fill_form send_file output_pdf, type: 'application/pdf', disposition: 'inline' end
Now, create a view in app/views/form_filler/new.html.erb with a form to collect the user's name and email using Simple Form:
<h1>Fill Out PDF Form</h1> <%= simple_form_for :pdf_form_data, url: form_filler_create_path, method: :post do |f| %> <div class="form-group"> <%= f.input :name, label: "Name", required: true, input_html: { class: "form-control" } %> </div> <div class="form-group"> <%= f.input :email, label: "Email", required: true, input_html: { class: "form-control" } %> </div> <%= f.button :submit, "Fill Form", class: "btn btn-primary" %> <% end %>
Finally, add a route to config/routes.rb:
post "form_filler/create", to: "form_filler#create", as: "form_filler_create"
Now, when you visit the form_filler/new URL and submit the form, the PDF form will be filled out with the provided name and email, and the filled form will be displayed in the browser.
Conclusion
In this blog post, I showed you how to fill out PDF forms programmatically using a Ruby on Rails 7 application with Hotwire, Turbo, Bootstrap, PostgreSQL, and Simple Form. We used the pdf-forms gem and the pdftk tool to achieve this functionality. This can be a useful feature for web applications that need to generate custom PDF documents from user input. By leveraging the power of Rails 7, Hotwire, and Turbo, you can create an efficient and interactive web application that handles PDF form filling with ease.