| 1 | package SelectPdf::PdfToTextClient; | ||
| 2 | |||
| 3 | use JSON; | ||
| 4 | use SelectPdf::ApiClient; | ||
| 5 | use SelectPdf::AsyncJobClient; | ||
| 6 | use strict; | ||
| 7 | our @ISA = qw(SelectPdf::ApiClient); | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | SelectPdf::PdfToTextClient - Pdf To Text Conversion with SelectPdf Online API. Extract text from PDF. Search PDF. | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | Extract text from PDF | ||
| 16 | |||
| 17 | use JSON; | ||
| 18 | use SelectPdf; | ||
| 19 | |||
| 20 | print "This is SelectPdf-$SelectPdf::VERSION.\n"; | ||
| 21 | |||
| 22 | my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf"; | ||
| 23 | my $test_pdf = "Input.pdf"; | ||
| 24 | my $local_file = "Test.txt"; | ||
| 25 | my $apiKey = "Your API key here"; | ||
| 26 | |||
| 27 | eval { | ||
| 28 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 29 | |||
| 30 | print "Starting pdf to text ...\n"; | ||
| 31 | |||
| 32 | # set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ | ||
| 33 | $client | ||
| 34 | ->setStartPage(1) # start page (processing starts from here) | ||
| 35 | ->setEndPage(0) # end page (set 0 to process file til the end) | ||
| 36 | ->setOutputFormat(0) # set output format - 0 (Text), 1 (Html) | ||
| 37 | ; | ||
| 38 | |||
| 39 | # convert local pdf to local text file | ||
| 40 | $client->getTextFromFileToFile($test_pdf, $local_file); | ||
| 41 | |||
| 42 | # extract text from local pdf to memory | ||
| 43 | # my $text = $client->getTextFromFile($test_pdf); | ||
| 44 | # print $text; | ||
| 45 | |||
| 46 | # convert pdf from public url to local text file | ||
| 47 | # $client->getTextFromUrlToFile($test_url, $local_file); | ||
| 48 | |||
| 49 | # extract text from pdf from public url to memory | ||
| 50 | # my $text = $client->getTextFromUrl($test_url); | ||
| 51 | # print $text; | ||
| 52 | |||
| 53 | print "Finished! Number of pages processed: " . $client->getNumberOfPages() . ".\n"; | ||
| 54 | |||
| 55 | # get API usage | ||
| 56 | my $usageClient = new SelectPdf::UsageClient($apiKey); | ||
| 57 | my $usage = $usageClient->getUsage(0); | ||
| 58 | print("Usage: " . encode_json($usage) . "\n"); | ||
| 59 | print("Conversions remained this month: ". $usage->{"available"}); | ||
| 60 | }; | ||
| 61 | |||
| 62 | if ($@) { | ||
| 63 | print "An error occurred: $@\n"; | ||
| 64 | } | ||
| 65 | |||
| 66 | Search PDF | ||
| 67 | |||
| 68 | use JSON; | ||
| 69 | use SelectPdf; | ||
| 70 | |||
| 71 | print "This is SelectPdf-$SelectPdf::VERSION.\n"; | ||
| 72 | |||
| 73 | my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf"; | ||
| 74 | my $test_pdf = "Input.pdf"; | ||
| 75 | my $apiKey = "Your API key here"; | ||
| 76 | |||
| 77 | eval { | ||
| 78 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 79 | |||
| 80 | print "Starting search pdf ...\n"; | ||
| 81 | |||
| 82 | # set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ | ||
| 83 | $client | ||
| 84 | ->setStartPage(1) # start page (processing starts from here) | ||
| 85 | ->setEndPage(0) # end page (set 0 to process file til the end) | ||
| 86 | ->setOutputFormat(0) # set output format - 0 (Text), 1 (Html) | ||
| 87 | ; | ||
| 88 | |||
| 89 | # search local pdf | ||
| 90 | my $results = $client->searchFile($test_pdf, "pdf", "True", "True"); | ||
| 91 | |||
| 92 | # search pdf from public url | ||
| 93 | # my $results = $client->searchUrl($test_url, "pdf", "True", "True"); | ||
| 94 | |||
| 95 | my $count = keys @{$results}; | ||
| 96 | print("Number of search results: " . $count . "\n"); | ||
| 97 | print("Results: " . encode_json($results) . "\n"); | ||
| 98 | |||
| 99 | print "Finished! Number of pages processed: " . $client->getNumberOfPages() . ".\n"; | ||
| 100 | |||
| 101 | # get API usage | ||
| 102 | my $usageClient = new SelectPdf::UsageClient($apiKey); | ||
| 103 | my $usage = $usageClient->getUsage(0); | ||
| 104 | print("Usage: " . encode_json($usage) . "\n"); | ||
| 105 | print("Conversions remained this month: ". $usage->{"available"}); | ||
| 106 | }; | ||
| 107 | |||
| 108 | if ($@) { | ||
| 109 | print "An error occurred: $@\n"; | ||
| 110 | } | ||
| 111 | |||
| 112 | For more details and full list of parameters see L<Pdf To Text API|https://selectpdf.com/pdf-to-text-api/>. | ||
| 113 | |||
| 114 | =head1 METHODS | ||
| 115 | |||
| 116 | =head2 new( $apiKey ) | ||
| 117 | |||
| 118 | Construct the Pdf To Text Client. | ||
| 119 | |||
| 120 | my $client = SelectPdf::PdfToTextClient->new($apiKey); | ||
| 121 | |||
| 122 | Parameters: | ||
| 123 | |||
| 124 | - $apiKey: API Key. | ||
| 125 | =cut | ||
| 126 | sub new { | ||
| 127 | my $type = shift; | ||
| 128 | my $self = $type->SUPER::new; | ||
| 129 | |||
| 130 | # API endpoint | ||
| 131 | $self->{apiEndpoint} = "https://selectpdf.com/api2/pdftotext/"; | ||
| 132 | |||
| 133 | $self->{fileIdx} = 0; | ||
| 134 | |||
| 135 | $self->{parameters}{"key"} = shift; | ||
| 136 | |||
| 137 | bless $self, $type; | ||
| 138 | return $self; | ||
| 139 | } | ||
| 140 | |||
| 141 | =head2 getTextFromFile( $inputPdf ) | ||
| 142 | |||
| 143 | Get the text from the specified pdf. | ||
| 144 | |||
| 145 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 146 | $text = $client->getTextFromFile($inputPdf); | ||
| 147 | |||
| 148 | Parameters: | ||
| 149 | |||
| 150 | - $inputPdf: Path to a local PDF file. | ||
| 151 | |||
| 152 | Returns: | ||
| 153 | |||
| 154 | - Extracted text. | ||
| 155 | =cut | ||
| 156 | sub getTextFromFile($) { | ||
| 157 | my($self, $inputPdf) = @_; | ||
| 158 | |||
| 159 | $self->{parameters}{"async"} = "False"; | ||
| 160 | $self->{parameters}{"action"} = "Convert"; | ||
| 161 | $self->{parameters}{"url"} = ""; | ||
| 162 | |||
| 163 | $self->{files} = {}; | ||
| 164 | $self->{files}{"inputPdf"} = $inputPdf; | ||
| 165 | |||
| 166 | my $text = $self->SUPER::performPostAsMultipartFormData(); | ||
| 167 | $text =~ s/\r//g; | ||
| 168 | |||
| 169 | return $text; | ||
| 170 | } | ||
| 171 | |||
| 172 | =head2 getTextFromFileToFile( $inputPdf, $outputFilePath ) | ||
| 173 | |||
| 174 | Get the text from the specified pdf and write it to the specified text file. | ||
| 175 | |||
| 176 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 177 | $client->getTextFromFileToFile($inputPdf, $outputFilePath); | ||
| 178 | |||
| 179 | Parameters: | ||
| 180 | |||
| 181 | - $inputPdf: Path to a local PDF file. | ||
| 182 | |||
| 183 | - $outputFilePath: The output file where the resulted text will be written. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | sub getTextFromFileToFile($,$) { | ||
| 187 | my($self, $inputPdf, $outputFilePath) = @_; | ||
| 188 | |||
| 189 | my $result = $self->getTextFromFile($inputPdf); | ||
| 190 | |||
| 191 | my $file = IO::File->new( $outputFilePath, '>:encoding(UTF-8)' ) or die "Unable to open output file - $!\n"; | ||
| 192 | $file->print( $result ); | ||
| 193 | $file->close; | ||
| 194 | } | ||
| 195 | |||
| 196 | =head2 getTextFromFileAsync( $inputPdf ) | ||
| 197 | |||
| 198 | Get the text from the specified pdf with an asynchronous call. | ||
| 199 | |||
| 200 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 201 | $text = $client->getTextFromFileAsync($inputPdf); | ||
| 202 | |||
| 203 | Parameters: | ||
| 204 | |||
| 205 | - $inputPdf: Path to a local PDF file. | ||
| 206 | |||
| 207 | Returns: | ||
| 208 | |||
| 209 | - Extracted text. | ||
| 210 | =cut | ||
| 211 | sub getTextFromFileAsync($) { | ||
| 212 | my($self, $inputPdf) = @_; | ||
| 213 | |||
| 214 | $self->{parameters}{"action"} = "Convert"; | ||
| 215 | $self->{parameters}{"url"} = ""; | ||
| 216 | |||
| 217 | $self->{files} = {}; | ||
| 218 | $self->{files}{"inputPdf"} = $inputPdf; | ||
| 219 | |||
| 220 | my $JobID = $self->SUPER::startAsyncJobMultipartFormData() or die "An error occurred launching the asynchronous call."; | ||
| 221 | |||
| 222 | my $noPings = 0; | ||
| 223 | |||
| 224 | do | ||
| 225 | { | ||
| 226 | $noPings++; | ||
| 227 | |||
| 228 | # sleep for a few seconds before next ping | ||
| 229 | sleep($self->{AsyncCallsPingInterval}); | ||
| 230 | |||
| 231 | my $asyncJobClient = new SelectPdf::AsyncJobClient($self->{parameters}{"key"}, $JobID); | ||
| 232 | $asyncJobClient->setApiEndpoint($self->{apiAsyncEndpoint}); | ||
| 233 | |||
| 234 | my $text = $asyncJobClient->getResult(); | ||
| 235 | |||
| 236 | if ($asyncJobClient->finished) | ||
| 237 | { | ||
| 238 | $self->{numberOfPages} = $asyncJobClient->getNumberOfPages(); | ||
| 239 | |||
| 240 | $text =~ s/\r//g; | ||
| 241 | return $text; | ||
| 242 | } | ||
| 243 | |||
| 244 | } while ($noPings <= $self->{AsyncCallsMaxPings}); | ||
| 245 | |||
| 246 | die "Asynchronous call did not finish in expected timeframe."; | ||
| 247 | } | ||
| 248 | |||
| 249 | =head2 getTextFromFileToFileAsync( $inputPdf, $outputFilePath ) | ||
| 250 | |||
| 251 | Get the text from the specified pdf with an asynchronous call and write it to the specified text file. | ||
| 252 | |||
| 253 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 254 | $client->getTextFromFileToFileAsync($inputPdf, $outputFilePath); | ||
| 255 | |||
| 256 | Parameters: | ||
| 257 | |||
| 258 | - $inputPdf: Path to a local PDF file. | ||
| 259 | |||
| 260 | - $outputFilePath: The output file where the resulted text will be written. | ||
| 261 | |||
| 262 | =cut | ||
| 263 | sub getTextFromFileToFileAsync($,$) { | ||
| 264 | my($self, $inputPdf, $outputFilePath) = @_; | ||
| 265 | |||
| 266 | my $result = $self->getTextFromFileAsync($inputPdf); | ||
| 267 | |||
| 268 | my $file = IO::File->new( $outputFilePath, '>:encoding(UTF-8)' ) or die "Unable to open output file - $!\n"; | ||
| 269 | $file->print( $result ); | ||
| 270 | $file->close; | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | =head2 getTextFromUrl( $url ) | ||
| 275 | |||
| 276 | Get the text from the specified pdf. | ||
| 277 | |||
| 278 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 279 | $text = $client->getTextFromUrl($url); | ||
| 280 | |||
| 281 | Parameters: | ||
| 282 | |||
| 283 | - $url: Address of the PDF file. | ||
| 284 | |||
| 285 | Returns: | ||
| 286 | |||
| 287 | - Extracted text. | ||
| 288 | =cut | ||
| 289 | sub getTextFromUrl($) { | ||
| 290 | my($self, $url) = @_; | ||
| 291 | |||
| 292 | $self->{parameters}{"async"} = "False"; | ||
| 293 | $self->{parameters}{"action"} = "Convert"; | ||
| 294 | $self->{parameters}{"url"} = $url; | ||
| 295 | |||
| 296 | $self->{files} = {}; | ||
| 297 | |||
| 298 | my $text = $self->SUPER::performPostAsMultipartFormData(); | ||
| 299 | $text =~ s/\r//g; | ||
| 300 | |||
| 301 | return $text; | ||
| 302 | } | ||
| 303 | |||
| 304 | =head2 getTextFromUrlToFile( $url, $outputFilePath ) | ||
| 305 | |||
| 306 | Get the text from the specified pdf and write it to the specified text file. | ||
| 307 | |||
| 308 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 309 | $client->getTextFromUrlToFile($url, $outputFilePath); | ||
| 310 | |||
| 311 | Parameters: | ||
| 312 | |||
| 313 | - $url: Address of the PDF file. | ||
| 314 | |||
| 315 | - $outputFilePath: The output file where the resulted text will be written. | ||
| 316 | |||
| 317 | =cut | ||
| 318 | sub getTextFromUrlToFile($,$) { | ||
| 319 | my($self, $url, $outputFilePath) = @_; | ||
| 320 | |||
| 321 | my $result = $self->getTextFromUrl($url); | ||
| 322 | |||
| 323 | my $file = IO::File->new( $outputFilePath, '>:encoding(UTF-8)' ) or die "Unable to open output file - $!\n"; | ||
| 324 | $file->print( $result ); | ||
| 325 | $file->close; | ||
| 326 | } | ||
| 327 | |||
| 328 | =head2 getTextFromUrlAsync( $url ) | ||
| 329 | |||
| 330 | Get the text from the specified pdf with an asynchronous call. | ||
| 331 | |||
| 332 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 333 | $text = $client->getTextFromUrlAsync($url); | ||
| 334 | |||
| 335 | Parameters: | ||
| 336 | |||
| 337 | - $url: Address of the PDF file. | ||
| 338 | |||
| 339 | Returns: | ||
| 340 | |||
| 341 | - Extracted text. | ||
| 342 | =cut | ||
| 343 | sub getTextFromUrlAsync($) { | ||
| 344 | my($self, $url) = @_; | ||
| 345 | |||
| 346 | $self->{parameters}{"action"} = "Convert"; | ||
| 347 | $self->{parameters}{"url"} = $url; | ||
| 348 | |||
| 349 | $self->{files} = {}; | ||
| 350 | |||
| 351 | my $JobID = $self->SUPER::startAsyncJobMultipartFormData() or die "An error occurred launching the asynchronous call."; | ||
| 352 | |||
| 353 | my $noPings = 0; | ||
| 354 | |||
| 355 | do | ||
| 356 | { | ||
| 357 | $noPings++; | ||
| 358 | |||
| 359 | # sleep for a few seconds before next ping | ||
| 360 | sleep($self->{AsyncCallsPingInterval}); | ||
| 361 | |||
| 362 | my $asyncJobClient = new SelectPdf::AsyncJobClient($self->{parameters}{"key"}, $JobID); | ||
| 363 | $asyncJobClient->setApiEndpoint($self->{apiAsyncEndpoint}); | ||
| 364 | |||
| 365 | my $text = $asyncJobClient->getResult(); | ||
| 366 | |||
| 367 | if ($asyncJobClient->finished) | ||
| 368 | { | ||
| 369 | $self->{numberOfPages} = $asyncJobClient->getNumberOfPages(); | ||
| 370 | |||
| 371 | $text =~ s/\r//g; | ||
| 372 | return $text; | ||
| 373 | } | ||
| 374 | |||
| 375 | } while ($noPings <= $self->{AsyncCallsMaxPings}); | ||
| 376 | |||
| 377 | die "Asynchronous call did not finish in expected timeframe."; | ||
| 378 | } | ||
| 379 | |||
| 380 | =head2 getTextFromUrlToFileAsync( $url, $outputFilePath ) | ||
| 381 | |||
| 382 | Get the text from the specified pdf with an asynchronous call and write it to the specified text file. | ||
| 383 | |||
| 384 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 385 | $client->getTextFromUrlToFileAsync($url, $outputFilePath); | ||
| 386 | |||
| 387 | Parameters: | ||
| 388 | |||
| 389 | - $url: Address of the PDF file. | ||
| 390 | |||
| 391 | - $outputFilePath: The output file where the resulted text will be written. | ||
| 392 | |||
| 393 | =cut | ||
| 394 | sub getTextFromUrlToFileAsync($,$) { | ||
| 395 | my($self, $url, $outputFilePath) = @_; | ||
| 396 | |||
| 397 | my $result = $self->getTextFromUrlAsync($url); | ||
| 398 | |||
| 399 | my $file = IO::File->new( $outputFilePath, '>:encoding(UTF-8)' ) or die "Unable to open output file - $!\n"; | ||
| 400 | $file->print( $result ); | ||
| 401 | $file->close; | ||
| 402 | } | ||
| 403 | |||
| 404 | |||
| 405 | =head2 searchFile( $inputPdf, $textToSearch, $caseSensitive, $wholeWordsOnly ) | ||
| 406 | |||
| 407 | Search for a specific text in a PDF document. | ||
| 408 | Pages that participate to this operation are specified by setStartPage() and setEndPage() methods. | ||
| 409 | |||
| 410 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 411 | $results = $client->searchFile($inputPdf, $textToSearch); | ||
| 412 | |||
| 413 | Parameters: | ||
| 414 | |||
| 415 | - $inputPdf: Path to a local PDF file. | ||
| 416 | |||
| 417 | - $textToSearch: Text to search. | ||
| 418 | |||
| 419 | - $caseSensitive: If the search is case sensitive or not. | ||
| 420 | |||
| 421 | - $wholeWordsOnly: If the search works on whole words or not. | ||
| 422 | |||
| 423 | Returns: | ||
| 424 | |||
| 425 | - List with text positions in the current PDF document. | ||
| 426 | =cut | ||
| 427 | sub searchFile($,$,$,$) { | ||
| 428 | my($self, $inputPdf, $textToSearch, $caseSensitive, $wholeWordsOnly) = @_; | ||
| 429 | |||
| 430 | if (!$textToSearch) { | ||
| 431 | die ("Search text cannot be empty."); | ||
| 432 | } | ||
| 433 | |||
| 434 | $self->{parameters}{"async"} = "False"; | ||
| 435 | $self->{parameters}{"action"} = "Search"; | ||
| 436 | $self->{parameters}{"url"} = ""; | ||
| 437 | $self->{parameters}{"search_text"} = $textToSearch; | ||
| 438 | $self->{parameters}{"case_sensitive"} = $self->SUPER::serializeBoolean($caseSensitive); | ||
| 439 | $self->{parameters}{"whole_words_only"} = $self->SUPER::serializeBoolean($wholeWordsOnly); | ||
| 440 | |||
| 441 | $self->{files} = {}; | ||
| 442 | $self->{files}{"inputPdf"} = $inputPdf; | ||
| 443 | |||
| 444 | $self->{headers}{"Accept"} = "text/json"; | ||
| 445 | |||
| 446 | my $result = $self->SUPER::performPostAsMultipartFormData(); | ||
| 447 | |||
| 448 | if ($result) { | ||
| 449 | return decode_json($result); | ||
| 450 | } | ||
| 451 | else { | ||
| 452 | return []; | ||
| 453 | } | ||
| 454 | } | ||
| 455 | |||
| 456 | =head2 searchFileAsync( $inputPdf, $textToSearch, $caseSensitive, $wholeWordsOnly ) | ||
| 457 | |||
| 458 | Search for a specific text in a PDF document with an asynchronous call. | ||
| 459 | Pages that participate to this operation are specified by setStartPage() and setEndPage() methods. | ||
| 460 | |||
| 461 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 462 | $results = $client->searchFileAsync($inputPdf, $textToSearch); | ||
| 463 | |||
| 464 | Parameters: | ||
| 465 | |||
| 466 | - $inputPdf: Path to a local PDF file. | ||
| 467 | |||
| 468 | - $textToSearch: Text to search. | ||
| 469 | |||
| 470 | - $caseSensitive: If the search is case sensitive or not. | ||
| 471 | |||
| 472 | - $wholeWordsOnly: If the search works on whole words or not. | ||
| 473 | |||
| 474 | Returns: | ||
| 475 | |||
| 476 | - List with text positions in the current PDF document. | ||
| 477 | =cut | ||
| 478 | sub searchFileAsync($,$,$,$) { | ||
| 479 | my($self, $inputPdf, $textToSearch, $caseSensitive, $wholeWordsOnly) = @_; | ||
| 480 | |||
| 481 | if (!$textToSearch) { | ||
| 482 | die ("Search text cannot be empty."); | ||
| 483 | } | ||
| 484 | |||
| 485 | $self->{parameters}{"action"} = "Search"; | ||
| 486 | $self->{parameters}{"url"} = ""; | ||
| 487 | $self->{parameters}{"search_text"} = $textToSearch; | ||
| 488 | $self->{parameters}{"case_sensitive"} = $self->SUPER::serializeBoolean($caseSensitive); | ||
| 489 | $self->{parameters}{"whole_words_only"} = $self->SUPER::serializeBoolean($wholeWordsOnly); | ||
| 490 | |||
| 491 | $self->{files} = {}; | ||
| 492 | $self->{files}{"inputPdf"} = $inputPdf; | ||
| 493 | |||
| 494 | $self->{headers}{"Accept"} = "text/json"; | ||
| 495 | |||
| 496 | my $JobID = $self->SUPER::startAsyncJobMultipartFormData() or die "An error occurred launching the asynchronous call."; | ||
| 497 | |||
| 498 | my $noPings = 0; | ||
| 499 | |||
| 500 | do | ||
| 501 | { | ||
| 502 | $noPings++; | ||
| 503 | |||
| 504 | # sleep for a few seconds before next ping | ||
| 505 | sleep($self->{AsyncCallsPingInterval}); | ||
| 506 | |||
| 507 | my $asyncJobClient = new SelectPdf::AsyncJobClient($self->{parameters}{"key"}, $JobID); | ||
| 508 | $asyncJobClient->setApiEndpoint($self->{apiAsyncEndpoint}); | ||
| 509 | |||
| 510 | my $result = $asyncJobClient->getResult(); | ||
| 511 | |||
| 512 | if ($asyncJobClient->finished) | ||
| 513 | { | ||
| 514 | $self->{numberOfPages} = $asyncJobClient->getNumberOfPages(); | ||
| 515 | |||
| 516 | if ($result) { | ||
| 517 | return decode_json($result); | ||
| 518 | } | ||
| 519 | else { | ||
| 520 | return []; | ||
| 521 | } | ||
| 522 | } | ||
| 523 | |||
| 524 | } while ($noPings <= $self->{AsyncCallsMaxPings}); | ||
| 525 | |||
| 526 | die "Asynchronous call did not finish in expected timeframe."; | ||
| 527 | |||
| 528 | } | ||
| 529 | |||
| 530 | =head2 searchUrl( $url, $textToSearch, $caseSensitive, $wholeWordsOnly ) | ||
| 531 | |||
| 532 | Search for a specific text in a PDF document. | ||
| 533 | Pages that participate to this operation are specified by setStartPage() and setEndPage() methods. | ||
| 534 | |||
| 535 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 536 | $results = $client->searchUrl($url, $textToSearch); | ||
| 537 | |||
| 538 | Parameters: | ||
| 539 | |||
| 540 | - $url: Address of the PDF file. | ||
| 541 | |||
| 542 | - $textToSearch: Text to search. | ||
| 543 | |||
| 544 | - $caseSensitive: If the search is case sensitive or not. | ||
| 545 | |||
| 546 | - $wholeWordsOnly: If the search works on whole words or not. | ||
| 547 | |||
| 548 | Returns: | ||
| 549 | |||
| 550 | - List with text positions in the current PDF document. | ||
| 551 | =cut | ||
| 552 | sub searchUrl($,$,$,$) { | ||
| 553 | my($self, $url, $textToSearch, $caseSensitive, $wholeWordsOnly) = @_; | ||
| 554 | |||
| 555 | if (!$textToSearch) { | ||
| 556 | die ("Search text cannot be empty."); | ||
| 557 | } | ||
| 558 | |||
| 559 | $self->{parameters}{"async"} = "False"; | ||
| 560 | $self->{parameters}{"action"} = "Search"; | ||
| 561 | $self->{parameters}{"search_text"} = $textToSearch; | ||
| 562 | $self->{parameters}{"case_sensitive"} = $self->SUPER::serializeBoolean($caseSensitive); | ||
| 563 | $self->{parameters}{"whole_words_only"} = $self->SUPER::serializeBoolean($wholeWordsOnly); | ||
| 564 | |||
| 565 | $self->{files} = {}; | ||
| 566 | $self->{parameters}{"url"} = $url; | ||
| 567 | |||
| 568 | $self->{headers}{"Accept"} = "text/json"; | ||
| 569 | |||
| 570 | my $result = $self->SUPER::performPostAsMultipartFormData(); | ||
| 571 | |||
| 572 | if ($result) { | ||
| 573 | return decode_json($result); | ||
| 574 | } | ||
| 575 | else { | ||
| 576 | return []; | ||
| 577 | } | ||
| 578 | } | ||
| 579 | |||
| 580 | =head2 searchUrlAsync( $url, $textToSearch, $caseSensitive, $wholeWordsOnly ) | ||
| 581 | |||
| 582 | Search for a specific text in a PDF document with an asynchronous call. | ||
| 583 | Pages that participate to this operation are specified by setStartPage() and setEndPage() methods. | ||
| 584 | |||
| 585 | my $client = new SelectPdf::PdfToTextClient($apiKey); | ||
| 586 | $results = $client->searchUrlAsync($url, $textToSearch); | ||
| 587 | |||
| 588 | Parameters: | ||
| 589 | |||
| 590 | - $url: Address of the PDF file. | ||
| 591 | |||
| 592 | - $textToSearch: Text to search. | ||
| 593 | |||
| 594 | - $caseSensitive: If the search is case sensitive or not. | ||
| 595 | |||
| 596 | - $wholeWordsOnly: If the search works on whole words or not. | ||
| 597 | |||
| 598 | Returns: | ||
| 599 | |||
| 600 | - List with text positions in the current PDF document. | ||
| 601 | =cut | ||
| 602 | sub searchUrlAsync($,$,$,$) { | ||
| 603 | my($self, $url, $textToSearch, $caseSensitive, $wholeWordsOnly) = @_; | ||
| 604 | |||
| 605 | if (!$textToSearch) { | ||
| 606 | die ("Search text cannot be empty."); | ||
| 607 | } | ||
| 608 | |||
| 609 | $self->{parameters}{"action"} = "Search"; | ||
| 610 | $self->{parameters}{"search_text"} = $textToSearch; | ||
| 611 | $self->{parameters}{"case_sensitive"} = $self->SUPER::serializeBoolean($caseSensitive); | ||
| 612 | $self->{parameters}{"whole_words_only"} = $self->SUPER::serializeBoolean($wholeWordsOnly); | ||
| 613 | |||
| 614 | $self->{files} = {}; | ||
| 615 | $self->{parameters}{"url"} = $url; | ||
| 616 | |||
| 617 | $self->{headers}{"Accept"} = "text/json"; | ||
| 618 | |||
| 619 | my $JobID = $self->SUPER::startAsyncJobMultipartFormData() or die "An error occurred launching the asynchronous call."; | ||
| 620 | |||
| 621 | my $noPings = 0; | ||
| 622 | |||
| 623 | do | ||
| 624 | { | ||
| 625 | $noPings++; | ||
| 626 | |||
| 627 | # sleep for a few seconds before next ping | ||
| 628 | sleep($self->{AsyncCallsPingInterval}); | ||
| 629 | |||
| 630 | my $asyncJobClient = new SelectPdf::AsyncJobClient($self->{parameters}{"key"}, $JobID); | ||
| 631 | $asyncJobClient->setApiEndpoint($self->{apiAsyncEndpoint}); | ||
| 632 | |||
| 633 | my $result = $asyncJobClient->getResult(); | ||
| 634 | |||
| 635 | if ($asyncJobClient->finished) | ||
| 636 | { | ||
| 637 | $self->{numberOfPages} = $asyncJobClient->getNumberOfPages(); | ||
| 638 | |||
| 639 | if ($result) { | ||
| 640 | return decode_json($result); | ||
| 641 | } | ||
| 642 | else { | ||
| 643 | return []; | ||
| 644 | } | ||
| 645 | } | ||
| 646 | |||
| 647 | } while ($noPings <= $self->{AsyncCallsMaxPings}); | ||
| 648 | |||
| 649 | die "Asynchronous call did not finish in expected timeframe."; | ||
| 650 | |||
| 651 | } | ||
| 652 | |||
| 653 | |||
| 654 | =head2 setCustomParameter( $parameterName, $parameterValue ) | ||
| 655 | |||
| 656 | Set a custom parameter. Do not use this method unless advised by SelectPdf. | ||
| 657 | |||
| 658 | Parameters: | ||
| 659 | |||
| 660 | - $parameterName: Parameter name. | ||
| 661 | |||
| 662 | - $parameterValue: Parameter value. | ||
| 663 | |||
| 664 | Returns: | ||
| 665 | |||
| 666 | - Reference to the current object. | ||
| 667 | =cut | ||
| 668 | sub setCustomParameter($,$) { | ||
| 669 | my($self, $parameterName, $parameterValue) = @_; | ||
| 670 | |||
| 671 | $self->{parameters}{$parameterName} = $parameterValue; | ||
| 672 | return $self; | ||
| 673 | } | ||
| 674 | |||
| 675 | =head2 setTimeout( $timeout ) | ||
| 676 | |||
| 677 | Set the maximum amount of time (in seconds) for this job. | ||
| 678 | The default value is 30 seconds. | ||
| 679 | Use a larger value (up to 120 seconds allowed) for pages that take a long time to load. | ||
| 680 | |||
| 681 | Parameters: | ||
| 682 | |||
| 683 | - $timeout: Timeout in seconds. | ||
| 684 | |||
| 685 | Returns: | ||
| 686 | |||
| 687 | - Reference to the current object. | ||
| 688 | =cut | ||
| 689 | sub setTimeout($) { | ||
| 690 | my($self, $timeout) = @_; | ||
| 691 | |||
| 692 | $self->{parameters}{"timeout"} = $timeout; | ||
| 693 | return $self; | ||
| 694 | } | ||
| 695 | |||
| 696 | =head2 setStartPage( $startPage ) | ||
| 697 | |||
| 698 | Set Start Page number. Default value is 1 (first page of the document). | ||
| 699 | |||
| 700 | Parameters: | ||
| 701 | |||
| 702 | - $startPage: Start page number (1-based). | ||
| 703 | |||
| 704 | Returns: | ||
| 705 | |||
| 706 | - Reference to the current object. | ||
| 707 | =cut | ||
| 708 | sub setStartPage($) { | ||
| 709 | my($self, $startPage) = @_; | ||
| 710 | |||
| 711 | $self->{parameters}{"start_page"} = $startPage; | ||
| 712 | return $self; | ||
| 713 | } | ||
| 714 | |||
| 715 | =head2 setEndPage( $endPage ) | ||
| 716 | |||
| 717 | Set End Page number. Default value is 0 (process till the last page of the document). | ||
| 718 | |||
| 719 | Parameters: | ||
| 720 | |||
| 721 | - $endPage: End page number (1-based). | ||
| 722 | |||
| 723 | Returns: | ||
| 724 | |||
| 725 | - Reference to the current object. | ||
| 726 | =cut | ||
| 727 | sub setEndPage($) { | ||
| 728 | my($self, $endPage) = @_; | ||
| 729 | |||
| 730 | $self->{parameters}{"end_page"} = $endPage; | ||
| 731 | return $self; | ||
| 732 | } | ||
| 733 | |||
| 734 | =head2 setUserPassword( $userPassword ) | ||
| 735 | |||
| 736 | Set PDF user password. | ||
| 737 | |||
| 738 | Parameters: | ||
| 739 | |||
| 740 | - $userPassword: PDF user password. | ||
| 741 | |||
| 742 | Returns: | ||
| 743 | |||
| 744 | - Reference to the current object. | ||
| 745 | =cut | ||
| 746 | sub setUserPassword($) { | ||
| 747 | my($self, $userPassword) = @_; | ||
| 748 | |||
| 749 | $self->{parameters}{"user_password"} = $userPassword; | ||
| 750 | return $self; | ||
| 751 | } | ||
| 752 | |||
| 753 | |||
| 754 | =head2 setTextLayout( $textLayout ) | ||
| 755 | |||
| 756 | Set the text layout. The default value is 0 (Original). | ||
| 757 | |||
| 758 | Parameters: | ||
| 759 | |||
| 760 | - $textLayout: The text layout. Possible values: 0 (Original), 1 (Reading). | ||
| 761 | |||
| 762 | Returns: | ||
| 763 | |||
| 764 | - Reference to the current object. | ||
| 765 | =cut | ||
| 766 | sub setTextLayout($) { | ||
| 767 | my($self, $textLayout) = @_; | ||
| 768 | |||
| 769 | if ($textLayout ne 0 and $textLayout ne 1) { | ||
| 770 | die ("Allowed values for Text Layout: 0 (Original), 1 (Reading)."); | ||
| 771 | } | ||
| 772 | |||
| 773 | $self->{parameters}{"text_layout"} = $textLayout; | ||
| 774 | return $self; | ||
| 775 | } | ||
| 776 | |||
| 777 | =head2 setOutputFormat( $outputFormat ) | ||
| 778 | |||
| 779 | Set the output format. The default value is 0 (Text). | ||
| 780 | |||
| 781 | Parameters: | ||
| 782 | |||
| 783 | - $outputFormat: The output format. Possible values: 0 (Text), 1 (Html). | ||
| 784 | |||
| 785 | Returns: | ||
| 786 | |||
| 787 | - Reference to the current object. | ||
| 788 | =cut | ||
| 789 | sub setOutputFormat($) { | ||
| 790 | my($self, $outputFormat) = @_; | ||
| 791 | |||
| 792 | if ($outputFormat ne 0 and $outputFormat ne 1) { | ||
| 793 | die ("Allowed values for Output Format: 0 (Text), 1 (Html)."); | ||
| 794 | } | ||
| 795 | |||
| 796 | $self->{parameters}{"output_format"} = $outputFormat; | ||
| 797 | return $self; | ||
| 798 | } | ||
| 799 | |||
| 800 | |||
| 801 | 1; |