| 1 | package SelectPdf; | ||
| 2 | |||
| 3 | our $VERSION = '1.4.0'; | ||
| 4 | |||
| 5 | require SelectPdf::HtmlToPdfClient; | ||
| 6 | require SelectPdf::PdfMergeClient; | ||
| 7 | require SelectPdf::PdfToTextClient; | ||
| 8 | require SelectPdf::UsageClient; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | SelectPdf - SelectPdf Online REST API client library for Perl. Contains HTML to PDF converter, PDF merge, PDF to text extractor, search PDF. | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use SelectPdf; | ||
| 17 | print "This is SelectPdf-$SelectPdf::VERSION\n"; | ||
| 18 | |||
| 19 | Convert HTML to PDF | ||
| 20 | |||
| 21 | use SelectPdf; | ||
| 22 | print "This is SelectPdf-$SelectPdf::VERSION\n"; | ||
| 23 | |||
| 24 | my $url = "https://selectpdf.com/"; | ||
| 25 | my $local_file = "Test.pdf"; | ||
| 26 | my $apiKey = "Your API key here"; | ||
| 27 | |||
| 28 | eval { | ||
| 29 | my $client = new HtmlToPdfClient($apiKey); | ||
| 30 | |||
| 31 | $client | ||
| 32 | ->setPageSize("A4") | ||
| 33 | ->setMargins(0) | ||
| 34 | ->setShowPageNumbers('False') | ||
| 35 | ->setPageBreaksEnhancedAlgorithm('True') | ||
| 36 | ; | ||
| 37 | |||
| 38 | $client->convertUrlToFile($url, $local_file); | ||
| 39 | }; | ||
| 40 | |||
| 41 | if ($@) { | ||
| 42 | print "An error occurred: $@\n"; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | Merge PDFs from local disk or public url and save result into a file on disk. | ||
| 47 | |||
| 48 | use SelectPdf; | ||
| 49 | print "This is SelectPdf-$SelectPdf::VERSION\n"; | ||
| 50 | |||
| 51 | my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf"; | ||
| 52 | my $test_pdf = "Input.pdf"; | ||
| 53 | my $local_file = "Result.pdf"; | ||
| 54 | my $apiKey = "Your API key here"; | ||
| 55 | |||
| 56 | eval { | ||
| 57 | my $client = new SelectPdf::PdfMergeClient($apiKey); | ||
| 58 | |||
| 59 | # set parameters - see full list at https://selectpdf.com/pdf-merge-api/ | ||
| 60 | $client | ||
| 61 | # specify the pdf files that will be merged (order will be preserved in the final pdf) | ||
| 62 | |||
| 63 | ->addFile($test_pdf) # add PDF from local file | ||
| 64 | ->addUrlFile($test_url) # add PDF From public url | ||
| 65 | #->addFileWithPassword($test_pdf, "pdf_password") # add PDF (that requires a password) from local file | ||
| 66 | #->addUrlFileWithPassword($test_url, "pdf_password") # add PDF (that requires a password) from public url | ||
| 67 | ; | ||
| 68 | |||
| 69 | print "Starting pdf merge ...\n"; | ||
| 70 | |||
| 71 | # merge pdfs to local file | ||
| 72 | $client->saveToFile($local_file); | ||
| 73 | |||
| 74 | # merge pdfs to memory | ||
| 75 | # my $pdf = $client->save(); | ||
| 76 | |||
| 77 | print "Finished! Number of pages: " . $client->getNumberOfPages() . ".\n"; | ||
| 78 | |||
| 79 | # get API usage | ||
| 80 | my $usageClient = new SelectPdf::UsageClient($apiKey); | ||
| 81 | my $usage = $usageClient->getUsage(0); | ||
| 82 | print("Usage: " . encode_json($usage) . "\n"); | ||
| 83 | print("Conversions remained this month: ". $usage->{"available"}); | ||
| 84 | }; | ||
| 85 | |||
| 86 | if ($@) { | ||
| 87 | print "An error occurred: $@\n"; | ||
| 88 | } | ||
| 89 | |||
| 90 | Extract text from PDF | ||
| 91 | |||
| 92 | use JSON; | ||
| 93 | use SelectPdf; | ||
| 94 | |||
| 95 | print "This is SelectPdf-$SelectPdf::VERSION.\n"; | ||
| 96 | |||
| 97 | my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf"; | ||
| 98 | my $test_pdf = "Input.pdf"; | ||
| 99 | my $local_file = "Test.txt"; | ||
| 100 | my $apiKey = "Your API key here"; | ||
| 101 | |||
| 102 | eval { | ||
| 103 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 104 | |||
| 105 | print "Starting pdf to text ...\n"; | ||
| 106 | |||
| 107 | # set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ | ||
| 108 | $client | ||
| 109 | ->setStartPage(1) # start page (processing starts from here) | ||
| 110 | ->setEndPage(0) # end page (set 0 to process file til the end) | ||
| 111 | ->setOutputFormat(0) # set output format - 0 (Text), 1 (Html) | ||
| 112 | ; | ||
| 113 | |||
| 114 | # convert local pdf to local text file | ||
| 115 | $client->getTextFromFileToFile($test_pdf, $local_file); | ||
| 116 | |||
| 117 | # extract text from local pdf to memory | ||
| 118 | # my $text = $client->getTextFromFile($test_pdf); | ||
| 119 | # print $text; | ||
| 120 | |||
| 121 | # convert pdf from public url to local text file | ||
| 122 | # $client->getTextFromUrlToFile($test_url, $local_file); | ||
| 123 | |||
| 124 | # extract text from pdf from public url to memory | ||
| 125 | # my $text = $client->getTextFromUrl($test_url); | ||
| 126 | # print $text; | ||
| 127 | |||
| 128 | print "Finished! Number of pages processed: " . $client->getNumberOfPages() . ".\n"; | ||
| 129 | |||
| 130 | # get API usage | ||
| 131 | my $usageClient = new SelectPdf::UsageClient($apiKey); | ||
| 132 | my $usage = $usageClient->getUsage(0); | ||
| 133 | print("Usage: " . encode_json($usage) . "\n"); | ||
| 134 | print("Conversions remained this month: ". $usage->{"available"}); | ||
| 135 | }; | ||
| 136 | |||
| 137 | if ($@) { | ||
| 138 | print "An error occurred: $@\n"; | ||
| 139 | } | ||
| 140 | |||
| 141 | Search PDF | ||
| 142 | |||
| 143 | use JSON; | ||
| 144 | use SelectPdf; | ||
| 145 | |||
| 146 | print "This is SelectPdf-$SelectPdf::VERSION.\n"; | ||
| 147 | |||
| 148 | my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf"; | ||
| 149 | my $test_pdf = "Input.pdf"; | ||
| 150 | my $apiKey = "Your API key here"; | ||
| 151 | |||
| 152 | eval { | ||
| 153 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 154 | |||
| 155 | print "Starting search pdf ...\n"; | ||
| 156 | |||
| 157 | # set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ | ||
| 158 | $client | ||
| 159 | ->setStartPage(1) # start page (processing starts from here) | ||
| 160 | ->setEndPage(0) # end page (set 0 to process file til the end) | ||
| 161 | ->setOutputFormat(0) # set output format - 0 (Text), 1 (Html) | ||
| 162 | ; | ||
| 163 | |||
| 164 | # search local pdf | ||
| 165 | my $results = $client->searchFile($test_pdf, "pdf", "True", "True"); | ||
| 166 | |||
| 167 | # search pdf from public url | ||
| 168 | # my $results = $client->searchUrl($test_url, "pdf", "True", "True"); | ||
| 169 | |||
| 170 | my $count = keys @{$results}; | ||
| 171 | print("Number of search results: " . $count . "\n"); | ||
| 172 | print("Results: " . encode_json($results) . "\n"); | ||
| 173 | |||
| 174 | print "Finished! Number of pages processed: " . $client->getNumberOfPages() . ".\n"; | ||
| 175 | |||
| 176 | # get API usage | ||
| 177 | my $usageClient = new SelectPdf::UsageClient($apiKey); | ||
| 178 | my $usage = $usageClient->getUsage(0); | ||
| 179 | print("Usage: " . encode_json($usage) . "\n"); | ||
| 180 | print("Conversions remained this month: ". $usage->{"available"}); | ||
| 181 | }; | ||
| 182 | |||
| 183 | if ($@) { | ||
| 184 | print "An error occurred: $@\n"; | ||
| 185 | } | ||
| 186 | |||
| 187 | =head1 DESCRIPTION | ||
| 188 | |||
| 189 | 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. | ||
| 190 | The API is easy to use and the integration takes only a few lines of code. The generated PDFs are perfect. | ||
| 191 | That makes SelectPdf API the best html to pdf online service that can be used. | ||
| 192 | |||
| 193 | For more details and full list of parameters see L<Html To Pdf API|https://selectpdf.com/html-to-pdf-api/>. | ||
| 194 | |||
| 195 | =cut | ||
| 196 | 1; |