html2pdf

A CLI tool for generating PDF from HTML by using Lua script.

Stars
6

Html2pdf

A CLI tool for generating PDF from HTML by using Lua script.

Html2pdf uses wkhtmltopdf to generate PDF. Actually, Html2pdf is designed as a wrapper of the wkhtmltopdf command. But Html2pdf bundles the wkhtmltopdf binary, so you don't need to install wkhtmltopdf. Html2pdf works well in a stand-alone.

Table of Contents

Installation

Html2pdf is provided as a single binary. You can download it and drop it in your $PATH.

Download latest version

Getting Started

Create Lua script hello.lua as the following:

local html2pdf = require "html2pdf"

local hello = html2pdf.pdf "hello.pdf"
hello.options = {
    page_size = "A4",
}
hello.pages = {
    input_content = "hello world!"
}

Run it:

$ html2pdf hello.lua
==> Starting html2pdf...
==> Loaded 1 pdf config.
==> Evaluating hello.pdf
    output_file: hello.pdf
==> Complete!

You will get hello.pdf file.

Configuration

Configuration in the Html2pdf is written in Lua.

Generate PDF

local html2pdf = require "html2pdf"

local example = html2pdf.pdf "example.pdf"
example.pages = {
    input_content = "hello world!"
}

html2pdf is a lua module of the Html2pdf and html2pdf.pdf function defines a config of generating PDF file.

Generate PDF from URL

By using input key, you can use a URL instead of html content.

local html2pdf = require "html2pdf"

local example = html2pdf.pdf "example.pdf"
example.pages = {
    input = "https://github.com/kohkimakimoto/html2pdf"
}

Multiple Pages

You can set mulitple pages.

example.pages = {
    { input = "https://github.com/kohkimakimoto/html2pdf" },
    { input = "https://github.com/kohkimakimoto/cof" },
}

Change Output File

You can change output file name.

example.output_file = "output.pdf"

Add Cover

example.cover = {
    input_content = "<b>title</b>"
    -- or
    -- input = "https://..."
}

Add TOC

example.toc = {
    toc_header_text = "Table of Content",
}

Options

You can set wkhtmltopdf options.

example.options = {
    page_size = "A4",
    margin_left = 5,
    margin_top = 5,
    margin_bottom = 5,
    margin_right = 5,
    orientation = "Landscape",
    -- etc...
}

See also: wkhtmltopdf docs

Variables

You can input variables to a config by -var and -var-file option. The variables can be read in a config by var global variable.

Example: create example.lua.

local html2pdf = require "html2pdf"

local example = html2pdf.pdf "example.pdf"
example.output_file = var.output_file
example.pages = {
    input = "https://github.com/kohkimakimoto/html2pdf"
}

Run html2pdf with -var.

$ html2pdf example.lua -var='{"output_file": "foo.pdf"}'

Write Complex Config

For writing more complex configuration, Html2pdf bundles the following lua modules.

For instance, you can generate PDF from markdown text. See the example.

DSL Syntax

Html2pdf supports to write code as DSL style. See the following example.

pdf "hello.pdf" {
    pages = {
        input_content = "hello world!"
    },
}

The pdf function is an alias of html2pdf.pdf method. So this is equivalent the following code:

local html2pdf = require "html2pdf"

local hello = html2pdf.pdf "hello.pdf"
hello.pages = {
    input_content = "hello world!"
}

Developing Html2pdf

Requirements

  • Go 1.7 or later (my development env)
  • Gom

Installing dependences

$ make deps

Building dev binary.

$ make build_bindata
$ make

Building distributed binaries.

$ make build_bindata
$ make dist

TODO

  • support windows
  • support full wkhtmltopdf options (now, It doesn't implement all options.)

Author

Kohki Makimoto [email protected]

License

The MIT License (MIT)

See Also