1. Products
  2.   PDF
  3.   Ruby
  4.   PDFKit
 
  

Open Source Ruby Library to Generate PDF from HTML

Ruby PDF API that enables developers to render HTML/CSS and output it as a PDF with high quality, Generate PDF document from a provided file or URL.

What is PDFKit Library?

Different types of businesses create a variety of documents on daily basis. Most of these documents contain very confidential or private data that needs to be protected. Portable document format (PDF) is a very useful and convenient way to keep your personal information safe while sharing it on the web. PDFKit is a very useful Ruby library that enables software developers to generate PDF files using plain HTML/CSS code.

The library is very easy to use and uses wkhtmltopdf on the backend which uses WebKit to render HTML/CSS and output it as a PDF with high quality. The library is very flexible and allows users to generate PDF documents from a provided URL with ease. The Library has included several important features for rendering PDF documents such as generating PDF from HTML code, modifying PDFs, adding styles to PDF, inserting header and footers, table page break, and many more.

The library has provided users lots of options to control how it creates PDFs inside their apps. You can configure those options globally as well as set them per page. You can use specially-named meta-tags on the page to control how a page's HTML is rendered into PDF.

.

Previous Next

Getting Started with PDFKit

To install the PDFKit on your system, please run the following command, 

Install PDFKit with ruby gems

gem install pdfkit

Generate PDF from HTML via Ruby

The open source Ruby library PDFKit enables software developers to generate PDF documents from HTML code inside their own applications. You can use CSS to apply different kinds of styles to your code and then can generate the PDF documents from it. You can also easily set the size of the page by default A4 size is used.  You can also different kinds of options through Meta tags. It is also possible to pass cookies to PDFKit to scrape a website using hash.

How to Create PDF File via PDFKit?


  # Generate PDF via Ruby
  
  require 'pdfkit'
  html = render_to_string(:layout => 'layouts/test_layout' , :action => print_form.html.erb")
  kit = PDFKit.new(html)
  send_data(kit.to_pdf, :filename => "Form.pdf", :type => 'application/pdf')

Header and Footer Addition to PDF

The PDFKit library has provided support for adding header and footer inside their PDF documents with just a couple of lines of code. You can easily set the margins of the PDF header and footer and can apply margins to it. You can dynamically generate header and footer with ease. Please remember that the library only accepts a file or URL. It will not accept raw text. Broken links must be avoided otherwise it will not produce the desired results.

Load and Parse PDF Data via Ruby API

The open source PDF library PDFKit has included support for loading and parsing PDF files inside Ruby applications. As recommended, loading data from PDF files is easy. Developers can also parse PDF files from memory. It is very effective to load data from memory especially for data received via the internet or from a different library.  Same as parsing rendering can also be performed either to the memory or to a file.

How to Parse Title Page of a PDF Guide via Ruby API?


  # Render PDF Title Page via Ruby

  const renderTitlePage = doc => {
  const title = 'PDFKit Guide';
  const author = 'By Devon Govett';
  const version = `Version ${require('../package.json').version}`;

  doc.font('fonts/AlegreyaSans-Light.ttf', 60);
  doc.y = doc.page.height / 2 - doc.currentLineHeight();
  doc.text(title, { align: 'center' });
  const w = doc.widthOfString(title);
  doc.h1Outline = doc.outline.addItem(title);

  doc.fontSize(20);
  doc.y -= 10;
  doc.text(author, {
    align: 'center',
    indent: w - doc.widthOfString(author)
  });

  doc.font(styles.para.font, 10);
  doc.text(version, {
    align: 'center',
    indent: w - doc.widthOfString(version)
  });

  doc.addPage();
  };

Add Text to Existing PDF Pages via Ruby

The open source PDFKit library gives software developers the power to add text or images to an existing PDF file with just a couple of lines of Ruby code. It has included support for numerous options to customize the display of the output text. The library includes support for Text styling, Text measurements, bulleted list, 14 standard fonts & more. It also supports automatic line wrapping; which means the text will be automatically wrapped within the page margins and placed in the document flow below any previous text.

How to Apply Rich Text Styles to PDF Content via PDFKit?


  # Rich Tex Support in PDF
  
  doc.fillColor('green')
  .text(lorem.slice(0, 500), {
  width: 465,
  continued: true
  }).fillColor('red')
  .text(lorem.slice(500));
 English