SelectPdf/PdfMergeClient.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 241 CC 4 SCAR 10.0
CC
4
Cov
0%
saveAsync
6.0
setViewerPageLayout
6.0
setViewerPageMode
6.0
CRAP 2.7
1 package SelectPdf::PdfMergeClient;
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::PdfMergeClient - Pdf Merge with SelectPdf Online API.
12
13 =head1 SYNOPSIS
14
15 Merge PDFs from local disk or public url and save result into a file on disk.
16
17 use SelectPdf;
18 print "This is SelectPdf-$SelectPdf::VERSION\n";
19
20 my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf";
21 my $test_pdf = "Input.pdf";
22 my $local_file = "Result.pdf";
23 my $apiKey = "Your API key here";
24
25 eval {
26 my $client = new SelectPdf::PdfMergeClient($apiKey);
27
28 # set parameters - see full list at https://selectpdf.com/pdf-merge-api/
29 $client
30 # specify the pdf files that will be merged (order will be preserved in the final pdf)
31
32 ->addFile($test_pdf) # add PDF from local file
33 ->addUrlFile($test_url) # add PDF From public url
34 #->addFileWithPassword($test_pdf, "pdf_password") # add PDF (that requires a password) from local file
35 #->addUrlFileWithPassword($test_url, "pdf_password") # add PDF (that requires a password) from public url
36 ;
37
38 print "Starting pdf merge ...\n";
39
40 # merge pdfs to local file
41 $client->saveToFile($local_file);
42
43 # merge pdfs to memory
44 # my $pdf = $client->save();
45
46 print "Finished! Number of pages: " . $client->getNumberOfPages() . ".\n";
47
48 # get API usage
49 my $usageClient = new SelectPdf::UsageClient($apiKey);
50 my $usage = $usageClient->getUsage(0);
51 print("Usage: " . encode_json($usage) . "\n");
52 print("Conversions remained this month: ". $usage->{"available"});
53 };
54
55 if ($@) {
56 print "An error occurred: $@\n";
57 }
58
59 For more details and full list of parameters see L<Pdf Merge API|https://selectpdf.com/pdf-merge-api/>.
60
61 =head1 METHODS
62
63 =head2 new( $apiKey )
64
65 Construct the Pdf Merge Client.
66
67 my $client = SelectPdf::PdfMergeClient->new($apiKey);
68
69 Parameters:
70
71 - $apiKey: API Key.
72 =cut
73 sub new {
74 my $type = shift;
75 my $self = $type->SUPER::new;
76
77 # API endpoint
78 $self->{apiEndpoint} = "https://selectpdf.com/api2/pdfmerge/";
79
80 $self->{fileIdx} = 0;
81
82 $self->{parameters}{"key"} = shift;
83
84 bless $self, $type;
85 return $self;
86 }
87
88 =head2 addFile( $inputPdf )
89
90 Add local PDF document to the list of input files.
91
92 my $client = new SelectPdf::PdfMergeClient($apiKey);
93 $client->addFile($inputPdf);
94
95 Parameters:
96
97 - $inputPdf: Path to a local PDF file.
98
99 Returns:
100
101 - Reference to the current object.
102 =cut
103 sub addFile($) {
104 my($self, $inputPdf) = @_;
105
106 $self->{fileIdx} = $self->{fileIdx} + 1;
107
108 $self->{files}{"file_" . $self->{fileIdx}} = $inputPdf;
109 $self->{parameters}{"url_" . $self->{fileIdx}} = "";
110 $self->{parameters}{"password_" . $self->{fileIdx}} = "";
111
112 return $self;
113 }
114
115 =head2 addFileWithPassword( $inputPdf, $userPassword )
116
117 Add local PDF document to the list of input files.
118
119 my $client = new SelectPdf::PdfMergeClient($apiKey);
120 $client->addFileWithPassword($inputPdf, $userPassword);
121
122 Parameters:
123
124 - $inputPdf: Path to a local PDF file.
125
126 - $userPassword: User password for the PDF document.
127
128 Returns:
129
130 - Reference to the current object.
131 =cut
132 sub addFileWithPassword($,$) {
133 my($self, $inputPdf, $userPassword) = @_;
134
135 $self->{fileIdx} = $self->{fileIdx} + 1;
136
137 $self->{files}{"file_" . $self->{fileIdx}} = $inputPdf;
138 $self->{parameters}{"url_" . $self->{fileIdx}} = "";
139 $self->{parameters}{"password_" . $self->{fileIdx}} = $userPassword;
140
141 return $self;
142 }
143
144 =head2 addUrlFile( $inputUrl )
145
146 Add remote PDF document to the list of input files.
147
148 my $client = new SelectPdf::PdfMergeClient($apiKey);
149 $client->addUrlFile($inputUrl);
150
151 Parameters:
152
153 - $inputUrl: Url of a remote PDF file.
154
155 Returns:
156
157 - Reference to the current object.
158 =cut
159 sub addUrlFile($) {
160 my($self, $inputUrl) = @_;
161
162 $self->{fileIdx} = $self->{fileIdx} + 1;
163
164 $self->{files}{"file_" . $self->{fileIdx}} = "";
165 $self->{parameters}{"url_" . $self->{fileIdx}} = $inputUrl;
166 $self->{parameters}{"password_" . $self->{fileIdx}} = "";
167
168 return $self;
169 }
170
171 =head2 addUrlFileWithPassword( $inputUrl, $userPassword )
172
173 Add remote PDF document to the list of input files.
174
175 my $client = new SelectPdf::PdfMergeClient($apiKey);
176 $client->addUrlFileWithPassword($inputUrl, $userPassword);
177
178 Parameters:
179
180 - $inputUrl: Url of a remote PDF file.
181
182 - $userPassword: User password for the PDF document.
183
184 Returns:
185
186 - Reference to the current object.
187 =cut
188 sub addUrlFileWithPassword($,$) {
189 my($self, $inputUrl, $userPassword) = @_;
190
191 $self->{fileIdx} = $self->{fileIdx} + 1;
192
193 $self->{files}{"file_" . $self->{fileIdx}} = "";
194 $self->{parameters}{"url_" . $self->{fileIdx}} = $inputUrl;
195 $self->{parameters}{"password_" . $self->{fileIdx}} = $userPassword;
196
197 return $self;
198 }
199
200 =head2 save
201
202 Merge all specified input pdfs and return the resulted PDF.
203
204 my $client = new SelectPdf::PdfMergeClient($apiKey);
205 $client->addFile($inputPdf1);
206 $client->addFile($inputPdf2);
207 $content = $client->save();
208
209 Returns:
210
211 - Byte array containing the resulted PDF.
212 =cut
213 sub save() {
214 my($self) = @_;
215
216 $self->{parameters}{"async"} = "False";
217 $self->{parameters}{"files_no"} = $self->{fileIdx};
218
219 my $result = $self->SUPER::performPostAsMultipartFormData();
220
221 $self->{fileIdx} = 0;
222 $self->{files} = {};
223
224 return $result;
225 }
226
227 =head2 saveToFile( $filePath )
228
229 Merge all specified input pdfs and writes the resulted PDF to a local file.
230
231 my $client = new SelectPdf::PdfMergeClient($apiKey);
232 $client->addFile($inputPdf1);
233 $client->addFile($inputPdf2);
234 $client->saveToFile($filePath);
235
236 Parameters:
237
238 - $filePath: Local file including path if necessary.
239
240 Returns:
241
242 - Byte array containing the resulted PDF.
243 =cut
244 sub saveToFile($) {
245 my($self, $filePath) = @_;
246
247 my $result = $self->save();
248
249 my $file = IO::File->new( $filePath, '>' ) or die "Unable to open output file - $!\n";
250 $file->binmode;
251 $file->print( $result );
252 $file->close;
253 }
254
255 =head2 saveAsync
256
257 Merge all specified input pdfs and return the resulted PDF.
258
259 my $client = new SelectPdf::PdfMergeClient($apiKey);
260 $client->addFile($inputPdf1);
261 $client->addFile($inputPdf2);
262 $content = $client->saveAsync();
263
264 Returns:
265
266 - Byte array containing the resulted PDF. An asynchronous call is used.
267 =cut
268 sub saveAsync() {
269 my($self) = @_;
270
271 $self->{parameters}{"files_no"} = $self->{fileIdx};
272
273 my $JobID = $self->SUPER::startAsyncJobMultipartFormData() or die "An error occurred launching the asynchronous call.";
274
275 my $noPings = 0;
276
277 do
278 {
279 $noPings++;
280
281 # sleep for a few seconds before next ping
282 sleep($self->{AsyncCallsPingInterval});
283
284 my $asyncJobClient = new SelectPdf::AsyncJobClient($self->{parameters}{"key"}, $JobID);
285 $asyncJobClient->setApiEndpoint($self->{apiAsyncEndpoint});
286
287 my $result = $asyncJobClient->getResult();
288
289 if ($asyncJobClient->finished)
290 {
291 $self->{numberOfPages} = $asyncJobClient->getNumberOfPages();
292
293 $self->{fileIdx} = 0;
294 $self->{files} = {};
295
296 return $result;
297 }
298
299 } while ($noPings <= $self->{AsyncCallsMaxPings});
300
301 $self->{fileIdx} = 0;
302 $self->{files} = {};
303
304 die "Asynchronous call did not finish in expected timeframe.";
305
306 }
307
308 =head2 saveToFileAsync( $filePath )
309
310 Merge all specified input pdfs and writes the resulted PDF to a local file. An asynchronous call is used.
311
312 my $client = new SelectPdf::PdfMergeClient($apiKey);
313 $client->addFile($inputPdf1);
314 $client->addFile($inputPdf2);
315 $client->saveToFileAsync($filePath);
316
317 Parameters:
318
319 - $filePath: Local file including path if necessary.
320
321 Returns:
322
323 - Byte array containing the resulted PDF.
324 =cut
325 sub saveToFileAsync($) {
326 my($self, $filePath) = @_;
327
328 my $result = $self->saveAsync();
329
330 my $file = IO::File->new( $filePath, '>' ) or die "Unable to open output file - $!\n";
331 $file->binmode;
332 $file->print( $result );
333 $file->close;
334 }
335
336
337 =head2 setDocTitle( $docTitle )
338
339 Set the PDF document title.
340
341 Parameters:
342
343 - $docTitle: Document title.
344
345 Returns:
346
347 - Reference to the current object.
348 =cut
349 sub setDocTitle($) {
350 my($self, $docTitle) = @_;
351
352 $self->{parameters}{"doc_title"} = $docTitle;
353 return $self;
354 }
355
356 =head2 setDocSubject( $docSubject )
357
358 Set the PDF document subject.
359
360 Parameters:
361
362 - $docSubject: Document subject.
363
364 Returns:
365
366 - Reference to the current object.
367 =cut
368 sub setDocSubject($) {
369 my($self, $docSubject) = @_;
370
371 $self->{parameters}{"doc_subject"} = $docSubject;
372 return $self;
373 }
374
375 =head2 setDocKeywords( $docKeywords )
376
377 Set the PDF document keywords.
378
379 Parameters:
380
381 - $docKeywords: Document keywords.
382
383 Returns:
384
385 - Reference to the current object.
386 =cut
387 sub setDocKeywords($) {
388 my($self, $docKeywords) = @_;
389
390 $self->{parameters}{"doc_keywords"} = $docKeywords;
391 return $self;
392 }
393
394 =head2 setDocAuthor( $docAuthor )
395
396 Set the PDF document author.
397
398 Parameters:
399
400 - $docAuthor: Document author.
401
402 Returns:
403
404 - Reference to the current object.
405 =cut
406 sub setDocAuthor($) {
407 my($self, $docAuthor) = @_;
408
409 $self->{parameters}{"doc_author"} = $docAuthor;
410 return $self;
411 }
412
413 =head2 setDocAddCreationDate( $docAddCreationDate )
414
415 Add the date and time when the PDF document was created to the PDF document information. The default value is False.
416
417 Parameters:
418
419 - $docAddCreationDate: Add creation date to the document metadata or not.
420
421 Returns:
422
423 - Reference to the current object.
424 =cut
425 sub setDocAddCreationDate($) {
426 my($self, $docAddCreationDate) = @_;
427
428 $self->{parameters}{"doc_add_creation_date"} = $self->SUPER::serializeBoolean($docAddCreationDate);
429 return $self;
430 }
431
432 =head2 setViewerPageLayout( $pageLayout )
433
434 Set the page layout to be used when the document is opened in a PDF viewer. The default value is 1 - OneColumn.
435
436 Parameters:
437
438 - $pageLayout: Page layout. Possible values: 0 (Single Page), 1 (One Column), 2 (Two Column Left), 3 (Two Column Right).
439
440 Returns:
441
442 - Reference to the current object.
443 =cut
444 sub setViewerPageLayout($) {
445 my($self, $pageLayout) = @_;
446
447 if ($pageLayout ne 0 and $pageLayout ne 1 and $pageLayout ne 2 and $pageLayout ne 3) {
448 die ("Allowed values for Page Layout: 0 (Single Page), 1 (One Column), 2 (Two Column Left), 3 (Two Column Right).");
449 }
450
451 $self->{parameters}{"viewer_page_layout"} = $pageLayout;
452 return $self;
453 }
454
455 =head2 setViewerPageMode( $pageMode )
456
457 Set the document page mode when the pdf document is opened in a PDF viewer. The default value is 0 - UseNone.
458
459 Parameters:
460
461 - $pageMode: Page mode. Possible values: 0 (Use None), 1 (Use Outlines), 2 (Use Thumbs), 3 (Full Screen), 4 (Use OC), 5 (Use Attachments).
462
463 Returns:
464
465 - Reference to the current object.
466 =cut
467 sub setViewerPageMode($) {
468 my($self, $pageMode) = @_;
469
470 if ($pageMode ne 0 and $pageMode ne 1 and $pageMode ne 2 and $pageMode ne 3 and $pageMode ne 4 and $pageMode ne 5) {
471 die ("Allowed values for Page Mode: 0 (Use None), 1 (Use Outlines), 2 (Use Thumbs), 3 (Full Screen), 4 (Use OC), 5 (Use Attachments).");
472 }
473
474 $self->{parameters}{"viewer_page_mode"} = $pageMode;
475 return $self;
476 }
477
478 =head2 setViewerCenterWindow( $viewerCenterWindow )
479
480 Set a flag specifying whether to position the document's window in the center of the screen. The default value is False.
481
482 Parameters:
483
484 - $viewerCenterWindow: Center window or not.
485
486 Returns:
487
488 - Reference to the current object.
489 =cut
490 sub setViewerCenterWindow($) {
491 my($self, $viewerCenterWindow) = @_;
492
493 $self->{parameters}{"viewer_center_window"} = $self->SUPER::serializeBoolean($viewerCenterWindow);
494 return $self;
495 }
496
497 =head2 setViewerDisplayDocTitle( $viewerDisplayDocTitle )
498
499 Set a flag specifying whether the window's title bar should display the document title taken from document information. The default value is False.
500
501 Parameters:
502
503 - $viewerDisplayDocTitle: Display title or not.
504
505 Returns:
506
507 - Reference to the current object.
508 =cut
509 sub setViewerDisplayDocTitle($) {
510 my($self, $viewerDisplayDocTitle) = @_;
511
512 $self->{parameters}{"viewer_display_doc_title"} = $self->SUPER::serializeBoolean($viewerDisplayDocTitle);
513 return $self;
514 }
515
516 =head2 setViewerFitWindow( $viewerFitWindow )
517
518 Set a flag specifying whether to resize the document's window to fit the size of the first displayed page. The default value is False.
519
520 Parameters:
521
522 - $viewerFitWindow: Fit window or not.
523
524 Returns:
525
526 - Reference to the current object.
527 =cut
528 sub setViewerFitWindow($) {
529 my($self, $viewerFitWindow) = @_;
530
531 $self->{parameters}{"viewer_fit_window"} = $self->SUPER::serializeBoolean($viewerFitWindow);
532 return $self;
533 }
534
535 =head2 setViewerHideMenuBar( $viewerHideMenuBar )
536
537 Set a flag specifying whether to hide the pdf viewer application's menu bar when the document is active. The default value is False.
538
539 Parameters:
540
541 - $viewerHideMenuBar: Hide menu bar or not.
542
543 Returns:
544
545 - Reference to the current object.
546 =cut
547 sub setViewerHideMenuBar($) {
548 my($self, $viewerHideMenuBar) = @_;
549
550 $self->{parameters}{"viewer_hide_menu_bar"} = $self->SUPER::serializeBoolean($viewerHideMenuBar);
551 return $self;
552 }
553
554 =head2 setViewerHideToolbar( $viewerHideToolbar )
555
556 Set a flag specifying whether to hide the pdf viewer application's tool bars when the document is active. The default value is False.
557
558 Parameters:
559
560 - $viewerHideToolbar: Hide tool bars or not.
561
562 Returns:
563
564 - Reference to the current object.
565 =cut
566 sub setViewerHideToolbar($) {
567 my($self, $viewerHideToolbar) = @_;
568
569 $self->{parameters}{"viewer_hide_toolbar"} = $self->SUPER::serializeBoolean($viewerHideToolbar);
570 return $self;
571 }
572
573 =head2 setViewerHideWindowUI( $viewerHideWindowUI )
574
575 Set a flag specifying whether to hide user interface elements in the document's window (such as scroll bars and navigation controls),
576 leaving only the document's contents displayed.
577
578 Parameters:
579
580 - $viewerHideWindowUI: Hide window UI or not.
581
582 Returns:
583
584 - Reference to the current object.
585 =cut
586 sub setViewerHideWindowUI($) {
587 my($self, $viewerHideWindowUI) = @_;
588
589 $self->{parameters}{"viewer_hide_window_ui"} = $self->SUPER::serializeBoolean($viewerHideWindowUI);
590 return $self;
591 }
592
593 =head2 setUserPassword( $userPassword )
594
595 Set PDF user password.
596
597 Parameters:
598
599 - $userPassword: PDF user password.
600
601 Returns:
602
603 - Reference to the current object.
604 =cut
605 sub setUserPassword($) {
606 my($self, $userPassword) = @_;
607
608 $self->{parameters}{"user_password"} = $userPassword;
609 return $self;
610 }
611
612 =head2 setOwnerPassword( $ownerPassword )
613
614 Set PDF owner password.
615
616 Parameters:
617
618 - $ownerPassword: PDF owner password.
619
620 Returns:
621
622 - Reference to the current object.
623 =cut
624 sub setOwnerPassword($) {
625 my($self, $ownerPassword) = @_;
626
627 $self->{parameters}{"owner_password"} = $ownerPassword;
628 return $self;
629 }
630
631 =head2 setCustomParameter( $parameterName, $parameterValue )
632
633 Set a custom parameter. Do not use this method unless advised by SelectPdf.
634
635 Parameters:
636
637 - $parameterName: Parameter name.
638
639 - $parameterValue: Parameter value.
640
641 Returns:
642
643 - Reference to the current object.
644 =cut
645 sub setCustomParameter($,$) {
646 my($self, $parameterName, $parameterValue) = @_;
647
648 $self->{parameters}{$parameterName} = $parameterValue;
649 return $self;
650 }
651
652 =head2 setTimeout( $timeout )
653
654 Set the maximum amount of time (in seconds) for this job.
655 The default value is 30 seconds.
656 Use a larger value (up to 120 seconds allowed) for pages that take a long time to load.
657
658 Parameters:
659
660 - $timeout: Timeout in seconds.
661
662 Returns:
663
664 - Reference to the current object.
665 =cut
666 sub setTimeout($) {
667 my($self, $timeout) = @_;
668
669 $self->{parameters}{"timeout"} = $timeout;
670 return $self;
671 }
672
673 1;