-
Notifications
You must be signed in to change notification settings - Fork 33
/
html2pdf.pl
81 lines (63 loc) · 1.82 KB
/
html2pdf.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/perl
# Author: Trizen
# Date: 16 April 2023
# https://github.com/trizen
# HTML|URL to PDF converter, with JavaScript support.
# Using the following tool:
# wkhtmltopdf -- for converting HTML to PDF
use 5.010;
use strict;
use warnings;
use open IO => ':utf8', ':std';
use Getopt::Long qw(GetOptions);
my $title = undef;
my $js = 0;
my $js_delay = 1000;
my $page_size = 'A3';
sub usage {
my ($exit_code) = @_;
$exit_code //= 0;
print <<"EOT";
usage: $0 [options] [input.html | URL] [output.pdf]
options:
--js : allow web pages to run JavaScript (default: $js)
--js-delay=i : wait some milliseconds for JavaScript to finish (default: $js_delay)
--title=s : title of the PDF file
--size=s : set paper size to: A4, Letter, etc. (default: $page_size)
EOT
exit($exit_code);
}
GetOptions(
"title=s" => \$title,
"size=s" => \$page_size,
'js|javascript!' => \$js,
'js-delay=i' => \$js_delay,
"h|help" => sub { usage(0) },
)
or die("Error in command line arguments\n");
my $input_html_file = $ARGV[0] // usage(2);
my $output_pdf_file = $ARGV[1] // "output.pdf";
say ":: Converting HTML to PDF...";
system(
qw(wkhtmltopdf
--quiet
--enable-smart-shrinking
--images
--enable-external-links
--enable-internal-links
--keep-relative-links
--enable-local-file-access
--load-error-handling ignore),
"--page-size", $page_size,
(defined($title) ? ('--title', $title) : ()),
($js ? (
'--enable-javascript',
'--javascript-delay', $js_delay
) : ('--disable-javascript')),
$input_html_file,
$output_pdf_file,
);
if ($? != 0) {
die "`wkhtmltopdf` failed with code: $?";
}
say ":: Done!"