Files
EnvelopeGenerator/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Services/PdfRenderService.cs
TekH 735f590c60 Add quality control for PDF rendering pipeline
Introduce a `qualityPercent` parameter to control the quality of rendered PDF page images. Updated `IPdfPageImageRenderer` and its implementation to support this parameter, normalizing it within defined bounds and dynamically adjusting rendering DPI.

Updated `PdfRenderController` to accept an optional `qualityPercent` query parameter, propagate it through the pipeline, and include it in cache keys. Modified `PdfRenderService` to construct API requests with the quality parameter.

Added `CreateEnvelopeWizardRenderOptions` with a default `PreviewQualityPercent` of 55% for rendering previews in `CreateEnvelopeWizardPopup`. Adjusted caching logic and cache key format to account for quality-specific results.
2026-11-30 14:04:42 +01:00

42 lines
1.6 KiB
C#

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);
private const int MinQualityPercent = 10;
private const int MaxQualityPercent = 100;
public async Task<IReadOnlyList<PdfPageImageDto>> RenderPagesAsync(byte[] pdfBytes, int? qualityPercent = null, CancellationToken cancel = default)
{
var query = string.Empty;
if (qualityPercent.HasValue)
{
var normalizedQuality = Math.Clamp(qualityPercent.Value, MinQualityPercent, MaxQualityPercent);
query = $"?qualityPercent={normalizedQuality}";
}
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
using var response = await http.PostAsJsonAsync(
$"/api/PdfRender/pages{query}",
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<List<PdfPageImageDto>>(_jsonOptions, cancel) ?? [];
}
}