Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more.
Perl is a family of high-level, general-purpose, interpreted, dynamic programming languages. The languages in this family include Perl 5 and Perl 6.
Though Perl is not officially an acronym, there are various backronyms in use, the most well-known being “Practical Extraction and Reporting Language”. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions. Perl 6, which began as a redesign of Perl 5 in 2000, eventually evolved into a separate language. Both languages continue to be developed independently by different development teams and liberally borrow ideas from one another.
In this post, SelectPdf team has put together a few Perl samples for our Html To Pdf API to make your life a little easier if you need to convert from web pages to pdf in your Perl applications.
Later Edit: The recommended approach to access SelectPdf Online REST API from Perl is to use the dedicated Perl Client Library for SelectPdf API. Alternatively, you can write your own code to access the API. Below are some examples for this approach.
Example #1 – How to use Perl to convert a webpage to PDF and save it on the disk
This code converts an url to pdf in Perl using SelectPdf HTML To PDF API through a GET request. The content is saved into a file on the disk.
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 |
# This code converts an url to pdf in Perl using SelectPdf REST API through a GET request. # The content is saved into a file on the disk. use LWP::UserAgent; my $api_endpoint = 'https://selectpdf.com/api2/convert/'; my $key = 'your license key here'; my $test_url = 'https://selectpdf.com'; my $local_file = 'test.pdf'; # parameters - add here any needed API parameter my $parameters = { 'key' => $key, 'url' => $test_url }; my $requesturl = URI->new($api_endpoint); $requesturl->query_form($parameters); print "Calling $requesturl\n\n"; $request = HTTP::Request->new(GET => $requesturl); $ua = LWP::UserAgent->new; $response = $ua->request($request); if ($response->is_success) { my $file = IO::File->new( $local_file, '>' ) or die "Unable to open output file - $!\n"; $file->binmode; $file->print( $response->decoded_content ); $file->close; print "Test pdf document generated successfully!\n"; } else { print "HTTP Response Code: ", $response->code, "\n"; print "HTTP Response Message: ", $response->message, "\n"; #print $response->status_line, "\n"; } |
Example #2 – How to use Perl to convert a webpage to PDF with a POST API request and save it on the disk
This code converts an url to pdf in Perl using SelectPdf HTML To PDF REST API through a POST request. The parameters are url encoded (default). The content is saved into a file on the disk.
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 |
# This code converts an url to pdf in Perl using SelectPdf REST API through a POST request. # The content is saved into a file on the disk. use LWP::UserAgent; use HTTP::Request::Common; my $api_endpoint = 'https://selectpdf.com/api2/convert/'; my $key = 'your license key here'; my $test_url = 'https://selectpdf.com'; my $local_file = 'test.pdf'; # parameters - add here any needed API parameter my $parameters = { 'key' => $key, 'url' => $test_url }; print "Calling $api_endpoint\n\n"; $ua = LWP::UserAgent->new; $response = $ua->request(POST $api_endpoint, $parameters); if ($response->is_success) { my $file = IO::File->new( $local_file, '>' ) or die "Unable to open output file - $!\n"; $file->binmode; $file->print( $response->decoded_content ); $file->close; print "Test pdf document generated successfully!\n"; } else { print "HTTP Response Code: ", $response->code, "\n"; print "HTTP Response Message: ", $response->message, "\n"; #print $response->status_line, "\n"; } |
Example #3 – How to use Perl to convert a web page to PDF with a POST API request and save it on the disk
This code converts an url to pdf in Perl using SelectPdf HTML To PDF REST API through a POST request. The parameters are JSON encoded. The content is saved into a file on the disk.
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 |
# This code converts an url to pdf in Perl using SelectPdf REST API through a POST request. # The parameters are JSON encoded. # The content is saved into a file on the disk. use LWP::UserAgent; use JSON; my $api_endpoint = 'https://selectpdf.com/api2/convert/'; my $key = 'your license key here'; my $test_url = 'https://selectpdf.com'; my $local_file = 'test.pdf'; # parameters - add here any needed API parameter my $parameters = { 'key' => $key, 'url' => $test_url }; print "Calling $api_endpoint\n\n"; $request = HTTP::Request->new(POST => $api_endpoint); $request->header('Content-Type' => 'application/json'); $request->content(encode_json $parameters); $ua = LWP::UserAgent->new; $response = $ua->request($request); if ($response->is_success) { my $file = IO::File->new( $local_file, '>' ) or die "Unable to open output file - $!\n"; $file->binmode; $file->print( $response->decoded_content ); $file->close; print "Test pdf document generated successfully!\n"; } else { print "HTTP Response Code: ", $response->code, "\n"; print "HTTP Response Message: ", $response->message, "\n"; #print $response->status_line, "\n"; } |
The above samples use only the 2 mandatory parameters (key and url or html) for the html to pdf conversion. For the full list of optional parameters, take a look at SelectPdf HTML To PDF API page. Using the API optional parameters, you can control the page size, orientation and margins in the pdf document, the document information and security, the headers and footers and a lot more.
Hopefully these examples will help you to be on your way using the API for URL to PDF conversion in Perl.
The above Perl samples can also be found in the following GitHub repository:
https://github.com/selectpdf/selectpdf-api-perl-samples