128 lines
4.9 KiB
C#
128 lines
4.9 KiB
C#
using DinkToPdf;
|
|
using DinkToPdf.Contracts;
|
|
using QuestPDF.Fluent;
|
|
using QuestPDF.Helpers;
|
|
using QuestPDF.Infrastructure;
|
|
using Unit = QuestPDF.Infrastructure.Unit;
|
|
|
|
namespace NetinaShop.Core.EntityServices.OrderHandlers;
|
|
|
|
public class GetOrderInvoiceCommandHandler : IRequestHandler<GetOrderInvoiceCommand, byte[]>
|
|
{
|
|
private readonly IRepositoryWrapper _repositoryWrapper;
|
|
private readonly IMediator _mediator;
|
|
private readonly IConverter _converter;
|
|
|
|
public GetOrderInvoiceCommandHandler(IRepositoryWrapper repositoryWrapper,IMediator mediator,IConverter converter)
|
|
{
|
|
_repositoryWrapper = repositoryWrapper;
|
|
_mediator = mediator;
|
|
_converter = converter;
|
|
}
|
|
public async Task<byte[]> Handle(GetOrderInvoiceCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var orderLDto = await _mediator.Send(new GetOrderLDtoQuery(request.OrderId), cancellationToken);
|
|
|
|
//var textStyleWithFallback = TextStyle
|
|
// .Default
|
|
// .FontSize(18)
|
|
// .Fallback(y => y
|
|
// .FontFamily(Fonts.Arial)
|
|
// .SemiBold()
|
|
// .Underline(false)
|
|
// .BackgroundColor(Colors.Red.Lighten4));
|
|
|
|
//var pdf = Document.Create(container =>
|
|
//{
|
|
// container.Page(page =>
|
|
// {
|
|
// page.Size(PageSizes.A4);
|
|
// page.Margin(2,Unit.Centimetre);
|
|
// page.PageColor(Colors.White);
|
|
// page.DefaultTextStyle(x=>x.FontSize(14));
|
|
|
|
// page.Header()
|
|
// .Text(text =>
|
|
// {
|
|
// text.DefaultTextStyle(textStyleWithFallback);
|
|
|
|
// text.Line("This is normal text.");
|
|
// text.EmptyLine();
|
|
|
|
// text.Line("Following line should use font fallback:");
|
|
// text.Line("中文文本");
|
|
// text.EmptyLine();
|
|
|
|
// text.Line("The following line contains a mix of known and unknown characters.");
|
|
// text.Line("Mixed line: This 中文 is 文文 a mixed 本 本 line 本 中文文本!");
|
|
// text.EmptyLine();
|
|
|
|
// text.Span("Emojis work out of the box because of font fallback: 😊😅🥳👍❤😍👌");
|
|
// });
|
|
|
|
// page.Content()
|
|
// .ContentFromRightToLeft()
|
|
// .Column(x =>
|
|
// {
|
|
// x.Spacing(20);
|
|
// x.Item().Text("فاکتور فروش ");
|
|
// });
|
|
// });
|
|
//}).GeneratePdf();
|
|
|
|
var sb = new StringBuilder();
|
|
sb.Append(@"
|
|
<html>
|
|
<head>
|
|
</head>
|
|
<body>
|
|
<div class='header'><h1>فاکتور فروش</h1></div>
|
|
<table align='center'>
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>LastName</th>
|
|
<th>Age</th>
|
|
<th>Gender</th>
|
|
</tr>");
|
|
foreach (var emp in orderLDto.OrderProducts)
|
|
{
|
|
sb.AppendFormat(@"<tr>
|
|
<td>{0}</td>
|
|
<td>{1}</td>
|
|
<td>{2}</td>
|
|
<td>{3}</td>
|
|
</tr>", emp.ProductName, emp.Count, emp.ProductFee, emp.ProductCost);
|
|
}
|
|
sb.Append(@"
|
|
</table>
|
|
</body>
|
|
</html>");
|
|
|
|
var globalSettings = new GlobalSettings
|
|
{
|
|
ColorMode = ColorMode.Color,
|
|
Orientation = Orientation.Portrait,
|
|
PaperSize = PaperKind.A4,
|
|
Margins = new MarginSettings { Top = 10 },
|
|
DocumentTitle = "PDF Report",
|
|
Out = @"D:\PDFCreator\Employee_Report.pdf"
|
|
};
|
|
var objectSettings = new ObjectSettings
|
|
{
|
|
PagesCount = true,
|
|
HtmlContent = sb.ToString(),
|
|
WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") },
|
|
HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true },
|
|
FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" }
|
|
};
|
|
var pdf = new HtmlToPdfDocument()
|
|
{
|
|
GlobalSettings = globalSettings,
|
|
Objects = { objectSettings }
|
|
};
|
|
var file = _converter.Convert(pdf);
|
|
|
|
|
|
return file;
|
|
}
|
|
} |