| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace App\Services;
- use Exception;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Str;
- class PdfConverterClient
- {
- const CONVERTER_ADDRESS = 'http://pdf-converter:5000/convert';
- /**
- * @param string $filePath
- * @return void
- * @throws Exception
- */
- public static function convert(string $filePath): void
- {
- if(!file_exists($filePath)) {
- throw new Exception("File does not exist");
- }
- $file = fopen($filePath, 'rb');
- if ($file === false) {
- throw new Exception("Cant open file");
- }
- try {
- $response = Http::attach('file', $file, '123.xlsx')->post(self::CONVERTER_ADDRESS);
- } finally {
- fclose($file);
- }
- if($response->successful()) {
- $newFilename = Str::replace('.xlsx', '.pdf', $filePath);
- file_put_contents($newFilename, $response->body());
- } else {
- Log::error($response->clientError());
- }
- }
- }
|