mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-01 06:23:16 +00:00
144 lines
No EOL
4.9 KiB
C#
144 lines
No EOL
4.9 KiB
C#
using Amazon.Runtime;
|
|
using Amazon.S3;
|
|
using Azure.Core;
|
|
using Data.SeaHavenIndustries;
|
|
using Microsoft.AspNetCore.Components.Forms;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.VisualBasic;
|
|
using PuppeteerSharp;
|
|
using SeaHavenIndustries.Data.IRepository;
|
|
using SeaHavenIndustries.Helper;
|
|
using SeaHavenIndustries.Helper.ViewModels;
|
|
using SeaHavenIndustries.ViewModel;
|
|
using System.Security.Claims;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace SeaHavenIndustries.Data.Services
|
|
{
|
|
public class PdfService
|
|
{
|
|
private readonly IWebHostEnvironment _hosting;
|
|
public PdfService(IWebHostEnvironment hosting)
|
|
{
|
|
|
|
_hosting = hosting;
|
|
|
|
}
|
|
public async Task<byte[]> GeneratePdf(string htmlContent)
|
|
{
|
|
var chromePath = _hosting.WebRootPath + "\\ChromeEx\\Win_x64_1000083_chrome-win\\chrome-win\\chrome.exe";
|
|
if (chromePath == null)
|
|
{
|
|
// Handle the case where Chrome executable is not found
|
|
throw new FileNotFoundException("Chrome executable not found.");
|
|
}
|
|
|
|
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
|
|
{
|
|
Headless = true,
|
|
Args = new[] { "--no-sandbox" }, // Add this option if running in a Linux environment
|
|
ExecutablePath = chromePath
|
|
});
|
|
|
|
var page = await browser.NewPageAsync();
|
|
await page.SetContentAsync(htmlContent);
|
|
|
|
// Get PDF content as stream
|
|
var pdfStream = await page.PdfStreamAsync();
|
|
|
|
// Convert stream to byte array
|
|
using (var memoryStream = new MemoryStream())
|
|
{
|
|
await pdfStream.CopyToAsync(memoryStream);
|
|
var pdfBytes = memoryStream.ToArray();
|
|
|
|
// Cleanup: Close the browser
|
|
await browser.CloseAsync();
|
|
|
|
return pdfBytes;
|
|
}
|
|
|
|
|
|
}
|
|
//public async Task<byte[]> GeneratePdf(string htmlContent)
|
|
//{
|
|
// Set the path where you want to save the generated PDF temporarily
|
|
// var pdfFilePath = Path.Combine(Path.GetTempPath(), "output.pdf");
|
|
// try
|
|
// {
|
|
// Convert HTML content to PDF
|
|
// using (var writer = new FileStream(pdfFilePath, FileMode.Create))
|
|
// {
|
|
// var pdfWriter = new PdfWriter(writer);
|
|
// var pdf = new PdfDocument(pdfWriter);
|
|
// var converter = new ConverterProperties();
|
|
// HtmlConverter.ConvertToPdf(htmlContent, pdf, converter);
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
|
|
// }
|
|
// Read the generated PDF into a byte array
|
|
// byte[] pdfBytes;
|
|
// using (var fileStream = new FileStream(pdfFilePath, FileMode.Open, FileAccess.Read))
|
|
// using (var memoryStream = new MemoryStream())
|
|
// {
|
|
// await fileStream.CopyToAsync(memoryStream);
|
|
// pdfBytes = memoryStream.ToArray();
|
|
// }
|
|
|
|
// Delete the temporary PDF file
|
|
// File.Delete(pdfFilePath);
|
|
|
|
// return pdfBytes;
|
|
//}
|
|
//public async Task<byte[]> GeneratePdf(string htmlContent)
|
|
//{
|
|
// // Set the path where you want to save the generated PDF temporarily
|
|
// StringBuilder sb = new StringBuilder();
|
|
// sb.Append(htmlContent);
|
|
|
|
// using (MemoryStream memoryStream = new MemoryStream())
|
|
// {
|
|
// PdfWriter writer = new PdfWriter(memoryStream);
|
|
// PdfDocument pdfDoc = new PdfDocument(writer);
|
|
// Document document = new Document(pdfDoc, PageSize.A4);
|
|
|
|
// HtmlConverter.ConvertToPdf(sb.ToString(), pdfDoc);
|
|
|
|
// pdfDoc.Close();
|
|
// byte[] pdfBytes = memoryStream.ToArray();
|
|
|
|
// // Optional: Save the PDF to a file for debugging or other purposes
|
|
// var pdfFilePath = Path.Combine(Path.GetTempPath(), "output.pdf");
|
|
// File.WriteAllBytes(pdfFilePath, pdfBytes);
|
|
|
|
// return pdfBytes;
|
|
// }
|
|
//}
|
|
private static string FindChromeExecutable()
|
|
{
|
|
// Check common installation paths for Chrome
|
|
string[] possiblePaths = {
|
|
@"\ChromeEx\Win_x64_1000083_chrome-win\chrome-win",
|
|
@"C:\Program Files\Google\Chrome\Application\chrome.exe",
|
|
@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
|
|
// Add more paths as needed
|
|
};
|
|
|
|
foreach (var path in possiblePaths)
|
|
{
|
|
if (File.Exists(path))
|
|
{
|
|
return path;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
} |