-
Notifications
You must be signed in to change notification settings - Fork 34
/
html2pdf_chromium.pl
68 lines (49 loc) · 1.3 KB
/
html2pdf_chromium.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
#!/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:
# chromium -- for converting HTML to PDF
use 5.010;
use strict;
use warnings;
use open IO => ':utf8', ':std';
use Getopt::Long qw(GetOptions);
my $js_delay = 10000;
sub usage {
my ($exit_code) = @_;
$exit_code //= 0;
print <<"EOT";
usage: $0 [options] [input.html | URL] [output.pdf]
options:
--js-delay=i : wait some milliseconds for JavaScript to finish (default: $js_delay)
EOT
exit($exit_code);
}
GetOptions('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...";
# Reference:
# https://peter.sh/experiments/chromium-command-line-switches/
system(
qw(
chromium
--headless
--disable-gpu
--no-pdf-header-footer
--disable-pdf-tagging
--enable-local-file-accesses
--run-all-compositor-stages-before-draw
),
"--virtual-time-budget=$js_delay",
"--print-to-pdf=$output_pdf_file",
$input_html_file,
);
if ($? != 0) {
die "`chromium` failed with code: $?";
}
say ":: Done!"