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