Skip to content

Instantly share code, notes, and snippets.

@reneeb
Created January 18, 2024 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reneeb/faa77eebc5012a123e42e2ae6f11733b to your computer and use it in GitHub Desktop.
Save reneeb/faa77eebc5012a123e42e2ae6f11733b to your computer and use it in GitHub Desktop.
html2pdf - Test
#!/usr/bin/perl
use v5.24;
use strict;
use warnings;
use MIME::Base64;
use Encode;
use LWP::UserAgent;
use JSON;
use Getopt::Long;
GetOptions(
'host=s' => \my $host,
'port=s' => \my $port,
'file=s' => \my $file,
);
$host ||= 'localhost';
$port ||= 8090;
my $url = sprintf "http://%s:%s", $host, $port;
my $html = do { local $/; <DATA> };
if ( $file ) {
$html = do { local (@ARGV, $/) = $file; <> };
}
my $base64 = MIME::Base64::encode_base64(
encode_utf8( $html )
);
my $JSON = JSON->new->utf8(1)->encode(
{ data => $base64 }
);
my $ua = LWP::UserAgent->new;
my $tx = $ua->post(
$url || 'http://localhost:8090',
Content => $JSON,
);
die "Request nicht erfolgreich!" if !$tx->is_success;
my $pdf = $tx->decoded_content;
open my $fh, '>', 'response_test.pdf';
print $fh $pdf;
close $fh;
say "PDF erstellt.";
__DATA__
<html>
<body>
<h1>Test</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment