#!/bin/bash
# doc/tool/wkhtmltopdf - HTML to PDF conversion support

set -e


######
# Native dependencies

doc_tool_wkhtmltopdf_client_packages=( wkhtmltopdf )


######
# Settings

wkhtmltodoc_pdf_config_init() {
	lib_setting_vars --null wkhtml_title wkhtml_orientation wkhtml_dpi \
		wkhtml_page_width wkhtml_page_height wkhtml_page_size \
		wkhtml_margin_left wkhtml_margin_top \
		wkhtml_margin_right wkhtml_margin_bottom \
		wkhtml_collate wkhtml_print_media
	wkhtml_paper_size="${wkhtml_page_size:-Letter}"
	wkhtml_margin_top="${wkhtml_margin_top:-1.2in}"
	wkhtml_margin_bottom="${wkhtml_margin_bottom:-1in}"
	wkhtml_margin_left="${wkhtml_margin_left:-1in}"
	wkhtml_margin_right="${wkhtml_margin_right:-1in}"

	lib_setting_vars wkhtml_grayscale wkhtml_low_quality
	wkhtml_grayscale=${wkhtml_grayscale:-false}
	wkhtml_low_quality=${wkhtml_low_quality:-false}
}


######
# Internal functions

wkhtml_opts() {
	! $quiet || opts+=( --quiet )

	[ -z "$wkhtml_title" ] || opts+=( --title "$wkhtml_title" )
	[ -z "$wkhtml_orientation" ] \
		|| opts+=( --orientation "$wkhtml_orientation" )
	[ -z "$wkhtml_dpi" ] || opts+=( --dpi "$mkhtml_dpi" )

	[ -z "$wkhtml_page_width" ] \
		|| opts+=( --page-width "$wkhtml_page_width" )
	[ -z "$wkhtml_page_height" ] \
		|| opts+=( --page-height "$wkhtml_page_height" )
	[ -z "$wkhtml_page_size" ] \
		|| opts+=( --page-size "$wkhtml_page_size" )

	[ -z "$wkhtml_margin_left" ] \
		|| opts+=( --margin-left "$wkhtml_margin_left" )
	[ -z "$wkhtml_margin_top" ] \
		|| opts+=( --margin-top "$wkhtml_margin_top" )
	[ -z "$wkhtml_margin_right" ] \
		|| opts+=( --margin-right "$wkhtml_margin_right" )
	[ -z "$wkhtml_margin_bottom" ] \
		|| opts+=( --margin-bottom "$wkhtml_margin_bottom" )

	! $wkhtml_grayscale || opts+=( --grayscale )
	! $wkhtml_low_quality || opts+=( --low-quality )

	if [ "$wkhtml_print_media" ]; then
		local prefix
		$wkhtml_print_media || prefix='no-'
		opts+=( "--${prefix}print-media-type" )
	fi

	if [ "$wkhtml_collate" ]; then
		local prefix
		$wkhtml_collate || prefix='no-'
		opts+=( "--${prefix}collate" )
	fi
}

run_wkhtml() {
	min_args 3 "$@"
	local bin="wkhtmlto$1"
	local ext=$2
	shift 2

	local tmp
	tmp=$(cmd_tempfile)

	local name
	for name in "$@"; do
		echo "$name" "${name%.html}.$ext"
	done >"$tmp"

	local -a opts
	wkhtml_opts

	local log
	log=$(cmd_tempfile)

	run "$bin" "${opts[@]}" --read-args-from-stdin <"$tmp" >"$log" \
		|| (cat "$log" && false)
}

######
# wkhtmltopdf support

html_to_pdf_wk() { run_wkhtml pdf pdf "$@"; }

######
# wkhtmltoimage support

html_to_jpg_wk() { run_wkhtml image jpg "$@"; }
html_to_png_wk() { run_wkhtml image png "$@"; }

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:35:43 PDT 2017 by mcsh d14 v0.23.0.