using System.Net.Http.Json; using System.Text.Json; using EnvelopeGenerator.Application.Common.Dto; namespace EnvelopeGenerator.Server.Client.Services; public class PdfRenderService(IHttpClientFactory httpClientFactory) { private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web); public async Task> RenderPagesAsync(byte[] pdfBytes, CancellationToken cancel = default) { using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server"); using var response = await http.PostAsJsonAsync( "/api/PdfRender/pages", new PdfRenderRequestDto { PdfBytes = pdfBytes }, _jsonOptions, cancel); if (!response.IsSuccessStatusCode) { var statusCode = (int)response.StatusCode; var reasonPhrase = response.ReasonPhrase ?? "Unknown error"; throw new HttpRequestException( $"Failed to render PDF pages. Status: {statusCode} ({reasonPhrase})", null, response.StatusCode); } return await response.Content.ReadFromJsonAsync>(_jsonOptions, cancel) ?? []; } }