PdfConverterClient.php 879 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Services;
  3. use Exception;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Str;
  7. class PdfConverterClient
  8. {
  9. const CONVERTER_ADDRESS = 'http://pdf-converter:5000/convert';
  10. /**
  11. * @param string $filePath
  12. * @return void
  13. * @throws Exception
  14. */
  15. public static function convert(string $filePath): void
  16. {
  17. if(!file_exists($filePath)) {
  18. throw new Exception("File does not exist");
  19. }
  20. $response = Http::attach('file', file_get_contents($filePath), '123.xlsx')->post(self::CONVERTER_ADDRESS);
  21. if($response->successful()) {
  22. $newFilename = Str::replace('.xlsx', '.pdf', $filePath);
  23. file_put_contents($newFilename, $response->body());
  24. } else {
  25. Log::error($response->clientError());
  26. }
  27. }
  28. }