|
9 | 9 |
|
10 | 10 | ## Installation
|
11 | 11 |
|
12 |
| -The library is available on [Packagist](https://packagist.org/packages/codeinc/services-cloud-client). The recommended way to install it is via Composer: |
| 12 | +The library is available on [Packagist](https://packagist.org/packages/codeinc/document-cloud-client). The recommended way to install it is via Composer: |
13 | 13 |
|
14 | 14 | ```bash
|
15 |
| -composer require codeinc/services-cloud-client |
| 15 | +composer require codeinc/document-cloud-client |
16 | 16 | ```
|
17 | 17 |
|
18 | 18 | ## Available APIs
|
@@ -71,53 +71,221 @@ $client->isSupported("a-file.pdf"); // returns false
|
71 | 71 |
|
72 | 72 | ### Pdf2Img API
|
73 | 73 |
|
74 |
| -This API allows you to convert PDF documents to images. For more information see [this documentation](https://github.com/codeinchq/pdf2img-php-client?tab=readme-ov-file#usage). |
| 74 | +This API allows you to convert PDF documents to images. |
75 | 75 |
|
76 |
| -The Pdf2Img client can be accessed using: |
| 76 | +#### Base example: |
| 77 | +```php |
| 78 | +use CodeInc\Pdf2ImgClient\Pdf2ImgClient; |
| 79 | +use CodeInc\Pdf2ImgClient\Exception; |
| 80 | + |
| 81 | +$apiBaseUri = 'http://localhost:3000/'; |
| 82 | +$localPdfPath = '/path/to/local/file.pdf'; |
| 83 | + |
| 84 | +try { |
| 85 | + $client = new Pdf2ImgClient($apiBaseUri); |
77 | 86 |
|
| 87 | + // convert |
| 88 | + $image = $client->convert( |
| 89 | + $client->createStreamFromFile($localPdfPath) |
| 90 | + ); |
| 91 | + |
| 92 | + // display the image |
| 93 | + header('Content-Type: image/webp'); |
| 94 | + echo (string)$image; |
| 95 | +} |
| 96 | +catch (Exception $e) { |
| 97 | + // handle exception |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +#### With options: |
78 | 102 | ```php
|
79 |
| -use CodeInc\ServicesCloud\Client; |
| 103 | +use CodeInc\Pdf2ImgClient\Pdf2ImgClient; |
| 104 | +use CodeInc\Pdf2ImgClient\ConvertOptions; |
| 105 | + |
| 106 | +$apiBaseUri = 'http://localhost:3000/'; |
| 107 | +$localPdfPath = '/path/to/local/file.pdf'; |
| 108 | +$destinationPath = '/path/to/destination/file.jpg'; |
| 109 | +$convertOption = new ConvertOptions( |
| 110 | + format: 'jpg', |
| 111 | + page: 3, |
| 112 | + density: 300, |
| 113 | + height: 800, |
| 114 | + width: 800, |
| 115 | + background: 'red', |
| 116 | + quality: 90, |
| 117 | +); |
80 | 118 |
|
81 |
| -// Create a new client |
82 |
| -$servicesCloudClient = new Client('my api key'); |
| 119 | +try { |
| 120 | + $client = new Pdf2ImgClient($apiBaseUri); |
83 | 121 |
|
84 |
| -// Convert a stream using the Pdf2Img API |
85 |
| -$response = $servicesCloudClient->pdf2Img()->convert(/* a PDF stream */); |
| 122 | + // convert |
| 123 | + $image = $client->convertLocalFile( |
| 124 | + $client->createStreamFromFile($localPdfPath), |
| 125 | + $convertOption |
| 126 | + ); |
| 127 | + |
| 128 | + // saves the image to a file |
| 129 | + $client->saveStreamToFile($image, $destinationPath); |
| 130 | +} |
| 131 | +catch (Exception $e) { |
| 132 | + // handle exception |
| 133 | +} |
86 | 134 | ```
|
87 | 135 |
|
88 | 136 | ### Pdf2Txt API
|
89 | 137 |
|
90 |
| -This API allows you to convert PDF documents to text. For more information see [this documentation](https://github.com/codeinchq/pdf2txt-php-client?tab=readme-ov-file#usage). |
| 138 | +This API allows you to convert PDF documents to text. |
| 139 | + |
| 140 | +#### Extracting text from a local file: |
| 141 | +```php |
| 142 | +use CodeInc\Pdf2TxtClient\Pdf2TxtClient; |
| 143 | +use CodeInc\Pdf2TxtClient\Exception; |
| 144 | + |
| 145 | +$apiBaseUri = 'http://localhost:3000/'; |
| 146 | +$localPdfPath = '/path/to/local/file.pdf'; |
91 | 147 |
|
92 |
| -The Pdf2Txt client can be accessed using: |
| 148 | +try { |
| 149 | + // convert |
| 150 | + $client = new Pdf2TxtClient($apiBaseUri); |
| 151 | + $stream = $client->extract( |
| 152 | + $client->createStreamFromFile($localPdfPath) |
| 153 | + ); |
| 154 | + |
| 155 | + // display the text |
| 156 | + echo (string)$stream; |
| 157 | +} |
| 158 | +catch (Exception $e) { |
| 159 | + // handle exception |
| 160 | +} |
| 161 | +``` |
93 | 162 |
|
| 163 | +#### With additional options: |
94 | 164 | ```php
|
95 |
| -use CodeInc\ServicesCloud\Client; |
| 165 | +use CodeInc\Pdf2TxtClient\Pdf2TxtClient; |
| 166 | +use CodeInc\Pdf2TxtClient\ConvertOptions; |
| 167 | +use CodeInc\Pdf2TxtClient\Format; |
96 | 168 |
|
97 |
| -// Create a new client |
98 |
| -$servicesCloudClient = new Client('my api key'); |
| 169 | +$apiBaseUri = 'http://localhost:3000/'; |
| 170 | +$localPdfPath = '/path/to/local/file.pdf'; |
| 171 | +$convertOption = new ConvertOptions( |
| 172 | + firstPage: 2, |
| 173 | + lastPage: 3, |
| 174 | + format: Format::json |
| 175 | +); |
99 | 176 |
|
100 |
| -// Extract text using the Pdf2Txt API |
101 |
| -$response = $servicesCloudClient->pdf2Txt()->extract(/* a PDF stream */); |
| 177 | +try { |
| 178 | + $client = new Pdf2TxtClient($apiBaseUri); |
| 179 | + |
| 180 | + // convert |
| 181 | + $jsonResponse = $client->extract( |
| 182 | + $client->createStreamFromFile($localPdfPath), |
| 183 | + $convertOption |
| 184 | + ); |
| 185 | + |
| 186 | + // display the text in a JSON format |
| 187 | + $decodedJson = $client->processJsonResponse($jsonResponse); |
| 188 | + var_dump($decodedJson); |
| 189 | +} |
| 190 | +catch (Exception $e) { |
| 191 | + // handle exception |
| 192 | +} |
102 | 193 | ```
|
103 | 194 |
|
104 |
| -### Watermarker API |
| 195 | +#### Saving the extracted text to a file: |
| 196 | +```php |
| 197 | +use CodeInc\Pdf2TxtClient\Pdf2TxtClient; |
| 198 | +use CodeInc\Pdf2TxtClient\ConvertOptions; |
| 199 | +use CodeInc\Pdf2TxtClient\Format; |
| 200 | + |
| 201 | +$apiBaseUri = 'http://localhost:3000/'; |
| 202 | +$localPdfPath = '/path/to/local/file.pdf'; |
| 203 | +destinationTextPath = '/path/to/local/file.txt'; |
| 204 | + |
| 205 | +try { |
| 206 | + $client = new Pdf2TxtClient($apiBaseUri); |
105 | 207 |
|
106 |
| -This API allows you to add a watermark to a PDF document. For more information see [this documentation](https://github.com/codeinchq/watermarker-php-client?tab=readme-ov-file#usage). |
| 208 | + // convert |
| 209 | + $stream = $client->extract( |
| 210 | + $client->createStreamFromFile($localPdfPath) |
| 211 | + ); |
| 212 | + |
| 213 | + // save the text to a file |
| 214 | + $client->saveStreamToFile($stream, $destinationTextPath); |
| 215 | +} |
| 216 | +catch (Exception $e) { |
| 217 | + // handle exception |
| 218 | +} |
| 219 | +``` |
107 | 220 |
|
108 |
| -The Watermarker client can be accessed using: |
| 221 | +### Watermarker API |
109 | 222 |
|
| 223 | +This API allows you to add a watermark to a PDF document. |
| 224 | + |
| 225 | +#### A simple scenario to apply a watermark to an image and display the result: |
110 | 226 | ```php
|
111 |
| -use CodeInc\ServicesCloud\Client; |
| 227 | +use CodeInc\WatermarkerClient\WatermarkerClient; |
| 228 | +use CodeInc\WatermarkerClient\Exception; |
112 | 229 |
|
113 |
| -// Create a new client |
114 |
| -$servicesCloudClient = new Client('my api key'); |
| 230 | +$apiBaseUri = 'http://localhost:3000/'; |
| 231 | +$anImage = '/path/to/local/image.png'; |
| 232 | +$theWatermark = '/path/to/local/watermark.png'; |
115 | 233 |
|
116 |
| -// Apply a watermark using the Watermarker API |
117 |
| -$response = $servicesCloudClient->watermarker()->apply( |
118 |
| - /* an image stream*/, |
119 |
| - /* a PDF stream */ |
| 234 | +try { |
| 235 | + $client = new WatermarkerClient($apiBaseUri); |
| 236 | + |
| 237 | + // apply the watermark |
| 238 | + $watermarkedImageStream = $client->apply( |
| 239 | + $client->createStreamFromFile($anImage), |
| 240 | + $client->createStreamFromFile($theWatermark), |
| 241 | + ); |
| 242 | + |
| 243 | + // display the watermarked image |
| 244 | + header('Content-Type: image/png'); |
| 245 | + echo (string)$watermarkedImageStream; |
| 246 | +} |
| 247 | +catch (Exception $e) { |
| 248 | + // handle exception |
| 249 | +} |
| 250 | +``` |
| 251 | + |
| 252 | +#### A mire complex scenario to apply a watermark to an image with options and save the result to a file: |
| 253 | +```php |
| 254 | +use CodeInc\WatermarkerClient\WatermarkerClient; |
| 255 | +use CodeInc\WatermarkerClient\ConvertOptions; |
| 256 | +use CodeInc\WatermarkerClient\Position; |
| 257 | +use CodeInc\WatermarkerClient\Format; |
| 258 | + |
| 259 | +$apiBaseUri = 'http://localhost:3000/'; |
| 260 | +$theImageStream = '/path/to/local/image.png'; |
| 261 | +$theWatermarkStream = '/path/to/local/watermark.png'; |
| 262 | +$theDestinationFile = '/path/to/local/destination.png'; |
| 263 | +$convertOption = new ConvertOptions( |
| 264 | + size: 50, |
| 265 | + position: Position::topRight, |
| 266 | + format: Format::jpg, |
| 267 | + quality: 80, |
| 268 | + blur: 3, |
| 269 | + opacity: 75 |
120 | 270 | );
|
| 271 | + |
| 272 | +try { |
| 273 | + $streamFactory = Psr17FactoryDiscovery::findStreamFactory(); |
| 274 | + $client = new WatermarkerClient($apiBaseUri); |
| 275 | + |
| 276 | + // apply the watermark |
| 277 | + $watermarkedImageStream = $client->apply( |
| 278 | + $client->createStreamFromFile($theImageStream), |
| 279 | + $client->createStreamFromFile($theWatermarkStream), |
| 280 | + $convertOption |
| 281 | + ); |
| 282 | + |
| 283 | + // save the watermarked image |
| 284 | + $client->saveStreamToFile($watermarkedImageStream, $theDestinationFile); |
| 285 | +} |
| 286 | +catch (Exception $e) { |
| 287 | + // handle exception |
| 288 | +} |
121 | 289 | ```
|
122 | 290 |
|
123 | 291 | ### Gotenberg API (legacy)
|
|
0 commit comments