Skip to content

Instantly share code, notes, and snippets.

@kristianmandrup
Last active May 31, 2019 09:29
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save kristianmandrup/5280569 to your computer and use it in GitHub Desktop.
Save kristianmandrup/5280569 to your computer and use it in GitHub Desktop.
Jekyll PDF Converter using PDFKit
require 'pdfkit'
require 'nokogiri'
require 'haml'
# config/initializers/pdfkit.rb
PDFKit.configure do |config|
# Note: Often required for Windows OS configuration
# config.wkhtmltopdf = '/path/to/wkhtmltopdf'
# Basic configuration/customization
# config.default_options = {
# :page_size => 'Legal',
# :print_media_type => true
# }
# config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.
end
module Jekyll
class PdfConverter < Converter
safe true
priority :low
def matches(ext)
ext =~ /\.pdf$/i
end
def output_ext(ext)
".pdf"
end
def convert(content)
styled_kit.to_pdf
end
protected
# override to assemble the specific part of the page to print to the PDF document!
#
def page
content
end
def styled_kit
css_files.compact.each do |css_ref|
kit.stylesheets << css_ref
end
end
def css_files
[]
end
def kit
@kit ||= PDFKit.new(page, :page_size => 'Letter')
end
def page_filter
@page_filter ||= PageFilter.new content
end
class PageFilter
attr_reader :content
def initialize content
@content = content
end
def doc
@doc ||= Nokogiri::HTML(content)
end
def layout
haml %Q{
!!!
html
body
= page_content
}
end
def page
layout.render page_content: page_content
end
# filter out the part of the page to use for PDF document
# See Nokogiri docs
# Tutorials:
# - http://ruby.bastardsbook.com/chapters/html-parsing/
# - https://blog.engineyard.com/2010/getting-started-with-nokogiri
def page_content
doc.css('section#page-content')
end
end
end
end
@chellem
Copy link

chellem commented Nov 1, 2014

Hello, can you explain how to use it?

am new to jekyll plugins

@ChrisChinchilla
Copy link

Seconded. I was going to post some ideas, but got stumped by requirements. I think something is missing as this is just a gist.

If you can add some pointers on how to get this started I will be happy to adapt this to a full plugin as i'd love to get wktohtml working with jekyll.

@damienh
Copy link

damienh commented Jun 15, 2015

Id be keen to use this as well.

@pOH7
Copy link

pOH7 commented Jun 17, 2015

How to use?

@ChrisChinchilla
Copy link

I'm going to try and get this into a proper plugin as I prefer wktopdf options to latex / pandoc plugins.

Bear with me :)

Follow along here - https://github.com/ChrisChinchilla/wkhtmltopdf-for-Jekyll

Copy link

ghost commented Nov 11, 2015

does someone has a tutorial/document on how to use this in jekyll.
Thanks.

Copy link

ghost commented Nov 11, 2015

I have added the file in _plugins directory on jeykll.
Updated the configuration as per my setup. (Installed wkhtmltopdf as required).

PDFKit.configure do |config|
  # Note: Often required for Windows OS configuration
  config.wkhtmltopdf = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'

  # Basic configuration/customization
  config.default_options = {
    :page_size => 'Legal',
    :print_media_type => true
  }
  #config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.
end

Here is how my file looks like.

require 'pdfkit'
require 'nokogiri'
require 'haml'

# config/initializers/pdfkit.rb

PDFKit.configure do |config|
  # Note: Often required for Windows OS configuration
  config.wkhtmltopdf = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'

  # Basic configuration/customization
  config.default_options = {
    :page_size => 'Legal',
    :print_media_type => true
  }
  #config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.
end

module Jekyll
  class PdfConverter < Converter
    safe true

    priority :low

    def matches(ext)
      ext =~ /\.pdf$/i
    end 

    def output_ext(ext)
      ".pdf"
    end

    def convert(content)
      styled_kit.to_pdf
    end

    protected

    # override to assemble the specific part of the page to print to the PDF document!
    #
    def page
      content
    end

    def styled_kit
      css_files.compact.each do |css_ref|
        kit.stylesheets << css_ref
      end
    end

    def css_files
      []
    end

    def kit
      @kit ||= PDFKit.new(page, :page_size => 'Letter')
    end

    def page_filter
      @page_filter ||= PageFilter.new content
    end

    class PageFilter
      attr_reader :content

      def initialize content
        @content = content
      end

      def doc
        @doc ||= Nokogiri::HTML(content)        
      end

      def layout
        haml %Q{
!!!
  html
    body
      = page_content
}
      end

      def page
        layout.render page_content: page_content        
      end

      # filter out the part of the page to use for PDF document
      # See Nokogiri docs
      # Tutorials: 
      # - http://ruby.bastardsbook.com/chapters/html-parsing/
      # - https://blog.engineyard.com/2010/getting-started-with-nokogiri
      def page_content
        doc.css('section#page-content')
      end
    end
  end
end

I am not sure where the pdf files are generated.
Any help is appreciated. Thanks.

@gamort
Copy link

gamort commented Jul 5, 2016

To run with begin.rb

Place begin.rb in the directory _plugins
Run bin/jekyll

Done.

I also had to add 2 lines to the file: Gemfile in the main directory:
gem "pdfkit"
gem "haml"

Lastly I removed Gemfile.lock just because I thought it might be needed and ran "bundler install" again
It installed some more stuff and then when I ran "bin/jekyll" it loaded fine.

My guess is that there should be a button/menu item somewhere that will generate the PDF. Right now it is generated the docs so I don't know yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment