SelectPdf Online REST API is a professional solution for managing PDF documents online. It now has a dedicated, easy to use, Perl client library that can be setup in minutes.
SelectPdf cloud API consists of the following:
HTML to PDF REST API – SelectPdf HTML To PDF Online REST API is a professional solution that lets you create PDF from web pages and raw HTML code in your applications.
PDF to TEXT REST API – SelectPdf Pdf To Text REST API is an online solution that lets you extract text from your PDF documents or search your PDF document for certain words.
PDF Merge REST API – SelectPdf Pdf Merge REST API is an online solution that lets you merge local or remote PDFs into a final PDF document.
All these APIs can be easily integrated with Perl using the dedicated client library.
Installation
Download selectpdf-api-perl-client-1.4.0.zip, unzip it and run:
perl Makefile.PL
make
make test
make install
OR
Install SelectPdf Perl Client for Online API via CPAN: SelectPdf on CPAN.
OR
Clone selectpdf-api-perl-client from Github and install the library.
cd selectpdf-api-perl-client
perl Makefile.PL
make
make test
make install
Get a trial key for SelectPdf online REST API
Once the library is installed, you need a key to be able to access the API.
GET A DEMO LICENSE KEY NOW
The free trial key for the online API is valid for 7 days and it includes 200 conversions.
Sample Code
Perl client library makes accessing SelectPdf online REST API very easy. Here are a few samples that present the main features of the API. For details and full list of parameters access the SelectPdf API Perl documentation on CPAN or the individual pages of the APIs: HTML to PDF API or PDF to TEXT API or PDF Merge API.
Convert HTML to PDF in Perl
The following sample shows the main features of the HTML To PDF API. Comment/uncomment code to convert an url to file or memory or also convert raw HTML to file or memory.
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 |
local $| = 1; use strict; use JSON; use SelectPdf; print "This is SelectPdf-$SelectPdf::VERSION.\n"; my $url = "https://selectpdf.com/"; my $local_file = "Test.pdf"; my $apiKey = "Your API key here"; eval { my $client = new SelectPdf::HtmlToPdfClient($apiKey); # set parameters - see full list at https://selectpdf.com/html-to-pdf-api/ $client # main properties ->setPageSize("A4") # PDF page size ->setPageOrientation("Portrait") # PDF page orientation ->setMargins(0) # PDF page margins ->setRenderingEngine('WebKit') # rendering engine ->setConversionDelay(1) # conversion delay ->setNavigationTimeout(30) # navigation timeout ->setShowPageNumbers('False') # page numbers ->setPageBreaksEnhancedAlgorithm('True') # enhanced page break algorithm # additional properties #->setUseCssPrint('True') # enable CSS media print #->setDisableJavascript('True') # disable javascript #->setDisableInternalLinks('True') # disable internal links #->setDisableExternalLinks('True') # disable external links #->setKeepImagesTogether('True') # keep images together #->setScaleImages('True') # scale images to create smaller pdfs #->setSinglePagePdf('True') # generate a single page PDF #->setUserPassword('password') # secure the PDF with a password # generate automatic bookmarks #->setPdfBookmarksSelectors("H1, H2") # create outlines (bookmarks) for the specified elements #->setViewerPageMode(1) # 1 (Use Outlines) - display outlines (bookmarks) in viewer ; print "Starting conversion ...\n"; # convert url to file $client->convertUrlToFile($url, $local_file); # convert url to memory # my $pdf = $client->convertUrl($url); # convert html string to file # $client->convertHtmlStringToFile("This is some <b>html</b>.", $local_file); # convert html string to memory # my $pdf = $client->convertHtmlString("This is some <b>html</b>."); print "Finished! Number of pages: " . $client->getNumberOfPages() . ".\n"; # get API usage my $usageClient = new SelectPdf::UsageClient($apiKey); my $usage = $usageClient->getUsage(); print("Usage: " . encode_json($usage) . "\n"); print("Conversions remained this month: ". $usage->{"available"}); }; if ($@) { print "An error occurred: $@\n"; } |
Convert HTML to PDF with custom header/footer in Perl
The following sample shows how to convert a web page to PDF and also setting a custom header or footer.
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 |
local $| = 1; use strict; use JSON; use SelectPdf; print "This is SelectPdf-$SelectPdf::VERSION.\n"; my $url = "https://selectpdf.com/"; my $local_file = "Test.pdf"; my $apiKey = "Your API key here"; eval { my $client = new SelectPdf::HtmlToPdfClient($apiKey); # set parameters - see full list at https://selectpdf.com/html-to-pdf-api/ $client ->setMargins(0) # PDF page margins ->setPageBreaksEnhancedAlgorithm('True') # enhanced page break algorithm # header properties ->setShowHeader('True') # display header #->setHeaderHeight(50) # header height #->setHeaderUrl($url) # header url ->setHeaderHtml("This is the <b>HEADER</b>!!!!") # header html # footer properties ->setShowFooter('True') # display footer #->setFooterHeight(60) # footer height #->setFooterUrl($url) # footer url ->setFooterHtml("This is the <b>Footer</b>!!!!") # footer html # footer page numbers ->setShowPageNumbers('True') # show page numbers in footer ->setPageNumbersTemplate('{page_number} / {total_pages}') # page numbers template ->setPageNumbersFontName('Verdana') # page numbers font name ->setPageNumbersFontSize(12) # page numbers font size ->setPageNumbersAlignment(2) # page numbers alignment 2 = Center ; print "Starting conversion ...\n"; # convert url to file $client->convertUrlToFile($url, $local_file); # convert url to memory # my $pdf = $client->convertUrl($url); # convert html string to file # $client->convertHtmlStringToFile("This is some <b>html</b>.", $local_file); # convert html string to memory # my $pdf = $client->convertHtmlString("This is some <b>html</b>."); print "Finished! Number of pages: " . $client->getNumberOfPages() . ".\n"; # get API usage my $usageClient = new SelectPdf::UsageClient($apiKey); my $usage = $usageClient->getUsage(); print("Usage: " . encode_json($usage) . "\n"); print("Conversions remained this month: ". $usage->{"available"}); }; if ($@) { print "An error occurred: $@\n"; } |
Extract text from PDF in Perl
The following sample shows how to extract the text from a PDF document using SelectPdf API. Comment/uncomment code to convert a local PDF or a PDF from a remote url to file or memory.
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 |
local $| = 1; use strict; use JSON; use SelectPdf; print "This is SelectPdf-$SelectPdf::VERSION.\n"; my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf"; my $test_pdf = "Input.pdf"; my $local_file = "Test.txt"; my $apiKey = "Your API key here"; eval { my $client = new SelectPdf::PdfToTextClient($apiKey); print "Starting pdf to text ...\n"; # set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ $client ->setStartPage(1) # start page (processing starts from here) ->setEndPage(0) # end page (set 0 to process file til the end) ->setOutputFormat(0) # set output format - 0 (Text), 1 (Html) ; # convert local pdf to local text file $client->getTextFromFileToFile($test_pdf, $local_file); # extract text from local pdf to memory # my $text = $client->getTextFromFile($test_pdf); # print $text; # convert pdf from public url to local text file # $client->getTextFromUrlToFile($test_url, $local_file); # extract text from pdf from public url to memory # my $text = $client->getTextFromUrl($test_url); # print $text; print "Finished! Number of pages processed: " . $client->getNumberOfPages() . ".\n"; # get API usage my $usageClient = new SelectPdf::UsageClient($apiKey); my $usage = $usageClient->getUsage(0); print("Usage: " . encode_json($usage) . "\n"); print("Conversions remained this month: ". $usage->{"available"}); }; if ($@) { print "An error occurred: $@\n"; } |
Search for text in PDF using Perl
The following sample shows how to search a PDF document for a specific text.
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 |
local $| = 1; use strict; use JSON; use SelectPdf; print "This is SelectPdf-$SelectPdf::VERSION.\n"; my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf"; my $test_pdf = "Input.pdf"; my $apiKey = "Your API key here"; eval { my $client = new SelectPdf::PdfToTextClient($apiKey); print "Starting search pdf ...\n"; # set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ $client ->setStartPage(1) # start page (processing starts from here) ->setEndPage(0) # end page (set 0 to process file til the end) ->setOutputFormat(0) # set output format - 0 (Text), 1 (Html) ; # search local pdf my $results = $client->searchFile($test_pdf, "pdf", "True", "True"); # search pdf from public url # my $results = $client->searchUrl($test_url, "pdf", "True", "True"); my $count = keys @{$results}; print("Number of search results: " . $count . "\n"); print("Results: " . encode_json($results) . "\n"); print "Finished! Number of pages processed: " . $client->getNumberOfPages() . ".\n"; # get API usage my $usageClient = new SelectPdf::UsageClient($apiKey); my $usage = $usageClient->getUsage(0); print("Usage: " . encode_json($usage) . "\n"); print("Conversions remained this month: ". $usage->{"available"}); }; if ($@) { print "An error occurred: $@\n"; } |
Merge PDFs using Perl
The following sample shows how merge several PDF documents into a final file. The source PDFs can be local files or PDFs from remote urls. The final PDF can be retrieved in memory or saved to a local file.
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 |
local $| = 1; use strict; use JSON; use SelectPdf; print "This is SelectPdf-$SelectPdf::VERSION\n"; my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf"; my $test_pdf = "Input.pdf"; my $local_file = "Result.pdf"; my $apiKey = "Your API key here"; eval { my $client = new SelectPdf::PdfMergeClient($apiKey); # set parameters - see full list at https://selectpdf.com/pdf-merge-api/ $client # specify the pdf files that will be merged (order will be preserved in the final pdf) ->addFile($test_pdf) # add PDF from local file ->addUrlFile($test_url) # add PDF From public url #->addFileWithPassword($test_pdf, "pdf_password") # add PDF (that requires a password) from local file #->addUrlFileWithPassword($test_url, "pdf_password") # add PDF (that requires a password) from public url ; print "Starting pdf merge ...\n"; # merge pdfs to local file $client->saveToFile($local_file); # merge pdfs to memory # my $pdf = $client->save(); print "Finished! Number of pages: " . $client->getNumberOfPages() . ".\n"; # get API usage my $usageClient = new SelectPdf::UsageClient($apiKey); my $usage = $usageClient->getUsage(0); print("Usage: " . encode_json($usage) . "\n"); print("Conversions remained this month: ". $usage->{"available"}); }; if ($@) { print "An error occurred: $@\n"; } |
The above Perl samples can also be found in the following GitHub repository:
https://github.com/selectpdf/selectpdf-api-perl-client/tree/main/samples.