A CLI tool for generating PDF from HTML by using Lua script.
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
Html2pdf is provided as a single binary. You can download it and drop it in your $PATH.
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 in the Html2pdf is written in Lua.
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.
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"
}
You can set mulitple pages.
example.pages = {
{ input = "https://github.com/kohkimakimoto/html2pdf" },
{ input = "https://github.com/kohkimakimoto/cof" },
}
You can change output file name.
example.output_file = "output.pdf"
example.cover = {
input_content = "<b>title</b>"
-- or
-- input = "https://..."
}
example.toc = {
toc_header_text = "Table of Content",
}
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
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"}'
For writing more complex configuration, Html2pdf bundles the following lua modules.
json
: layeh/gopher-json.fs
: kohkimakimoto/gluafs.yaml
: kohkimakimoto/gluayaml.template
: kohkimakimoto/gluatemplate.makrdown
: kohkimakimoto/makrdown.env
: kohkimakimoto/gluaenv.http
: cjoudrey/gluahttp.re
: yuin/gluare
sh
:otm/gluash
For instance, you can generate PDF from markdown text. See the example.
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!"
}
Requirements
Installing dependences
$ make deps
Building dev binary.
$ make build_bindata
$ make
Building distributed binaries.
$ make build_bindata
$ make dist
Kohki Makimoto [email protected]
The MIT License (MIT)