pdf libraries

pdf libraries

Ruby on rails

Ruby on rails

How to Easily Generate PDF from HTML with HexaPDF

Marcelo Abreu, founder of pdforge

Marcelo | Founder

Marcelo | Founder

Oct 28, 2024

Oct 28, 2024

Introducing HexaPDF: A Lightweight, Open Source PDF Library for Ruby

HexaPDF is an open-source, lightweight PDF library designed to streamline the process of creating and managing PDF documents within Ruby applications. Built in pure Ruby, it offers a highly idiomatic interface that Ruby developers can quickly adapt to, making it an ideal choice for those looking to integrate PDF functionalities directly into their codebase with minimal dependencies.

You can check out their full documentation here.

Challenges in Converting HTML to PDF for Developers

Developers often face significant challenges when attempting to convert HTML content into PDF. HTML is designed for dynamic, web-based layouts, while PDF is a fixed-format layout. This means that creating a PDF from HTML often leads to issues with font alignment, precise layout control, and responsiveness.

PDF tools must bridge this gap effectively by providing robust formatting controls while preserving document fidelity across different environments. HexaPDF offers a more intuitive approach by allowing developers to build PDFs directly with a structured API, sidestepping the common pitfalls of HTML-to-PDF conversions.

Comparison Between HexaPDF and Other Ruby PDF Libraries

Number of downloads from bestgems of HexaPDF

HexaPDF differentiates itself from other Ruby PDF tools by offering a pure Ruby solution with comprehensive functionality. Here's how it compares with popular alternatives:

  • Prawn: Prawn is a well-regarded Ruby PDF library, but it lacks support for HTML content, relying instead on manually structured PDF elements.

  • WickedPDF: This library wraps around wkhtmltopdf, enabling HTML-to-PDF conversions in Rails. However, its reliance on wkhtmltopdf can lead to compatibility issues and requires an external dependency.

  • PDFKit: Another wrapper around wkhtmltopdf, PDFKit provides a straightforward HTML-to-PDF solution but offers limited customization and control over PDF layout.

  • Puppeteer-Ruby: While Puppeteer-Ruby generates PDFs from web pages with high fidelity, it comes with a Node.js dependency and can be heavyweight for smaller Ruby-based applications.

In contrast, HexaPDF provides developers with direct control over the PDF content layout, text formatting, and other low-level PDF functionalities—all within a pure Ruby environment.

Guide to generate pdf from html using Ruby on rails HexaPDF
Guide to generate pdf from html using Ruby on rails HexaPDF

Setting Up HexaPDF in Ruby on Rails

Getting started with HexaPDF is straightforward and can be integrated seamlessly within Ruby on Rails applications.

Installing HexaPDF Gem

First, ensure that Ruby (version 2.6 or above, ideally 3.0+) is installed on your system. Then, add HexaPDF to your Rails project by including it in the Gemfile:

# Gemfile
gem 'hexapdf', '~> 1.0.0'

Install the gem by running:

To confirm that HexaPDF is correctly installed, execute the following command:

hexapdf version

Converting HTML to PDF Using HexaPDF

While HexaPDF does not support direct HTML-to-PDF conversion, it provides a powerful Canvas API to help you design PDFs with precision. This approach requires structuring the document layout programmatically, enabling you to create complex documents with fine-tuned control over every element.

Creating a Complete Invoice Layout Using the Canvas API

To demonstrate HexaPDF’s layout capabilities, let’s build a simple invoice PDF layout.

require 'hexapdf'

doc = HexaPDF::Document.new
page = doc.pages.add
canvas = page.canvas
canvas.font('Helvetica', size: 12).fill_color(0, 0, 0)
canvas.text("Invoice", at: [100, 750])
canvas.text("Invoice #: 12345", at: [100, 730])
canvas.text("Date: #{Time.now.strftime("%Y-%m-%d")}", at: [100, 710])
canvas.text("Client: Jane Doe", at: [100, 690])
canvas.text("Amount Due: $500.00", at: [100, 670])
doc.write("invoice.pdf"

In this example, we create a PDF that includes basic text and layout elements. The Canvas API enables custom text placement, color control, and font selection, allowing for a high level of customization. This approach can be scaled to more intricate layouts and formatting styles as needed.

Main Features of HexaPDF

HexaPDF includes a rich array of features, ideal for both simple and complex document creation:

  • Pure Ruby Implementation: HexaPDF is written entirely in Ruby, with minimal dependencies, ensuring easy integration and consistent behavior across environments.

  • Document Layout Engine: Supports automatic content layout with capabilities such as text wrapping, page breaks, multi-column layouts, and more. The layout system is designed to flow text around images and other elements.

  • Comprehensive Canvas API: Direct access to low-level PDF operations enables developers to perform tasks such as drawing paths, shapes, and curves, as well as embedding images and working with TrueType fonts.

  • Form Handling and Annotations: Supports PDF forms (AcroForm) and annotations, enabling interactive elements within documents.

  • Document Security: Provides encryption, including AES256 for PDF 2.0, and supports digital signatures, making it suitable for handling sensitive information.

  • File Optimization: Offers built-in tools for file size reduction without compromising quality, ideal for web applications where file size is a concern.

  • Command-line Tool: The hexapdf CLI offers utility commands for tasks such as merging, encrypting, or optimizing PDFs.

These features make HexaPDF a well-rounded PDF solution that balances low-level control with high-level convenience.

How to Use a PDF API to Automate PDF Creation at Scale

For SaaS platforms, automating PDF generation at scale might require offloading the heavy lifting to a PDF API.

It's also an option to integrate with third-party APIs like pdforge you can handle high-volume PDF generation, complex formatting, and post-processing, all from a single backend call.

Here’s an example of how to integrate pdforge in Rails to convert HTML content into a PDF via an API call:

require 'net/http'
require 'json'
require 'uri'

class PdfApiService
  def self.generate_pdf(html_content)
    uri = URI("https://api.pdforge.com/v1/pdf/sync")
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(uri.path, {
      'Content-Type' => 'application/json',
      'Authorization' => "Bearer your-api-key"
    })
    request.body = {
      templateId: 'your-template',
      data: { html: html_content }
    }.to_json
    response = http.request(request)
    response.body if response.is_a?(Net::HTTPSuccess)
  end
end

This code sends a POST request to the pdforge API, receives the generated PDF, and saves it locally.

Conclusion

HexaPDF is a powerful option for developers who need a lightweight yet feature-rich PDF solution in Ruby. Its design focuses on layout precision, security, and document flexibility, making it ideal for applications requiring advanced PDF handling.

For straightforward HTML-to-PDF conversion with minimal configuration, tools like WickedPDF may still be effective.

However, when handling high PDF volumes or distributing load across services, using an external PDF API such as pdforge provides a robust and scalable solution. Choose the right tool based on your project’s requirements, workload, and infrastructure to achieve the best results.

Generating pdfs at scale can be quite complicated!

Generating pdfs at scale can be quite complicated!

We take care of all of this, so you focus on what trully matters on your Product!

We take care of all of this, so you focus on what trully matters on your Product!

Try for free

7-day free trial

Table of contents

Title