Skip to content

Commit

Permalink
set pdf service to be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
hoapooh committed Jul 26, 2024
1 parent 150fb25 commit c1e86f7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ namespace Dental_Clinic_System.Areas.Dentist.Controllers
[Area("Dentist")]
public class AppointmentPdfController : Controller
{
private readonly PdfService _pdfService;
//private readonly PdfService _pdfService;
private readonly DentalClinicDbContext _context;
IWebHostEnvironment _webHostEnvironment;

public AppointmentPdfController(PdfService pdfService, DentalClinicDbContext context, IWebHostEnvironment webHostEnvironment)
//public AppointmentPdfController(PdfService pdfService, DentalClinicDbContext context, IWebHostEnvironment webHostEnvironment)
//{
// _pdfService = pdfService;
// _context = context;
// _webHostEnvironment = webHostEnvironment;
//}

public AppointmentPdfController(DentalClinicDbContext context, IWebHostEnvironment webHostEnvironment)
{
_pdfService = pdfService;
_context = context;
_webHostEnvironment = webHostEnvironment;
}
Expand Down Expand Up @@ -159,11 +165,11 @@ public IActionResult GeneratePdf(int dentistID, int appointmentID, string? ketqu
";
#endregion

byte[] pdf = _pdfService.GeneratePdf(htmlContent);
//byte[] pdf = _pdfService.GeneratePdf(htmlContent);
string fileName = $"donkham_{appointmentID}.pdf";
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "pdf", fileName);

System.IO.File.WriteAllBytes(filePath, pdf);
//System.IO.File.WriteAllBytes(filePath, pdf);

//return File(pdf, "application/pdf", "appointment.pdf"); //Trả về file pdf
return Redirect($"/pdf/{fileName}");
Expand Down
120 changes: 60 additions & 60 deletions Dental_Clinic_System/Dental_Clinic_System/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@

//});

//Soyu: Cấu hình DinkToPDF
var context = new CustomAssemblyLoadContext();
var wkHtmlToPdfPath = Path.Combine(Directory.GetCurrentDirectory(), "wkhtmltox", "libwkhtmltox.dll");
context.LoadUnmanagedLibrary(wkHtmlToPdfPath);
////Soyu: Cấu hình DinkToPDF
//var context = new CustomAssemblyLoadContext();
//var wkHtmlToPdfPath = Path.Combine(Directory.GetCurrentDirectory(), "wkhtmltox", "libwkhtmltox.dll");
//context.LoadUnmanagedLibrary(wkHtmlToPdfPath);

//Soyu: Thêm Singleton cho phần xuất pdf
builder.Services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
////Soyu: Thêm Singleton cho phần xuất pdf
//builder.Services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

//Soyu: Đăng Ký PDFService
builder.Services.AddSingleton<PdfService>();
////Soyu: Đăng Ký PDFService
//builder.Services.AddSingleton<PdfService>();

var app = builder.Build();

Expand Down Expand Up @@ -225,55 +225,55 @@

app.Run();

#region Class CustomAssemblyLoadContext
public class CustomAssemblyLoadContext : AssemblyLoadContext
{
public IntPtr LoadUnmanagedLibrary(string absolutePath)
{
return LoadUnmanagedDll(absolutePath);
}

protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
return LoadUnmanagedDllFromPath(unmanagedDllName);
}

protected override Assembly Load(AssemblyName assemblyName)
{
throw new NotImplementedException();
}
}
#endregion

#region Class PdfService
public class PdfService
{
private readonly IConverter _converter;

public PdfService(IConverter converter)
{
_converter = converter;
}

public byte[] GeneratePdf(string html)
{
var doc = new HtmlToPdfDocument()
{
GlobalSettings = {
ColorMode = DinkToPdf.ColorMode.Color,
Orientation = DinkToPdf.Orientation.Portrait,
PaperSize = DinkToPdf.PaperKind.A4
},
Objects = {
new ObjectSettings() {
PagesCount = null,
HtmlContent = html,
WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/assets/css", "appointmentpdf.css") },
}
}
};

return _converter.Convert(doc);
}
}
#endregion
//#region Class CustomAssemblyLoadContext
//public class CustomAssemblyLoadContext : AssemblyLoadContext
//{
// public IntPtr LoadUnmanagedLibrary(string absolutePath)
// {
// return LoadUnmanagedDll(absolutePath);
// }

// protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
// {
// return LoadUnmanagedDllFromPath(unmanagedDllName);
// }

// protected override Assembly Load(AssemblyName assemblyName)
// {
// throw new NotImplementedException();
// }
//}
//#endregion

//#region Class PdfService
//public class PdfService
//{
// private readonly IConverter _converter;

// public PdfService(IConverter converter)
// {
// _converter = converter;
// }

// public byte[] GeneratePdf(string html)
// {
// var doc = new HtmlToPdfDocument()
// {
// GlobalSettings = {
// ColorMode = DinkToPdf.ColorMode.Color,
// Orientation = DinkToPdf.Orientation.Portrait,
// PaperSize = DinkToPdf.PaperKind.A4
// },
// Objects = {
// new ObjectSettings() {
// PagesCount = null,
// HtmlContent = html,
// WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/assets/css", "appointmentpdf.css") },
// }
// }
// };

// return _converter.Convert(doc);
// }
//}
//#endregion

0 comments on commit c1e86f7

Please sign in to comment.