generate images (1-4; no naming format, only alph.)
This commit is contained in:
parent
ba48e513df
commit
138d88918f
@ -61,11 +61,10 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// 1. Fetch core data from database
|
||||||
var tarballData = (from marine in _context.MarineTarballs
|
var tarballData = (from marine in _context.MarineTarballs
|
||||||
join station in _context.MarineStations
|
join station in _context.MarineStations on marine.StationID equals station.StationID
|
||||||
on marine.StationID equals station.StationID
|
join state in _context.States on station.StateID equals state.StateID
|
||||||
join state in _context.States
|
|
||||||
on station.StateID equals state.StateID
|
|
||||||
where marine.Id == id
|
where marine.Id == id
|
||||||
select new
|
select new
|
||||||
{
|
{
|
||||||
@ -85,18 +84,25 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
|
|||||||
}).FirstOrDefault();
|
}).FirstOrDefault();
|
||||||
|
|
||||||
if (tarballData == null)
|
if (tarballData == null)
|
||||||
|
return NotFound("Record not found");
|
||||||
|
|
||||||
|
// 2. Get photos from station folder
|
||||||
|
var stationFolder = Path.Combine(PhotoBasePath, tarballData.StationID);
|
||||||
|
var stationImages = new List<string>();
|
||||||
|
|
||||||
|
if (Directory.Exists(stationFolder))
|
||||||
{
|
{
|
||||||
return NotFound("The specified record was not found.");
|
stationImages = Directory.GetFiles(stationFolder)
|
||||||
|
.Where(f => f.EndsWith(".jpg") || f.EndsWith(".png"))
|
||||||
|
.OrderBy(f => f) // Alphabetical order
|
||||||
|
.Take(4) // Only need 4 photos max
|
||||||
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate photo paths
|
Console.WriteLine($"Found {stationImages.Count} images for {tarballData.StationID}");
|
||||||
var photoBaseName = $"{tarballData.StationID}_{tarballData.DateSample:yyyyMMdd}";
|
|
||||||
var photoPath1 = GetValidPhotoPath(photoBaseName + "_1.jpg");
|
|
||||||
var photoPath2 = GetValidPhotoPath(photoBaseName + "_2.jpg");
|
|
||||||
var photoPath3 = GetValidPhotoPath(photoBaseName + "_3.jpg");
|
|
||||||
var photoPath4 = GetValidPhotoPath(photoBaseName + "_4.jpg");
|
|
||||||
|
|
||||||
var pdfDocument = new TarBallPDF(
|
// 3. Generate PDF
|
||||||
|
var pdf = new TarBallPDF(
|
||||||
tarballData.StateName,
|
tarballData.StateName,
|
||||||
tarballData.StationID,
|
tarballData.StationID,
|
||||||
tarballData.LocationName,
|
tarballData.LocationName,
|
||||||
@ -110,48 +116,47 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
|
|||||||
tarballData.IsSand,
|
tarballData.IsSand,
|
||||||
tarballData.IsNonSandy,
|
tarballData.IsNonSandy,
|
||||||
tarballData.IsCoquina,
|
tarballData.IsCoquina,
|
||||||
photoPath1,
|
stationImages.Count > 0 ? stationImages[0] : null,
|
||||||
photoPath2,
|
stationImages.Count > 1 ? stationImages[1] : null,
|
||||||
photoPath3,
|
stationImages.Count > 2 ? stationImages[2] : null,
|
||||||
photoPath4
|
stationImages.Count > 3 ? stationImages[3] : null
|
||||||
);
|
).GeneratePdf();
|
||||||
|
|
||||||
var pdf = pdfDocument.GeneratePdf();
|
// 4. Return file
|
||||||
Console.WriteLine("PDF generation completed.");
|
return forceDownload
|
||||||
|
? File(pdf, "application/pdf", $"TbReport_{tarballData.StationID}_{tarballData.DateSample:yyyyMMdd}.pdf")
|
||||||
if (forceDownload)
|
: File(pdf, "application/pdf");
|
||||||
{
|
|
||||||
var fileName = $"TbReport_{tarballData.StationID}_{tarballData.DateSample:yyyyMMdd}.pdf";
|
|
||||||
return File(pdf, "application/pdf", fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
return File(pdf, "application/pdf");
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Error in {(forceDownload ? "GenerateReport" : "ViewPDF")}: {ex.Message}");
|
Console.WriteLine($"Error: {ex.Message}");
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError,
|
return StatusCode(500, $"PDF generation failed: {ex.Message}");
|
||||||
$"An error occurred while {(forceDownload ? "generating" : "viewing")} the PDF. {ex.Message}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetValidPhotoPath(string photoName)
|
private static List<string> GetStationPhotos(string folderPath)
|
||||||
{
|
|
||||||
var fullPath = Path.Combine(PhotoBasePath, photoName);
|
|
||||||
return System.IO.File.Exists(fullPath) ? fullPath : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string? FindPhoto(string folderPath, string searchPattern)
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var files = Directory.GetFiles(folderPath, searchPattern);
|
if (!Directory.Exists(folderPath))
|
||||||
return files.Length > 0 ? files[0] : null;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
{
|
||||||
return null;
|
Console.WriteLine($"Folder not found: {folderPath}");
|
||||||
|
return new List<string>(); // Return empty list
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get ALL .jpg/.png files (no date sorting)
|
||||||
|
return Directory.GetFiles(folderPath)
|
||||||
|
.Where(f => f.EndsWith(".jpg") || f.EndsWith(".png"))
|
||||||
|
.OrderBy(f => f) // Optional: Sort alphabetically
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error fetching photos: {ex.Message}");
|
||||||
|
return new List<string>(); // Return empty list on error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -25,19 +25,27 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
|||||||
private readonly bool _isSand = isSand;
|
private readonly bool _isSand = isSand;
|
||||||
private readonly bool _isNonSandy = isNonSandy;
|
private readonly bool _isNonSandy = isNonSandy;
|
||||||
private readonly bool _isCoquina = isCoquina;
|
private readonly bool _isCoquina = isCoquina;
|
||||||
private readonly string _photoPath1 = photoPath1;
|
private readonly string? _photoPath1 = photoPath1;
|
||||||
private readonly string _photoPath2 = photoPath2;
|
private readonly string? _photoPath2 = photoPath2;
|
||||||
private readonly string _photoPath3 = photoPath3;
|
private readonly string? _photoPath3 = photoPath3;
|
||||||
private readonly string _photoPath4 = photoPath4;
|
private readonly string? _photoPath4 = photoPath4;
|
||||||
|
|
||||||
//INSERT LOADIMAGE() HERE
|
//INSERT LOADIMAGE() HERE
|
||||||
private static Image? LoadImage(string path)
|
private Image? LoadImage(string? imagePath)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(path) && File.Exists(path))
|
if (string.IsNullOrEmpty(imagePath)) // Fixed: Added closing )
|
||||||
{
|
{
|
||||||
return Image.FromFile(path); // Load image if the file exists
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Image.FromFile(imagePath);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return Image.FromFile("default-placeholder.jpg"); // Return null if the file is missing
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Metadata for the PDF document
|
// Metadata for the PDF document
|
||||||
@ -101,10 +109,12 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
|||||||
innerRow.RelativeItem(1).Element(CellStyle).Text("02")
|
innerRow.RelativeItem(1).Element(CellStyle).Text("02")
|
||||||
.AlignLeft();
|
.AlignLeft();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
static IContainer CellStyle(IContainer container)
|
static IContainer CellStyle(IContainer container)
|
||||||
=> container.Border(0.5f).Padding(5);
|
=> container.Border(0.5f).Padding(5);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Content Section
|
// Content Section
|
||||||
@ -214,10 +224,12 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
|||||||
|
|
||||||
// Row 1: Photos
|
// Row 1: Photos
|
||||||
table.Cell().Element(CellStyle).Height(150)
|
table.Cell().Element(CellStyle).Height(150)
|
||||||
.Image(LoadImage(_photoPath1)); // Left Side View photo space
|
.Image(LoadImage(_photoPath1) ?? null) // Just pass null if no image
|
||||||
|
.FitArea();
|
||||||
|
|
||||||
table.Cell().Element(CellStyle).Height(150)
|
table.Cell().Element(CellStyle).Height(150)
|
||||||
.Image(LoadImage(_photoPath2)); // Right Side View photo space
|
.Image(LoadImage(_photoPath2) ?? null) // Just pass null if no image
|
||||||
|
.FitArea();
|
||||||
|
|
||||||
// Row 1: Captions
|
// Row 1: Captions
|
||||||
table.Cell().Element(CellStyle)
|
table.Cell().Element(CellStyle)
|
||||||
@ -230,10 +242,12 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
|||||||
|
|
||||||
// Row 2: Photos
|
// Row 2: Photos
|
||||||
table.Cell().Element(CellStyle).Height(150)
|
table.Cell().Element(CellStyle).Height(150)
|
||||||
.Image(LoadImage(_photoPath3)); // Vertical Lines photo space
|
.Image(LoadImage(_photoPath3) ?? null) // Just pass null if no image
|
||||||
|
.FitArea();
|
||||||
|
|
||||||
table.Cell().Element(CellStyle).Height(150)
|
table.Cell().Element(CellStyle).Height(150)
|
||||||
.Image(LoadImage(_photoPath4)); // Horizontal Lines photo space
|
.Image(LoadImage(_photoPath4) ?? null) // Just pass null if no image
|
||||||
|
.FitArea();
|
||||||
|
|
||||||
// Row 2: Captions
|
// Row 2: Captions
|
||||||
table.Cell().Element(CellStyle)
|
table.Cell().Element(CellStyle)
|
||||||
@ -248,6 +262,8 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
|||||||
// Page Break
|
// Page Break
|
||||||
column.Item().PageBreak();
|
column.Item().PageBreak();
|
||||||
|
|
||||||
|
column.Spacing(3);
|
||||||
|
|
||||||
// Additional Photos Section
|
// Additional Photos Section
|
||||||
column.Item().Table(table =>
|
column.Item().Table(table =>
|
||||||
{
|
{
|
||||||
|
|||||||
@ -18,10 +18,10 @@ namespace PSTW_CentralSystem.Models
|
|||||||
public double GetLongitude { get; set; } // Maps to 'getLongitude'
|
public double GetLongitude { get; set; } // Maps to 'getLongitude'
|
||||||
public DateTime Timestamp { get; set; } // Maps to 'timestamp'
|
public DateTime Timestamp { get; set; } // Maps to 'timestamp'
|
||||||
|
|
||||||
public string PhotoPath1 { get; set; } // Left Side Coastal View
|
public required string PhotoPath1 { get; set; } // Left Side Coastal View
|
||||||
public string PhotoPath2 { get; set; } // Right Side Coastal View
|
public required string PhotoPath2 { get; set; } // Right Side Coastal View
|
||||||
public string PhotoPath3 { get; set; } // Vertical Lines
|
public required string PhotoPath3 { get; set; } // Vertical Lines
|
||||||
public string PhotoPath4 { get; set; } // Horizontal Lines
|
public required string PhotoPath4 { get; set; } // Horizontal Lines
|
||||||
|
|
||||||
[ForeignKey("StationID")]
|
[ForeignKey("StationID")]
|
||||||
public required MarineStation MarineStation { get; set; }
|
public required MarineStation MarineStation { get; set; }
|
||||||
|
|||||||
@ -28,6 +28,7 @@
|
|||||||
<PackageReference Include="QuestPDF" Version="2025.1.7" />
|
<PackageReference Include="QuestPDF" Version="2025.1.7" />
|
||||||
<PackageReference Include="QuestPDF.HTML" Version="1.4.2" />
|
<PackageReference Include="QuestPDF.HTML" Version="1.4.2" />
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
|
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
|
||||||
|
<PackageReference Include="SkiaSharp" Version="3.116.1" />
|
||||||
<PackageReference Include="System.Drawing.Common" Version="9.0.3" />
|
<PackageReference Include="System.Drawing.Common" Version="9.0.3" />
|
||||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.3" />
|
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.3" />
|
||||||
<PackageReference Include="Verify.QuestPDF" Version="2.3.0" />
|
<PackageReference Include="Verify.QuestPDF" Version="2.3.0" />
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user