Deploying a Rails 7 Application with PDFTK on Heroku

Coding Posted on May 02, 2023
In this blog post, we will walk through the process of deploying a Ruby on Rails 7 application that uses PDFTK to Heroku. PDFTK is a powerful PDF manipulation tool that we used in our previous blog post to fill out PDF forms programmatically. We use PDFTK in the previous post to help us fill PDF forms from within our Rails application. Deploying a Rails application to Heroku is relatively straightforward, but there are a few extra steps required to ensure that PDFTK is installed and working correctly on the Heroku platform.

Step 1: Preparing the Rails Application for Heroku


Before deploying the application to Heroku, we need to make some changes to our application to make it compatible with the Heroku platform.

  1. Add the rails_12factor gem to your Gemfile in the production group:

group :production do
  gem 'rails_12factor'
end

2. Run bundle install to install the new gem:

$ bundle install

3. Configure the production database in config/database.yml to use the DATABASE_URL environment variable:

production:
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  url: <%= ENV['DATABASE_URL'] %>

Step 2: Installing the Heroku CLI


To deploy your application to Heroku, you need to have the Heroku CLI installed on your local machine. You can download the installer for your operating system from the official Heroku CLI website.

Step 3: Creating a Heroku Application


Once the Heroku CLI is installed, log in to your Heroku account using the command line:

$ heroku login

Next, create a new Heroku application:

$ heroku create your-app-name

Make sure to replace your-app-name with a unique name for your application.

Step 4: Adding the Heroku Buildpack for PDFTK


To install PDFTK on Heroku, we need to use a custom buildpack. A buildpack is a script that sets up the environment for your application on the Heroku platform. Add the following buildpacks to your application:

$ heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-apt
$ heroku buildpacks:add --index 2 heroku/ruby

The first buildpack allows us to install packages using the apt package manager, while the second buildpack installs the necessary Ruby and Rails dependencies.

Next, create an Aptfile in the root of your Rails application with the following content:

pdftk

This file tells the heroku-buildpack-apt buildpack to install the pdftk package.

Step 5: Deploying the Application


Now, we are ready to deploy our application to Heroku. Commit your changes and push the code to the Heroku remote:

$ git add .
$ git commit -m "Prepare for Heroku deployment"
$ git push heroku main

After the deployment is complete, run the following command to set up the database on Heroku:

$ heroku run rails db:migrate

Step 6: Testing the Application


Open your application in a browser by running the following command:

$ heroku open

Fill out the PDF form in your Rails application, and verify that the filled PDF is generated and displayed correctly. If everything is working as expected, congratulations! You have successfully deployed your Rails 7 application with PDFTK to Heroku.

Step 7: Monitoring and Scaling Your Application


Now that your application is deployed, you may want to monitor its performance and handle any issues that arise. Heroku provides a range of tools to monitor your application's performance and logs, which can be accessed through the Heroku Dashboard or the Heroku CLI.

To view your application logs, run the following command:

$ heroku logs --tail

You can also scale your application by adjusting the number of dynos (Heroku's term for application instances) and the resources allocated to them. To learn more about scaling your application on Heroku, refer to the official Heroku documentation.

Conclusion


In this blog post, we have demonstrated how to deploy a Ruby on Rails 7 application that uses PDFTK to Heroku. We walked through the process of preparing the application for deployment, installing the Heroku CLI, creating a Heroku application, adding the required buildpacks, and deploying the application. By following these steps, you can successfully deploy your Rails application with PDFTK to the Heroku platform, making it accessible to users worldwide.

Leave a comment:

Comments (0)