2018年3月20日 星期二

[研究][ASP.NET]OpenHtmlToPdf v1.12.0 產生 pdf (NuGet 安裝)

[研究][ASP.NET]OpenHtmlToPdf v1.12.0 產生 pdf (NuGet 安裝)

2018-03-20

OpenHtmlToPdf
https://github.com/vilppu/OpenHtmlToPdf

OpenHtmlToPdf  是 .NET library,可把 HTML 文件轉為 PDF 格式。它使用 WkHtmlToPdf native Windows library 來做這件事情。

WkHtmlToPdf native Windows library
https://github.com/wkhtmltopdf/wkhtmltopdf

授權
https://creativecommons.org/licenses/by/3.0/




新增說明文字









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">
        <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" /><br />
        <asp:Button ID="Button3" runat="server" Text="Button" OnClick="Button3_Click" /><br />
        <asp:Button ID="Button4" runat="server" Text="Button" OnClick="Button4_Click" /><br />
        </div>
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenHtmlToPdf;

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)
        {
            const string html =
    "<!DOCTYPE html>" +
    "<html>" +
    "<head><meta charset='UTF-8'><title>Title</title></head>" +
    "<body>測試 Body text...</body>" +
    "</html>";
            var pdf = Pdf.From(html).Content();
            this.Response.AddHeader("Content-Type", "application/pdf");
            this.Response.AddHeader("Content-Disposition", "attachment; filename=SampleGuide.pdf");
            this.Response.Flush();
            this.Response.BinaryWrite(pdf);
            //DocumentContent is a Byte[] of the data to be written in PDF
            this.Response.Flush();
            this.Response.End();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            const string html =
    "<!DOCTYPE html>" +
    "<html>" +
    "<head><meta charset='UTF-8'><title>Title</title></head>" +
    "<body>測試 Body text...</body>" +
    "</html>";

            var pdf = Pdf
                .From(html)
                .OfSize(PaperSize.A4)
                .WithTitle("Title")
                .WithoutOutline()
                .WithMargins(1.25.Centimeters())
                .Portrait()
                .Comressed()
                .Content();

            this.Response.AddHeader("Content-Type", "application/pdf");
            this.Response.AddHeader("Content-Disposition", "attachment; filename=SampleGuide.pdf");
            this.Response.Flush();
            this.Response.BinaryWrite(pdf);
            //DocumentContent is a Byte[] of the data to be written in PDF
            this.Response.Flush();
            this.Response.End();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            const string html =
    "<!DOCTYPE html>" +
    "<html>" +
    "<head><meta charset='UTF-8'><title>Title</title></head>" +
    "<body>測試 Body text...</body>" +
    "</html>";

            var pdf = Pdf
                .From(html)
                .WithGlobalSetting("orientation", "Landscape")
                .WithObjectSetting("web.defaultEncoding", "utf-8")
                .Content();


            this.Response.AddHeader("Content-Type", "application/pdf");
            this.Response.AddHeader("Content-Disposition", "attachment; filename=SampleGuide.pdf");
            this.Response.Flush();
            this.Response.BinaryWrite(pdf);
            //DocumentContent is a Byte[] of the data to be written in PDF
            this.Response.Flush();
            this.Response.End();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            // 把 aspx 轉成 html
            string path= "WebForm1.aspx";
            System.Web.UI.Page page = new Page();
            StringWriter writer = new StringWriter();
            page.Server.Execute(path, writer);
            string html = writer.ToString();

            // 使用 OpenHtmlToPdf 把 html 轉成 pdf
            var pdf = Pdf.From(html).Content();

            // 輸出
            this.Response.AddHeader("Content-Type", "application/pdf");
            this.Response.AddHeader("Content-Disposition", "attachment; filename=SampleGuide.pdf");
            this.Response.Flush();
            this.Response.BinaryWrite(pdf);
            //DocumentContent is a Byte[] of the data to be written in PDF
            this.Response.Flush();
            this.Response.End();
        }
    }
}

WebForm1.aspx

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

<!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">
        <div>
            測試<br />
            TEST
        </div>
    </form>
</body>
</html>


WebForm1.aspx.cs 為預設,沒有改寫。



**********

PS:發現 NuGet 有許多套件都在使用 WkHtmlToPdf 。


(待續)

沒有留言:

張貼留言