text将html转换为pdf(代码片段)

author author     2022-11-29     194

关键词:

1. install Select.HtmlToPdf.NetCore

2.in startup.cs
services.AddSingleton<IFileProvider>(
  new PhysicalFileProvider(
      Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")));

3. in controller
#region write pdf
[HttpGet("WritePdf")]
[ProducesResponseType(typeof(ApiResponse), 200)]
[ProducesResponseType(typeof(ApiResponse), 400)]
public IActionResult WritePdf(int orderId, double gst, double priceExclGst, double discount, int userId)

    try
    
        var order = trojantradingDbContext.Orders.Where(x => x.Id == orderId).FirstOrDefault();
        var cart = shoppingCartRepository.GetShoppingCartByID(order.ShoppingCartId, userId);
        var currentUser = userRepository.GetUserByAccount(userId);
        double priceIncGst = priceExclGst + gst;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.Append("<!DOCTYPE html><html lang='en'><head><meta charset='utf-8'><style>");
        stringBuilder.Append(".clearfix:aftercontent:'';display: table;clear: both; acolor: #5D6975;text-decoration: underline; bodyposition: relative;width: 21cm;height: 29.7cm;margin: 0 auto;color: #001028;background: #FFFFFF;font-family: Arial, sans-serif;font-size: 12px;");
        stringBuilder.Append("headerpadding: 10px 0;margin-bottom: 30px;#logotext-align: center;margin-bottom: 10px; font-size: 2em#logo imgwidth: 90px;h1 border-top: 1px solid #5D6975;border-bottom: 1px solid #5D6975;color: #5D6975;font-size: 1.5em;line-height: 1.4em;font-weight: normal;text-align: center;margin: 0 0 20px 0;");
        stringBuilder.Append("#project float: left; font-size: 18px#project span color: #5D6975;text-align:left;display: inline-block;font-size: 0.8em;#company float: right;text-align: right; font-size: 18px#company span color: #5D6975;text-align: right;display: inline-block;font-size: 0.8em;#project div,#company div white-space: nowrap;");
        stringBuilder.Append("table width: 100%;border-collapse: collapse;border-spacing: 0;margin-bottom: 20px;table tr:nth-child(2n-1) td background: #F5F5F5;table th,table td text-align: center;white-space:pre-wrap;word-wrap:break-word;table th padding: 5px 20px;color: #5D6975;border-bottom: 1px solid #C1CED9;font-weight: normal;table .service,table .desc text-align: left;table tdpadding: 20px;text-align: right;table td.service,table td.desc vertical-align: top;table td.unit,table td.qty,table td.total font-size: 1.2em;table td.grand border-top: 1px solid #5D6975;");
        stringBuilder.Append("#notices,#notices .notice color: #5D6975;font-size: 1.2em;footer font-size: 1.5em;color: #5D6975;width: 100%;height: 30px;position: absolute;bottom: 0;border-top: 1px solid #C1CED9;padding: 8px 0;text-align: center;");
        stringBuilder.Append("</style></head>");
        stringBuilder.Append("<body><header class='clearfix'><div id='logo'><p>Trojan Trading Company PTY LTD</p></div>");
        stringBuilder.Append("<h1>Order #" + orderId + " for Customer " + currentUser.Account + "</h1>");
        stringBuilder.Append("<div id='company' class='clearfix'><div>SHIPPING ADDRESS</div><div><span>ACCOUNT:&nbsp;&nbsp;</span>" + currentUser.Account + "</div><div><span>CUSTOMER:&nbsp;&nbsp;</span>" + currentUser.ShippingCustomerName + "</div><div><span>ADDRESS:&nbsp;&nbsp;</span>" + currentUser.ShippingStreetNumber + " " + currentUser.ShippingAddressLine + "</div><div>" + currentUser.ShippingSuburb + ", " + currentUser.ShippingState + ", " + currentUser.ShippingPostCode + "</div><div><span>EMAIL:&nbsp;&nbsp;</span> <a href='" + currentUser.Email + "' target='_blank'>" + currentUser.Email + "</a></div><div><span>PHONE:&nbsp;&nbsp;</span>" + currentUser.Phone + "</div></div>");
        stringBuilder.Append("<div id='project'><div>BILLING ADDRESS</div><div><span>ACCOUNT:&nbsp;&nbsp;</span>" + currentUser.Account + "</div><div><span>CUSTOMER:&nbsp;&nbsp;</span>" + currentUser.BillingCustomerName + "</div><div><span>ADDRESS:&nbsp;&nbsp;</span>" + currentUser.BillingStreetNumber + " " + currentUser.BillingAddressLine + "</div><div>" + currentUser.BillingSuburb + ", " + currentUser.BillingState + ", " + currentUser.BillingPostCode + "</div><div><span>EMAIL:&nbsp;&nbsp;</span> <a href='" + currentUser.Email + "' target='_blank'>" + currentUser.Email + "</a></div><div><span>PHONE:&nbsp;&nbsp;</span>" + currentUser.Phone + "</div></div></header>");
        stringBuilder.Append("<main><table><thead><tr><th class='service' style='width: 10%'>Item Code</th><th class='desc' style='width: 50%'>Product Name</th><th style='width: 10%'>Original Price ex.GST</th><th style='width: 10%'>Buy Price ex.GST</th><th style='width: 10%'>Order Qty</th><th style='width: 10%'>Line Amount ex.GST</th></tr></thead><tbody>");
        foreach (var item in cart.ShoppingItems)
        
            stringBuilder.Append("<tr><td class='service'>" + item.Product.ItemCode + "</td><td class='desc'>#" + item.Product.Id + " " + item.Product.Name + "dfsfdsfdsfdsfdsfdsfdsfsdfsdsadasdasdasdsadasdadasdasdassad</td><td class='total'>$" + item.Product.OriginalPrice + "</td>");
            if (currentUser.Role.ToLower() == "agent")
            
                stringBuilder.Append("<td class='unit'>$" + String.Format("0:0.00", item.Product.AgentPrice) + "</td>");
            
            else if (currentUser.Role.ToLower() == "wholesaler")
            
                stringBuilder.Append("<td class='unit'>$" + String.Format("0:0.00", item.Product.WholesalerPrice) + "</td>");
            
            stringBuilder.Append("<td class='qty'>" + item.Amount + "</td>");
            if (currentUser.Role.ToLower() == "agent")
            
                stringBuilder.Append("<td class='total'>$" + String.Format("0:0.00", item.Product.AgentPrice * item.Amount) + "</td></tr>");
            
            else if (currentUser.Role.ToLower() == "wholesaler")
            
                stringBuilder.Append("<td class='total'>$" + String.Format("0:0.00", item.Product.WholesalerPrice * item.Amount) + "</td></tr>");
            
        
        stringBuilder.Append("<tr><td colspan='5' style='text-align:right'> Payment Method</td><td class='total'>" + String.Format("0", cart.PaymentMethod == "onaccount" ? "On Account" : "Prepayment") + "</td></tr>");
        stringBuilder.Append("<tr><td colspan='5' style='text-align:right'> Total Price Excl.GST</td><td class='total'>$" + String.Format("0:0.00", priceExclGst) + "</td></tr>");
        stringBuilder.Append("<tr><td colspan='5' style='text-align:right'> GST</td><td class='total'>$" + String.Format("0:0.00", gst) + "</td></tr>");
        stringBuilder.Append("<tr><td colspan='5' style='text-align:right'> Total Discount Earned</td><td class='total'>$" + String.Format("0:0.00", discount) + "</td></tr>");
        stringBuilder.Append("<tr><td colspan='5' style='text-align:right' class='grand total'> Total Price Inc.GST</td><td class='grand total'>$" + String.Format("0:0.00", priceIncGst) + "</td></tr>");
        stringBuilder.Append("</tbody></table>");
        stringBuilder.Append("<div id='notices'><div>NOTICE:</div><div class='notice'>A finance charge of 1.5% will be made on unpaid balances after 30 days.</div></div>");
        stringBuilder.Append("</main>");
        stringBuilder.Append("<footer><a href='http://xxxxxxxx' target='_blank'>http://xxxxxxxx</a></footer>");
        stringBuilder.Append("</body></html>");
        string pdfBody = stringBuilder.ToString();
        HtmlToPdf Renderer = new HtmlToPdf();
        var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "order_" + orderId + ".pdf");
        var pdf = Renderer.ConvertHtmlString(pdfBody);
        
        MemoryStream stream = new MemoryStream();
        pdf.Save(stream);
        pdf.Close();
        byte[] docBytes = stream.ToArray();
        return File(docBytes, "application/pdf");
    
    catch (Exception ex)
    
        return Ok(new ApiResponse
        
            Status = "fail",
            Message = ex.Message
        );
    

#endregion

text将网站转换为html(代码片段)

查看详情

使用 itextsharp 将 HTML 转换为 PDF

】使用itextsharp将HTML转换为PDF【英文标题】:ConvertHTMLtoPDFusingitextsharp【发布时间】:2022-03-0721:05:07【问题描述】:当使用itextsharp将html转换为pdf时,使用css为网页应用的样式在转换后的pdf中不起作用。这是我的css代码:<styletype... 查看详情

使用 C# 将 Html 表(文本)转换为图像

】使用C#将Html表(文本)转换为图像【英文标题】:Htmltable(text)toimageusingC#【发布时间】:2010-10-2015:18:09【问题描述】:谁能指出我在C#中的一些示例代码,用于将html表格转换为图像?我知道如何将文本转换为图像,但我需要创... 查看详情

text将en转换为ru(代码片段)

查看详情

text将img转换为svg(代码片段)

查看详情

如何使用 iText 将 HTML 转换为 PDF [重复]

】如何使用iText将HTML转换为PDF[重复]【英文标题】:HowtoconvertHTMLtoPDFusingiText[duplicate]【发布时间】:2013-07-2310:38:16【问题描述】:importjava.io.File;importjava.io.FileOutputStream;importjava.io.OutputStream;importcom.itextpdf.text.Document;impo 查看详情

sh将pdf转换为png(代码片段)

查看详情

sh将pdf转换为jpg(代码片段)

查看详情

java示例代码_使用iText将HTML转换为PDF

java示例代码_使用iText将HTML转换为PDF 查看详情

text将mp4转换为hls(代码片段)

查看详情

html转成pdf下载,支持后台保存(代码片段)

最近有个需求,需要将html转换成pdf并支持下载1.需要两个js库下载 提取码:vab7 <scripttype="text/javascript"src="~/lib/html2canvas.js"></script><scripttype="text/javascript"src="~/lib/jsPdf.debug.js"></ 查看详情

sh文件系统::将jpg转换为pdf(代码片段)

查看详情

将html中的表单内容转换为.pdf格式[重复]

】将html中的表单内容转换为.pdf格式[重复]【英文标题】:Converttheformcontentinhtmlinto.pdfformat[duplicate]【发布时间】:2016-05-0618:21:02【问题描述】:我该怎么做?<form>Firstname:<br><inputtype="text"name="firstname">Lastname:<br><... 查看详情

使用 iTextSharp 将 HTML 样式(点下划线)转换为 PDF

】使用iTextSharp将HTML样式(点下划线)转换为PDF【英文标题】:ConvertHTMLstyle(dottedunderline)toPDFusingiTextSharp【发布时间】:2015-10-0119:31:33【问题描述】:我正在尝试从HTML下方生成PDF,即带有点下划线的文本。(下面是示例实际HTML更... 查看详情

使用nodejs将html转换为pdf

】使用nodejs将html转换为pdf【英文标题】:converthtmlintopdfusingnodejs【发布时间】:2014-03-1911:42:21【问题描述】:我是网络开发的新手。我有一个html页面,其中有一些包含文字和图片的文章。我想将该html页面的内容制作成pdf并使用... 查看详情

text将id_rsa转换为pem文件(代码片段)

查看详情

text将int(使用java,bigendian签名)转换为littleendianunsigned(代码片段)

查看详情

text可以将任何污染转换为无污染值的原型(代码片段)

查看详情