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.
This commit is contained in:
2026-11-30 14:04:42 +01:00
parent af24bb61ee
commit 735f590c60
6 changed files with 34 additions and 11 deletions

View File

@@ -769,7 +769,9 @@
try
{
_documentPagePreviews = (await PdfRenderService.RenderPagesAsync(_documentBytes))
_documentPagePreviews = (await PdfRenderService.RenderPagesAsync(
_documentBytes,
CreateEnvelopeWizardRenderOptions.PreviewQualityPercent))
.OrderBy(p => p.PageNumber)
.ToList();
}

View File

@@ -20,4 +20,9 @@ public sealed record CreateEnvelopeWizardResult(
byte[]? DocumentBytes,
string? DocumentFileName);
public static class CreateEnvelopeWizardRenderOptions
{
public const int PreviewQualityPercent = 55;
}
public sealed record CreateEnvelopeWizardReceiver(string FullName, string EmailAddress);

View File

@@ -7,12 +7,21 @@ 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, CancellationToken cancel = default)
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",
$"/api/PdfRender/pages{query}",
new PdfRenderRequestDto { PdfBytes = pdfBytes },
_jsonOptions,
cancel);

View File

@@ -14,28 +14,31 @@ namespace EnvelopeGenerator.Server.Controllers;
public class PdfRenderController(IPdfPageImageRenderer renderer, IMemoryCache memoryCache) : ControllerBase
{
private static readonly TimeSpan RenderCacheDuration = TimeSpan.FromMinutes(5);
private const int MinQualityPercent = 10;
private const int MaxQualityPercent = 100;
// TODO(perf): Replace JSON/base64 transport for PdfBytes with application/octet-stream (or multipart) and stream-based request parsing to reduce payload size and allocation pressure for large PDFs.
[HttpPost("pages")]
[AllowAnonymous]
public async Task<IActionResult> RenderPages([FromBody] PdfRenderRequestDto? request, CancellationToken cancel)
public async Task<IActionResult> RenderPages([FromBody] PdfRenderRequestDto? request, [FromQuery] int? qualityPercent, CancellationToken cancel)
{
if (request?.PdfBytes is not { Length: > 0 } bytes)
return BadRequest("Missing PDF bytes.");
var cacheKey = BuildCacheKey(bytes);
var normalizedQuality = Math.Clamp(qualityPercent ?? 100, MinQualityPercent, MaxQualityPercent);
var cacheKey = BuildCacheKey(bytes, normalizedQuality);
if (memoryCache.TryGetValue(cacheKey, out IReadOnlyList<PdfPageImageDto>? cachedPages) && cachedPages is not null)
return Ok(cachedPages);
IReadOnlyList<PdfPageImageDto> pages = await renderer.RenderPagesAsImagesAsync(bytes, cancel);
IReadOnlyList<PdfPageImageDto> pages = await renderer.RenderPagesAsImagesAsync(bytes, normalizedQuality, cancel);
memoryCache.Set(cacheKey, pages, RenderCacheDuration);
return Ok(pages);
}
private static string BuildCacheKey(byte[] pdfBytes)
private static string BuildCacheKey(byte[] pdfBytes, int qualityPercent)
{
var hash = Convert.ToHexString(SHA256.HashData(pdfBytes));
return $"pdf-render:pages:v1:dpi110:{hash}";
return $"pdf-render:pages:v2:q{qualityPercent}:{hash}";
}
}