SelectPdf.pm

filter: Statementstmt 100% 11 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 11 / 11 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« SelectPdf/WebElementsClient.pm ↑ Summary SelectPdf/ApiEnums.pm »
1 package SelectPdf;
2
3 1 our $VERSION = '1.6.0';
4
5 1 require SelectPdf::ApiEnums;
6 1 require SelectPdf::DemoExceptions;
7 1 require SelectPdf::HtmlToPdfClient;
8 1 require SelectPdf::InvoiceClient;
9 1 require SelectPdf::PdfMergeClient;
10 1 require SelectPdf::PdfToTextClient;
11 1 require SelectPdf::UsageClient;
12 1 require SelectPdf::WebElementsClient;
13 1 require SelectPdf::AsyncJobClient;
14
15 =head1 NAME
16
17 SelectPdf - SelectPdf Online REST API client library for Perl. Contains HTML to PDF converter, ZUGFeRD / Factur-X electronic invoices, PDF merge, PDF to text extractor, search PDF.
18
19 =head1 SYNOPSIS
20
21 use SelectPdf;
22 print "This is SelectPdf-$SelectPdf::VERSION\n";
23
24 Convert HTML to PDF
25
26 use SelectPdf;
27 print "This is SelectPdf-$SelectPdf::VERSION\n";
28
29 my $url = "https://selectpdf.com/";
30 my $local_file = "Test.pdf";
31 my $apiKey = "Your API key here";
32
33 eval {
34 my $client = SelectPdf::HtmlToPdfClient->new($apiKey);
35
36 $client
37 ->setPageSize("A4")
38 ->setMargins(0)
39 ->setShowPageNumbers('False')
40 ->setPageBreaksEnhancedAlgorithm('True')
41 ;
42
43 $client->convertUrlToFile($url, $local_file);
44 };
45
46 if ($@) {
47 print "An error occurred: $@\n";
48 }
49
50
51 Merge PDFs from local disk or public url and save result into a file on disk.
52
53 use SelectPdf;
54 print "This is SelectPdf-$SelectPdf::VERSION\n";
55
56 my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf";
57 my $test_pdf = "Input.pdf";
58 my $local_file = "Result.pdf";
59 my $apiKey = "Your API key here";
60
61 eval {
62 my $client = new SelectPdf::PdfMergeClient($apiKey);
63
64 # set parameters - see full list at https://selectpdf.com/pdf-merge-api/
65 $client
66 # specify the pdf files that will be merged (order will be preserved in the final pdf)
67
68 ->addFile($test_pdf) # add PDF from local file
69 ->addUrlFile($test_url) # add PDF From public url
70 #->addFileWithPassword($test_pdf, "pdf_password") # add PDF (that requires a password) from local file
71 #->addUrlFileWithPassword($test_url, "pdf_password") # add PDF (that requires a password) from public url
72 ;
73
74 print "Starting pdf merge ...\n";
75
76 # merge pdfs to local file
77 $client->saveToFile($local_file);
78
79 # merge pdfs to memory
80 # my $pdf = $client->save();
81
82 print "Finished! Number of pages: " . $client->getNumberOfPages() . ".\n";
83
84 # get API usage
85 my $usageClient = new SelectPdf::UsageClient($apiKey);
86 my $usage = $usageClient->getUsage(0);
87 print("Usage: " . encode_json($usage) . "\n");
88 print("Conversions remained this month: ". $usage->{"available"});
89 };
90
91 if ($@) {
92 print "An error occurred: $@\n";
93 }
94
95 Extract text from PDF
96
97 use JSON;
98 use SelectPdf;
99
100 print "This is SelectPdf-$SelectPdf::VERSION.\n";
101
102 my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf";
103 my $test_pdf = "Input.pdf";
104 my $local_file = "Test.txt";
105 my $apiKey = "Your API key here";
106
107 eval {
108 my $client = new SelectPdf::PdfToTextClient($apiKey);
109
110 print "Starting pdf to text ...\n";
111
112 # set parameters - see full list at https://selectpdf.com/pdf-to-text-api/
113 $client
114 ->setStartPage(1) # start page (processing starts from here)
115 ->setEndPage(0) # end page (set 0 to process file til the end)
116 ->setOutputFormat(0) # set output format - 0 (Text), 1 (Html)
117 ;
118
119 # convert local pdf to local text file
120 $client->getTextFromFileToFile($test_pdf, $local_file);
121
122 # extract text from local pdf to memory
123 # my $text = $client->getTextFromFile($test_pdf);
124 # print $text;
125
126 # convert pdf from public url to local text file
127 # $client->getTextFromUrlToFile($test_url, $local_file);
128
129 # extract text from pdf from public url to memory
130 # my $text = $client->getTextFromUrl($test_url);
131 # print $text;
132
133 print "Finished! Number of pages processed: " . $client->getNumberOfPages() . ".\n";
134
135 # get API usage
136 my $usageClient = new SelectPdf::UsageClient($apiKey);
137 my $usage = $usageClient->getUsage(0);
138 print("Usage: " . encode_json($usage) . "\n");
139 print("Conversions remained this month: ". $usage->{"available"});
140 };
141
142 if ($@) {
143 print "An error occurred: $@\n";
144 }
145
146 Search PDF
147
148 use JSON;
149 use SelectPdf;
150
151 print "This is SelectPdf-$SelectPdf::VERSION.\n";
152
153 my $test_url = "https://selectpdf.com/demo/files/selectpdf.pdf";
154 my $test_pdf = "Input.pdf";
155 my $apiKey = "Your API key here";
156
157 eval {
158 my $client = new SelectPdf::PdfToTextClient($apiKey);
159
160 print "Starting search pdf ...\n";
161
162 # set parameters - see full list at https://selectpdf.com/pdf-to-text-api/
163 $client
164 ->setStartPage(1) # start page (processing starts from here)
165 ->setEndPage(0) # end page (set 0 to process file til the end)
166 ->setOutputFormat(0) # set output format - 0 (Text), 1 (Html)
167 ;
168
169 # search local pdf
170 my $results = $client->searchFile($test_pdf, "pdf", "True", "True");
171
172 # search pdf from public url
173 # my $results = $client->searchUrl($test_url, "pdf", "True", "True");
174
175 my $count = keys @{$results};
176 print("Number of search results: " . $count . "\n");
177 print("Results: " . encode_json($results) . "\n");
178
179 print "Finished! Number of pages processed: " . $client->getNumberOfPages() . ".\n";
180
181 # get API usage
182 my $usageClient = new SelectPdf::UsageClient($apiKey);
183 my $usage = $usageClient->getUsage(0);
184 print("Usage: " . encode_json($usage) . "\n");
185 print("Conversions remained this month: ". $usage->{"available"});
186 };
187
188 if ($@) {
189 print "An error occurred: $@\n";
190 }
191
192 Convert HTML to PDF with the keyless demo endpoint (no API key; watermarked output, capped at 5 pages)
193
194 use SelectPdf;
195
196 eval {
197 my $client = SelectPdf::HtmlToPdfClient->new(); # undef, "" or "demo" selects the demo endpoint
198
199 $client->convertUrlToFile("https://selectpdf.com/", "Test.pdf");
200
201 print "Mode: " . $client->getMode() . "\n"; # "demo"
202 };
203
204 if (my $err = $@) {
205 if (ref $err && $err->isa('SelectPdf::DemoRateLimitException')) {
206 print "Demo rate limit (" . $err->reason() . "). Retry after " . $err->retryAfter() . "s.\n";
207 }
208 elsif (ref $err && $err->isa('SelectPdf::DemoSafetyException')) {
209 print "Demo safety guard rejected '" . $err->field() . "' (reason=" . $err->reason() . ").\n";
210 }
211 elsif (ref $err && $err->isa('SelectPdf::DemoUnsupportedException')) {
212 print "Feature '" . $err->field() . "' not available in demo mode.\n";
213 }
214 else {
215 print "An error occurred: $err\n";
216 }
217 }
218
219 Tagged (accessible) PDF and PDF/A
220
221 use SelectPdf;
222
223 my $apiKey = "Your API key here";
224
225 eval {
226 my $client = SelectPdf::HtmlToPdfClient->new($apiKey);
227
228 $client
229 ->setTagged('True')
230 ->setDocTitle("Accessible document")
231 ->setDocumentLanguage("en-US")
232 ->setPdfStandard(SelectPdf::PdfStandard::PdfA3A)
233 ;
234
235 $client->convertUrlToFile("https://selectpdf.com/", "Accessible.pdf");
236
237 print "Credits remaining: " . $client->getCreditsRemaining() . " / " . $client->getCreditsTotal() . "\n";
238 };
239
240 if ($@) {
241 print "An error occurred: $@\n";
242 }
243
244 ZUGFeRD / Factur-X electronic invoice
245
246 use SelectPdf;
247
248 my $apiKey = "Your API key here";
249
250 eval {
251 my $client = SelectPdf::InvoiceClient->new($apiKey);
252
253 $client
254 ->setInvoiceXmlFile("factur-x.xml")
255 ->setZugferdProfile(SelectPdf::ZugferdProfile::En16931)
256 ;
257
258 $client->createFromHtmlStringToFile("<h1>Invoice INV-2026-001</h1>", "Invoice.pdf");
259 };
260
261 if ($@) {
262 print "An error occurred: $@\n";
263 }
264
265 =head1 DESCRIPTION
266
267 SelectPdf HTML To PDF Online REST API is a professional solution that lets you create PDF from web pages and raw HTML code in your applications.
268 The API is easy to use and the integration takes only a few lines of code. The generated PDFs are perfect.
269 That makes SelectPdf API the best html to pdf online service that can be used.
270
271 For more details and full list of parameters see L<Html To Pdf API|https://selectpdf.com/html-to-pdf-api/>.
272
273 The distribution contains: SelectPdf::HtmlToPdfClient (HTML to PDF, with a keyless demo mode), SelectPdf::InvoiceClient (ZUGFeRD / Factur-X hybrid electronic invoices),
274 SelectPdf::PdfMergeClient (PDF merge), SelectPdf::PdfToTextClient (PDF to text, search PDF), SelectPdf::UsageClient (API usage),
275 SelectPdf::WebElementsClient and SelectPdf::AsyncJobClient (helpers), the typed demo errors in SelectPdf::DemoExceptions and the value constants in SelectPdf::ApiEnums.
276
277 Every client exposes the response telemetry of the most recent call: getNumberOfPages, getCreditsTotal, getCreditsRemaining, getMode and getExecutionMode.
278
279 =cut
280 1 1;
« SelectPdf/WebElementsClient.pm ↑ Summary SelectPdf/ApiEnums.pm »