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
|
||||
{
|
||||
// 1. Fetch core data from database
|
||||
var tarballData = (from marine in _context.MarineTarballs
|
||||
join station in _context.MarineStations
|
||||
on marine.StationID equals station.StationID
|
||||
join state in _context.States
|
||||
on station.StateID equals state.StateID
|
||||
join station in _context.MarineStations on marine.StationID equals station.StationID
|
||||
join state in _context.States on station.StateID equals state.StateID
|
||||
where marine.Id == id
|
||||
select new
|
||||
{
|
||||
@ -85,18 +84,25 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
|
||||
}).FirstOrDefault();
|
||||
|
||||
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
|
||||
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");
|
||||
Console.WriteLine($"Found {stationImages.Count} images for {tarballData.StationID}");
|
||||
|
||||
var pdfDocument = new TarBallPDF(
|
||||
// 3. Generate PDF
|
||||
var pdf = new TarBallPDF(
|
||||
tarballData.StateName,
|
||||
tarballData.StationID,
|
||||
tarballData.LocationName,
|
||||
@ -110,48 +116,47 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
|
||||
tarballData.IsSand,
|
||||
tarballData.IsNonSandy,
|
||||
tarballData.IsCoquina,
|
||||
photoPath1,
|
||||
photoPath2,
|
||||
photoPath3,
|
||||
photoPath4
|
||||
);
|
||||
stationImages.Count > 0 ? stationImages[0] : null,
|
||||
stationImages.Count > 1 ? stationImages[1] : null,
|
||||
stationImages.Count > 2 ? stationImages[2] : null,
|
||||
stationImages.Count > 3 ? stationImages[3] : null
|
||||
).GeneratePdf();
|
||||
|
||||
var pdf = pdfDocument.GeneratePdf();
|
||||
Console.WriteLine("PDF generation completed.");
|
||||
|
||||
if (forceDownload)
|
||||
{
|
||||
var fileName = $"TbReport_{tarballData.StationID}_{tarballData.DateSample:yyyyMMdd}.pdf";
|
||||
return File(pdf, "application/pdf", fileName);
|
||||
}
|
||||
|
||||
return File(pdf, "application/pdf");
|
||||
// 4. Return file
|
||||
return forceDownload
|
||||
? File(pdf, "application/pdf", $"TbReport_{tarballData.StationID}_{tarballData.DateSample:yyyyMMdd}.pdf")
|
||||
: File(pdf, "application/pdf");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error in {(forceDownload ? "GenerateReport" : "ViewPDF")}: {ex.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError,
|
||||
$"An error occurred while {(forceDownload ? "generating" : "viewing")} the PDF. {ex.Message}");
|
||||
Console.WriteLine($"Error: {ex.Message}");
|
||||
return StatusCode(500, $"PDF generation failed: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private string GetValidPhotoPath(string photoName)
|
||||
{
|
||||
var fullPath = Path.Combine(PhotoBasePath, photoName);
|
||||
return System.IO.File.Exists(fullPath) ? fullPath : null;
|
||||
}
|
||||
|
||||
private string? FindPhoto(string folderPath, string searchPattern)
|
||||
private static List<string> GetStationPhotos(string folderPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var files = Directory.GetFiles(folderPath, searchPattern);
|
||||
return files.Length > 0 ? files[0] : null;
|
||||
if (!Directory.Exists(folderPath))
|
||||
{
|
||||
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
|
||||
catch (Exception ex)
|
||||
{
|
||||
return null;
|
||||
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 _isNonSandy = isNonSandy;
|
||||
private readonly bool _isCoquina = isCoquina;
|
||||
private readonly string _photoPath1 = photoPath1;
|
||||
private readonly string _photoPath2 = photoPath2;
|
||||
private readonly string _photoPath3 = photoPath3;
|
||||
private readonly string _photoPath4 = photoPath4;
|
||||
private readonly string? _photoPath1 = photoPath1;
|
||||
private readonly string? _photoPath2 = photoPath2;
|
||||
private readonly string? _photoPath3 = photoPath3;
|
||||
private readonly string? _photoPath4 = photoPath4;
|
||||
|
||||
//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
|
||||
@ -101,10 +109,12 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
||||
innerRow.RelativeItem(1).Element(CellStyle).Text("02")
|
||||
.AlignLeft();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
static IContainer CellStyle(IContainer container)
|
||||
=> container.Border(0.5f).Padding(5);
|
||||
|
||||
});
|
||||
|
||||
// Content Section
|
||||
@ -214,10 +224,12 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
||||
|
||||
// Row 1: Photos
|
||||
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)
|
||||
.Image(LoadImage(_photoPath2)); // Right Side View photo space
|
||||
.Image(LoadImage(_photoPath2) ?? null) // Just pass null if no image
|
||||
.FitArea();
|
||||
|
||||
// Row 1: Captions
|
||||
table.Cell().Element(CellStyle)
|
||||
@ -230,10 +242,12 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
||||
|
||||
// Row 2: Photos
|
||||
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)
|
||||
.Image(LoadImage(_photoPath4)); // Horizontal Lines photo space
|
||||
.Image(LoadImage(_photoPath4) ?? null) // Just pass null if no image
|
||||
.FitArea();
|
||||
|
||||
// Row 2: Captions
|
||||
table.Cell().Element(CellStyle)
|
||||
@ -248,6 +262,8 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
||||
// Page Break
|
||||
column.Item().PageBreak();
|
||||
|
||||
column.Spacing(3);
|
||||
|
||||
// Additional Photos Section
|
||||
column.Item().Table(table =>
|
||||
{
|
||||
|
||||
@ -18,10 +18,10 @@ namespace PSTW_CentralSystem.Models
|
||||
public double GetLongitude { get; set; } // Maps to 'getLongitude'
|
||||
public DateTime Timestamp { get; set; } // Maps to 'timestamp'
|
||||
|
||||
public string PhotoPath1 { get; set; } // Left Side Coastal View
|
||||
public string PhotoPath2 { get; set; } // Right Side Coastal View
|
||||
public string PhotoPath3 { get; set; } // Vertical Lines
|
||||
public string PhotoPath4 { get; set; } // Horizontal Lines
|
||||
public required string PhotoPath1 { get; set; } // Left Side Coastal View
|
||||
public required string PhotoPath2 { get; set; } // Right Side Coastal View
|
||||
public required string PhotoPath3 { get; set; } // Vertical Lines
|
||||
public required string PhotoPath4 { get; set; } // Horizontal Lines
|
||||
|
||||
[ForeignKey("StationID")]
|
||||
public required MarineStation MarineStation { get; set; }
|
||||
|
||||
@ -28,6 +28,7 @@
|
||||
<PackageReference Include="QuestPDF" Version="2025.1.7" />
|
||||
<PackageReference Include="QuestPDF.HTML" Version="1.4.2" />
|
||||
<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.Text.Encoding.CodePages" Version="9.0.3" />
|
||||
<PackageReference Include="Verify.QuestPDF" Version="2.3.0" />
|
||||
|
||||
Loading…
Reference in New Issue
Block a user