0.0
A long-lived project that still receives updates
This gem is built to convert HTML document into PDFs. There are lots of tools out there that do this. This one uses Selenium+Chromedriver to render the HTML and generate the PDF.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

HTML2PDFChrome

This gem provides a tool for converting HTML documents to PDFs.

There are a lof of tools out there that do this. This one uses Chromedriver to render the HTML and convert it to a PDF. It uses Selenium to fire up Chromedriver.

Installation

This gem can be installed with rubygems:

gem "html2pdf_chrome"

You will, of course, also need a working Google Chrome and Chromedriver installation. Chromedriver can be downloaded from Google or it can be installed via homebrew.

Usage

Once everything is installed using it is pretty easy:

require "html2pdf_chrome"
my_html = "<p>This PDF just has this paragraph</p>"
pdf_data = HTML2PDFChrome.convert_html_to_pdf(my_html)
File.write("my_pdf.pdf", pdf_data)

Custom Headers and Footers

You can also specify custom header and footer templates for your PDFs:

require "html2pdf_chrome"

my_html = "<h1>My Document</h1><p>Content goes here</p>"

header = "<div style='font-size: 10px; text-align: center; width: 100%;'>My Custom Header</div>"

footer = <<~HTML
  <div style="font-size: 10px; text-align: center; width: 100%;">
    Page <span class="pageNumber"></span> of <span class="totalPages"></span>
  </div>
HTML

pdf_data = HTML2PDFChrome.convert_html_to_pdf(
  my_html,
  header_template: header,
  footer_template: footer
)

File.write("my_pdf.pdf", pdf_data)

The header and footer templates support HTML and CSS styling. You can use special Chrome variables for pagination:

  • <span class="pageNumber"></span> - Current page number
  • <span class="totalPages"></span> - Total number of pages
  • <span class="date"></span> - Current date
  • <span class="title"></span> - Document title
  • <span class="url"></span> - Document URL