From bf1a7ca7360e79e5abf6fb70fd1f6ee226a34f92 Mon Sep 17 00:00:00 2001 From: hualin Date: Mon, 23 Sep 2024 17:23:08 +0800 Subject: [PATCH] commit --- Models/PdfRequest.cs | 4 ++-- Program.cs | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Models/PdfRequest.cs b/Models/PdfRequest.cs index ff9fa0b..9bcb5c2 100644 --- a/Models/PdfRequest.cs +++ b/Models/PdfRequest.cs @@ -15,13 +15,13 @@ public class PdfRequest [SwaggerSchema(Format = "binary")] public IFormFile? WatermarkImageFile { get; set; } [EnumDataType(typeof(Position))] - public Position? WatermarkPosition { get; set; } + public Position WatermarkPosition { get; set; }= Position.Center; public string? StampImageUrl { get; set; } [SwaggerSchema(Format = "binary")] public IFormFile? StampImageFile { get; set; } [EnumDataType(typeof(Position))] - public Position? StampPosition { get; set; } + public Position StampPosition { get; set; } = Position.RightBottom; } diff --git a/Program.cs b/Program.cs index 8b28223..e94e190 100644 --- a/Program.cs +++ b/Program.cs @@ -105,7 +105,14 @@ // Return a bad request response if neither URL nor HTML content is provided return Results.BadRequest("A URL or HTML content must be provided"); } - + // **Insert the script to wait for all images to load** + await page.EvaluateAsync(@"() => Promise.all( + Array.from(document.images) + .filter(img => !img.complete) + .map(img => new Promise(resolve => { + img.onload = img.onerror = resolve; + })) + )"); // Hide specified elements based on CSS selectors if (request.HideSelectors != null && request.HideSelectors.Any()) { @@ -155,7 +162,7 @@ // Generate CSS for the watermark async Task GenerateWatermarkCssAsync(PdfRequest request) { - var position = request.WatermarkPosition ?? Position.Center; + var position = request.WatermarkPosition; var opacity = 0.2; // Set a reasonable opacity var positionCss = GetPositionCss(position); @@ -168,7 +175,8 @@ async Task GenerateWatermarkCssAsync(PdfRequest request) content: '{request.WatermarkText}'; position: fixed; {positionCss} - font-size: 50px; + font-size: 5rem; + font-weight: 800; color: rgba(0, 0, 0, {opacity}); pointer-events: none; z-index: 9999; @@ -202,7 +210,7 @@ async Task GenerateWatermarkCssAsync(PdfRequest request) // Generate CSS for the stamp async Task GenerateStampCssAsync(PdfRequest request) { - var position = request.StampPosition ?? Position.RightBottom; + var position = request.StampPosition; // Get the image URL (either from the uploaded file or the provided URL) var imageUrl = await GetImageUrlAsync(request.StampImageFile, request.StampImageUrl); var positionCss = GetPositionCss(position); @@ -213,8 +221,8 @@ async Task GenerateStampCssAsync(PdfRequest request) content: ''; position: fixed; {positionCss} - width: 100px; - height: 100px; + width: 150px; + height: 150px; background: url('{imageUrl}') no-repeat center center; background-size: contain; opacity: 1; @@ -237,8 +245,22 @@ async Task GetImageUrlAsync(IFormFile imageFile, string imageUrl) } else if (!string.IsNullOrEmpty(imageUrl)) { - // Use the provided image URL - return imageUrl; + // Validate the URL by performing a test request to ensure the image can be loaded + try + { + var httpClient = new HttpClient(); + var response = await httpClient.GetAsync(imageUrl); + if (response.IsSuccessStatusCode) + { + // Return the provided image URL if it is valid and reachable + return imageUrl; + } + } + catch + { + // Log or handle error if the URL is unreachable + return string.Empty; // Return empty string if the image URL is invalid + } } return string.Empty; // Return empty string if no image is provided