Можете ли вы мне помочь?
Код: Выделить всё
[HttpGet]
public IActionResult Create()
{
var model = new GstInvoiceViewModel
{
InvoiceNo = "A0001",
InvoiceDate = DateTime.Today,
Items = new List
{
new GstInvoiceItemViewModel { ItemName = "", Quantity = 1, Rate = 0, GstRate = 18 }
}
};
return View(model);
}
[HttpPost]
public async Task Create(GstInvoiceViewModel model)
{
var htmlContent = await RazorTemplateEngine.RenderAsync("/Views/Invoice/InvoiceTemplate.cshtml", model);
var pdfDoc = new HtmlToPdfDocument
{
GlobalSettings = new GlobalSettings
{
PaperSize = PaperKind.A4,
Orientation = Orientation.Portrait,
Margins = new MarginSettings { Top = 10, Bottom = 10 }
},
Objects = {
new ObjectSettings
{
HtmlContent = htmlContent,
WebSettings = { DefaultEncoding = "utf-8" }
}
}
};
var pdfBytes = _converter.Convert(pdfDoc); // This will now work
return File(pdfBytes, "application/pdf", $"GST-Invoice-{model.InvoiceNo}.pdf");
}
@model InvoiceGenerator.Models.GstInvoiceViewModel
@using System.IO
@using System
@{
Layout = null;
// Safely convert wwwroot path to forward-slash format for use in file:// links
var baseUrl = "https://localhost:7193"; // or your app's actual base URL/port
string cssPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "assets", "css", "style.css");
string cssContent = System.IO.File.Exists(cssPath) ? System.IO.File.ReadAllText(cssPath) : "";
string imagePath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "assets", "img", "logos", "logo.png");
string imageBase64 = System.IO.File.Exists(imagePath)
? Convert.ToBase64String(System.IO.File.ReadAllBytes(imagePath))
: "";
}
INVO - Invoice HTML5 Template
@*
*@
@cssContent
[img]data:image/png;base64,@imageBase64[/img]
Invoice
Invoice Number @Model.InvoiceNo
Invoice Date @Model.InvoiceDate.ToString("dd MMM yyyy")
Invoice To
@Model.ToBusinessNameGSTIN: @Model.ToGst
PAN: @Model.ToPan
Invoice From
@Model.FromBusinessNameGSTIN: @Model.FromGst
PAN: @Model.FromPan
No.
Item Description
Price
Quantity
Amount
@for (int i = 0; i < Model.Items.Count; i++)
{
var item = Model.Items;
@(i + 1)
@item.ItemName
@item.Rate.ToString("F2")
@item.Quantity
@item.Total.ToString("F2")
}
Terms & Conditions
@Model.Terms
Sub Total@Model.TotalAmount.ToString("F2")
CGST@Model.TotalCgst.ToString("F2")
SGST@Model.TotalSgst.ToString("F2")
Grand Total@Model.GrandTotal.ToString("F2")
@*
*@
@*
*@
@* *@
< /code>
Сначала я пытался использовать HTML, но это не сработало. Не работал, для меня. Я пытаюсь, но все еще не так, как ожидалось.
Подробнее здесь: https://stackoverflow.com/questions/796 ... nktopdflib
Мобильная версия