|
| 1 | +#!/usr/bin/env perl |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | +use utf8; |
| 5 | +use FindBin qw($Bin); |
| 6 | +use File::Basename qw(basename); |
| 7 | +use JSON::PP qw(decode_json); |
| 8 | +use LWP::UserAgent; |
| 9 | +use HTTP::Request; |
| 10 | +use HTTP::Request::Common qw(POST); |
| 11 | +use URI::Escape qw(uri_escape); |
| 12 | +use Dotenv; |
| 13 | + |
| 14 | +#! |
| 15 | +# What this sample does: |
| 16 | +# - Merges multiple inputs (PDFs and non-PDFs) into a single PDF. |
| 17 | +# - Non-PDFs are converted to PDF; PDFs are uploaded. Collected IDs are merged via /merged-pdf. |
| 18 | +# |
| 19 | +# Setup (.env): |
| 20 | +# - Copy .env.example to .env (Perl folder root) |
| 21 | +# - Set PDFREST_API_KEY=your_api_key_here |
| 22 | +# - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use: |
| 23 | +# PDFREST_URL=https://eu-api.pdfrest.com |
| 24 | +# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work |
| 25 | +# |
| 26 | +# Usage: |
| 27 | +# perl "Complex Flow Examples/merge-different-file-types.pl" /path/to/file1 /path/to/file2 [/path/to/file3 ...] |
| 28 | +# |
| 29 | +# Output: |
| 30 | +# - Prints the API JSON response to stdout. Non-2xx responses exit with a concise message. |
| 31 | +# - Tip: pipe output to a file: perl ... > response.json |
| 32 | + |
| 33 | +binmode STDOUT, ':raw'; |
| 34 | +binmode STDERR, ':encoding(UTF-8)'; |
| 35 | + |
| 36 | +# Load .env from the Perl folder root (one level up from this script) |
| 37 | +my $env_path = "$Bin/../.env"; |
| 38 | +-e $env_path and Dotenv->load($env_path); |
| 39 | + |
| 40 | +my $api_key = $ENV{PDFREST_API_KEY} // ''; |
| 41 | +if (!$api_key || $api_key =~ /^\s*$/) { |
| 42 | + print STDERR "Missing PDFREST_API_KEY in .env or environment\n"; |
| 43 | + exit 1; |
| 44 | +} |
| 45 | + |
| 46 | +my $api_base = $ENV{PDFREST_URL} // $ENV{PDFREST_API} // 'https://api.pdfrest.com'; |
| 47 | +$api_base =~ s{/+$}{}; |
| 48 | + |
| 49 | +my @paths = @ARGV; |
| 50 | +if (@paths < 2) { |
| 51 | + print STDERR "Usage: perl merge-different-file-types.pl /path/to/file1 /path/to/file2 [/path/to/file3 ...]\n"; |
| 52 | + exit 1; |
| 53 | +} |
| 54 | +for my $p (@paths) { if (!-f $p) { print STDERR "Not a file: $p\n"; exit 1; } } |
| 55 | + |
| 56 | +sub content_type_for { |
| 57 | + my ($path) = @_; |
| 58 | + my ($ext) = $path =~ /(\.[^.]+)$/; |
| 59 | + $ext = lc($ext // ''); |
| 60 | + return 'application/pdf' if $ext eq '.pdf'; |
| 61 | + return 'image/png' if $ext eq '.png'; |
| 62 | + return 'image/jpeg' if $ext eq '.jpg' || $ext eq '.jpeg'; |
| 63 | + return 'image/gif' if $ext eq '.gif'; |
| 64 | + return 'image/tiff' if $ext eq '.tif' || $ext eq '.tiff'; |
| 65 | + return 'image/bmp' if $ext eq '.bmp'; |
| 66 | + return 'image/webp' if $ext eq '.webp'; |
| 67 | + return 'application/msword' if $ext eq '.doc'; |
| 68 | + return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' if $ext eq '.docx'; |
| 69 | + return 'application/vnd.ms-powerpoint' if $ext eq '.ppt'; |
| 70 | + return 'application/vnd.openxmlformats-officedocument.presentationml.presentation' if $ext eq '.pptx'; |
| 71 | + return 'application/vnd.ms-excel' if $ext eq '.xls'; |
| 72 | + return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' if $ext eq '.xlsx'; |
| 73 | + return 'text/plain' if $ext eq '.txt'; |
| 74 | + return 'application/rtf' if $ext eq '.rtf'; |
| 75 | + return 'text/html' if $ext eq '.html' || $ext eq '.htm'; |
| 76 | + return 'application/octet-stream'; |
| 77 | +} |
| 78 | + |
| 79 | +my $ua = LWP::UserAgent->new( timeout => 120 ); |
| 80 | + |
| 81 | +eval { |
| 82 | + my @ids; |
| 83 | + for my $i (0..$#paths) { |
| 84 | + my $p = $paths[$i]; |
| 85 | + my $ext = lc(($p =~ /(\.[^.]+)$/)[0] // ''); |
| 86 | + if ($ext eq '.pdf') { |
| 87 | + # Upload and capture id |
| 88 | + open my $fh, '<:raw', $p or do { print STDERR "Unable to read $p: $!\n"; exit 1; }; |
| 89 | + my $bytes; { local $/; $bytes = <$fh>; } |
| 90 | + close $fh; |
| 91 | + my $req = HTTP::Request->new('POST', "$api_base/upload"); |
| 92 | + $req->header('api-key' => $api_key); |
| 93 | + $req->header('content-filename' => basename($p)); |
| 94 | + $req->header('Content-Type' => 'application/octet-stream'); |
| 95 | + $req->content($bytes); |
| 96 | + my $resp = $ua->request($req); |
| 97 | + print STDERR $resp->decoded_content // ''; |
| 98 | + if (!$resp->is_success) { print STDERR "\nUpload failed (input #" . ($i+1) . ") status " . $resp->code . "\n"; exit 1; } |
| 99 | + my $json = decode_json($resp->decoded_content // '{}'); |
| 100 | + my $id = $json->{files} && ref $json->{files} eq 'ARRAY' ? $json->{files}[0]{id} : undef; |
| 101 | + if (!$id) { print STDERR "Unexpected upload response format for input #" . ($i+1) . "\n"; exit 1; } |
| 102 | + push @ids, $id; |
| 103 | + print STDERR "Uploaded PDF (#" . ($i+1) . "); id=$id\n"; |
| 104 | + } else { |
| 105 | + # Convert to PDF via /pdf and capture outputId |
| 106 | + my $ct = content_type_for($p); |
| 107 | + my $req = POST("$api_base/pdf", |
| 108 | + 'Content_Type' => 'form-data', |
| 109 | + 'Content' => [ file => [$p, basename($p), 'Content-Type' => $ct] ] |
| 110 | + ); |
| 111 | + $req->header('api-key' => $api_key); |
| 112 | + my $resp = $ua->request($req); |
| 113 | + print STDERR $resp->decoded_content // ''; |
| 114 | + if (!$resp->is_success) { print STDERR "\nConversion failed (input #" . ($i+1) . ") status " . $resp->code . "\n"; exit 1; } |
| 115 | + my $json = decode_json($resp->decoded_content // '{}'); |
| 116 | + my $id = $json->{outputId}; |
| 117 | + if (!$id) { print STDERR "Unexpected conversion response format for input #" . ($i+1) . "\n"; exit 1; } |
| 118 | + push @ids, $id; |
| 119 | + print STDERR "Converted non-PDF (#" . ($i+1) . "); outputId=$id\n"; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + # Build x-www-form-urlencoded with repeated arrays |
| 124 | + my @parts; |
| 125 | + for my $id (@ids) { |
| 126 | + push @parts, 'id[]=' . uri_escape($id); |
| 127 | + push @parts, 'pages[]=' . uri_escape('1-last'); |
| 128 | + push @parts, 'type[]=id'; |
| 129 | + } |
| 130 | + my $body = join('&', @parts); |
| 131 | + |
| 132 | + my $merge_req = HTTP::Request->new('POST', "$api_base/merged-pdf"); |
| 133 | + $merge_req->header('api-key' => $api_key); |
| 134 | + $merge_req->header('Content-Type' => 'application/x-www-form-urlencoded'); |
| 135 | + $merge_req->content($body); |
| 136 | + my $merge_resp = $ua->request($merge_req); |
| 137 | + print STDOUT $merge_resp->decoded_content // ''; |
| 138 | + if (!$merge_resp->is_success) { print STDERR "\nMerge failed with status " . $merge_resp->code . "\n"; exit 1; } |
| 139 | + 1; |
| 140 | +} or do { |
| 141 | + my $err = $@ || 'Unknown error'; |
| 142 | + $err =~ s/\s+$//; |
| 143 | + print STDERR "Error: $err\n"; |
| 144 | + exit 1; |
| 145 | +}; |
| 146 | + |
| 147 | +__END__ |
0 commit comments