2024年4月30日 星期二

[研究]ASP.NET,試用 NReco.PdfGenerator 1.2.1 把 html 轉 pdf

[研究]ASP.NET,試用 NReco.PdfGenerator 1.2.1 把 html 轉 pdf

2024-04-30

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

NuGet Gallery | NReco.PdfGenerator 1.2.1
https://www.nuget.org/packages/NReco.PdfGenerator

Convert HTML to PDF in C#/.NET with NReco.PdfGenerator (FREE)
https://www.nrecosite.com/pdf_generator_net.aspx

線上說明
https://www.nrecosite.com/doc/NReco.PdfGenerator/

授權:1. Can I use PdfGenerator in my commercial project for FREE?

NReco.PdfGenerator can be used for FREE in .NET apps that have only one single-server production deployment.

A commercial license is required for:

  • Applications with multiple deployments, or one multi-server production deployment
  • SaaS apps

官方範例

Convert HTML to PDF in your C# code:

var htmlContent = String.Format("<body>Hello world: {0}</body>", 
		DateTime.Now);
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);

Create PDF from HTML file or URL:

htmlToPdf.GeneratePdfFromFile("http://www.nrecosite.com/", null, "export.pdf");  

********************************************************************************

NuGet 安裝 NReco.PdfGenerator 1.2.1 並沒有相依套件。

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </form>
</body>
</html>



Default.aspx.cs

using System;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
			System.Text.StringBuilder html = new System.Text.StringBuilder();
			
			html.Append("<html>");
			html.Append("<head>");
			html.Append("<meta charset='utf-8' />");
			html.Append("<title>無標題文件</title>");
			html.Append(@"<style>
body {
	margin: 0px;
	padding: 0px;
}

.Paper {
	width: 1000px;
	margin-right: auto;
	margin-left: auto;
}
</style>"); html.Append("</head>");
			html.Append("<body>");
			html.Append("<div class='Paper'>");
			// h1 不可有「報名表」,否則產出的 .pdf 索引會變成「報吊表」
			//html.Append("<h1 style ='text-align:center'>報名表</h1>");
			// 若用 h1、h2、、等,產生的 .pdf 會有書籤定位點,「報名表」在書籤定位點會顯示成「報吊表」,故改用 div
			html.Append("<div style ='text-align:center; font-size:xx-large'>報名表</div>");

			string FName = "test.pdf";

			var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
			var pdfBytes = htmlToPdf.GeneratePdf(html.ToString());
			//Response給用戶端下載
			Response.Clear();
			Response.AddHeader("content-disposition", "attachment; filename=" + FName);//強制下載
			Response.ContentType = "application/octet-stream";
			Response.BinaryWrite(pdfBytes);
		}
	}
}




(完)

相關

沒有留言:

張貼留言