Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Sep 23, 2024
1 parent 111a82e commit bf1a7ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Models/PdfRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

38 changes: 30 additions & 8 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down Expand Up @@ -155,7 +162,7 @@
// Generate CSS for the watermark
async Task<string> 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);
Expand All @@ -168,7 +175,8 @@ async Task<string> 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;
Expand Down Expand Up @@ -202,7 +210,7 @@ async Task<string> GenerateWatermarkCssAsync(PdfRequest request)
// Generate CSS for the stamp
async Task<string> 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);

Check warning on line 215 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'imageFile' in 'Task<string> GetImageUrlAsync(IFormFile imageFile, string imageUrl)'.

Check warning on line 215 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'imageUrl' in 'Task<string> GetImageUrlAsync(IFormFile imageFile, string imageUrl)'.

Check warning on line 215 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'imageFile' in 'Task<string> GetImageUrlAsync(IFormFile imageFile, string imageUrl)'.

Check warning on line 215 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'imageUrl' in 'Task<string> GetImageUrlAsync(IFormFile imageFile, string imageUrl)'.
var positionCss = GetPositionCss(position);
Expand All @@ -213,8 +221,8 @@ async Task<string> 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;
Expand All @@ -237,8 +245,22 @@ async Task<string> 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
Expand Down

0 comments on commit bf1a7ca

Please sign in to comment.