|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RenderPdfIoPhp; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use Throwable; |
| 7 | + |
| 8 | +class RenderPdfIoService |
| 9 | +{ |
| 10 | + public function __construct( |
| 11 | + /** |
| 12 | + * Your RenderPDF.io API Key |
| 13 | + * |
| 14 | + * Get one here |
| 15 | + * @see https://renderpdf.io/app/api-keys |
| 16 | + */ |
| 17 | + protected readonly string $apiKey, |
| 18 | + |
| 19 | + /** |
| 20 | + * Don't set this param on your own, we do this to help us to write Unit Tests |
| 21 | + * |
| 22 | + * @internal |
| 23 | + */ |
| 24 | + protected ?Client $customClient = null |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + public static function make(string $apiKey): self |
| 29 | + { |
| 30 | + return new self($apiKey); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Convert HTML to PDF file using sync mode |
| 35 | + * |
| 36 | + * @param RenderPdfOptions $options |
| 37 | + * |
| 38 | + * @return string |
| 39 | + * |
| 40 | + * @throws RenderPdfIoException on errors (validation, generic) |
| 41 | + */ |
| 42 | + public function render(RenderPdfOptions $options): string |
| 43 | + { |
| 44 | + try { |
| 45 | + $res = $this->createHttpClient() |
| 46 | + ->post('render-sync', [ |
| 47 | + 'json' => $options->toRequestData(), |
| 48 | + ]); |
| 49 | + |
| 50 | + $body = json_decode((string) $res->getBody(), true); |
| 51 | + |
| 52 | + return $body['fileUrl']; |
| 53 | + } catch (Throwable $exception) { |
| 54 | + throw RenderPdfIoException::fromException($exception); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Convert HTML to PDF file using sync mode |
| 60 | + * @note you need to create a webhook URL before using this mode https://renderpdf.io/app/webhooks |
| 61 | + * |
| 62 | + * @param RenderPdfOptions $options |
| 63 | + * |
| 64 | + * @return bool true on success |
| 65 | + * |
| 66 | + * @throws RenderPdfIoException on errors (validation, generic) |
| 67 | + */ |
| 68 | + public function renderAsync(RenderPdfOptions $options): bool |
| 69 | + { |
| 70 | + if (!$options->identifier) { |
| 71 | + throw RenderPdfIoException::forMissingIdentifierForAsyncFlow(); |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + $res = $this->createHttpClient() |
| 76 | + ->post('render-sync', [ |
| 77 | + 'json' => $options->toRequestData(), |
| 78 | + ]); |
| 79 | + |
| 80 | + $body = json_decode((string) $res->getBody(), true); |
| 81 | + |
| 82 | + return $body['outcome'] === 'SUCCESS'; |
| 83 | + } catch (Throwable $exception) { |
| 84 | + throw RenderPdfIoException::fromException($exception); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + protected function createHttpClient(): Client |
| 89 | + { |
| 90 | + return $this->customClient ?? new Client([ |
| 91 | + 'base_uri' => 'https://renderpdf.io/api/pdfs', |
| 92 | + 'headers' => [ |
| 93 | + 'Authorization' => 'Bearer ' . $this->apiKey, |
| 94 | + 'User-Agent' => 'RenderPdfIoHTTP/1.0', |
| 95 | + 'Content-Type' => 'application/json', |
| 96 | + ], |
| 97 | + ]); |
| 98 | + } |
| 99 | +} |
0 commit comments