2018-03-20
HtmlToPdfOrImage
https://github.com/MRDaghestani/HtmlToPdfOrImage
需要 .NET 4.0 和 Newtonsoft.Json (>= 5.0.8)
新增說明文字 |
(下圖) 實際使用會有問題,錯誤訊息
無法載入檔案或組件'Newtonsoft.Json, Version 6.0.0.0, Culture=neutral
錯誤訊息說 Newtonsoft.Json 至少 6.0 版,但是 HtmlToPdfOrImage 1.0.2說只要 5.0.8 版,安裝新版 Newtonsoft.Json 吧。
<%@ 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" /> </div> </form> </body> </html> |
Default.aspx.cs
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using HtmlToPdfOrImage; 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) { var api = new HtmlToPdfOrImage.Api("99433eae-569c-470d-a763-0a3d0b28ad21", "vmZ2Qryg"); var result = api.Convert("<b>測試 Html to PDF Sample</b>"); //File.WriteAllBytes("myfile.pdf", (byte[])result.model); this.Response.AddHeader("Content-Type", "application/pdf"); this.Response.AddHeader("Content-Disposition", "attachment; filename=SampleGuide.pdf"); this.Response.Flush(); this.Response.BinaryWrite((byte[])result.model); //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 為預設內容,沒有修改。
產生結果
(待續)
沒有留言:
張貼留言