日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pechkin:html - pdf 利器

發布時間:2025/3/8 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pechkin:html - pdf 利器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Pechkin 是GitHub上的一個開源項目,可方便將html轉化成pdf文檔,使用也很方便,下面是winform項目中的示例代碼:

using System; using System.Diagnostics; using System.Drawing.Printing; using System.IO; using System.Windows.Forms; using Pechkin; using Pechkin.Synchronized;namespace PdfTest {public partial class Form1 : Form{public Form1(){InitializeComponent();}private void btnCreatePDF_Click(object sender, EventArgs e){SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig().SetMargins(new Margins() { Left = 0, Right = 0, Top = 0, Bottom = 0 }) //設置邊距.SetPaperOrientation(true) //設置紙張方向為橫向.SetPaperSize(ConvertToHundredthsInch(50), ConvertToHundredthsInch(100))); //設置紙張大小50mm * 100mmbyte[] buf = sc.Convert(new ObjectConfig(), this.txtHtml.Text);if (buf == null){MessageBox.Show("Error converting!");return;}try{string fn = Path.GetTempFileName() + ".pdf";FileStream fs = new FileStream(fn, FileMode.Create);fs.Write(buf, 0, buf.Length);fs.Close();Process myProcess = new Process();myProcess.StartInfo.FileName = fn;myProcess.Start();}catch { }}private int ConvertToHundredthsInch(int millimeter){return (int)((millimeter * 10.0) / 2.54);}} }

web項目中也可以使用:

1.??新建一個待打印的頁面,比如index.htm,示例內容如下:

<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>html打印測試</title> <style type="text/css" media="all"> * { margin:0; padding:0; font-size:12px } table { margin:10px; border:2px solid #000; border-collapse:collapse; margin:5px auto } th, td { border:1px solid #000; border-collapse:collapse; padding:3px 5px } h1 { font-size:24px } @media print { .no-print { display: none; } .page-break { page-break-after: always; } } </style> <script type="text/javascript">function createPdf() { window.open("CreatePdf.ashx?html=index.htm");}</script></head> <body> <div class="no-print" style="text-align:center;margin:5px"> <button onClick="createPdf()">導出pdf</button> </div> <table > <thead> <tr> <th colspan="5"> <h1>XXXX報表</h1> </th> </tr> <tr> <th> 序號 </th> <th> 欄目1 </th> <th> 欄目2 </th> <th> 欄目3 </th> <th> 欄目4 </th> </tr> </thead> <tbody> <tr> <td> 1 </td> <td> 數據1 </td> <td> 數據2 </td> <td> 數據3 </td> <td> 數據4 </td> </tr> <tr class="page-break"> <td> 2 </td> <td> 數據1 </td> <td> 數據2 </td> <td> 數據3 </td> <td> 數據4 </td> </tr> <tr> <td> 3 </td> <td> 數據1 </td> <td> 數據2 </td> <td> 數據3 </td> <td> 數據4 </td> </tr> <tr> <td> 4 </td> <td> 數據1 </td> <td> 數據2 </td> <td> 數據3 </td> <td> 數據4 </td> </tr> <tr> <td> 5 </td> <td> 數據1 </td> <td> 數據2 </td> <td> 數據3 </td> <td> 數據4 </td> </tr> </tbody> <tfoot> <tr> <th> 合計: </th> <th> </th> <th> </th> <th> 300.00 </th> <th> 300.00 </th> </tr> </tfoot> </table> </body> </html>

?2、創建一個ashx來生成并輸出pdf

using System; using System.Drawing.Printing; using System.IO; using System.Web; using Pechkin; using Pechkin.Synchronized;namespace PdfWebTest {/// <summary>/// Summary description for CreatePdf/// </summary>public class CreatePdf : IHttpHandler{public void ProcessRequest(HttpContext context){string htmlFile = context.Request["html"];if (!string.IsNullOrWhiteSpace(htmlFile)){string filePath = context.Server.MapPath(htmlFile);if (File.Exists(filePath)){string html = File.ReadAllText(filePath);SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig().SetMargins(new Margins() { Left = 0, Right = 0, Top = 0, Bottom = 0 }) //設置邊距.SetPaperOrientation(true) //設置紙張方向為橫向.SetPaperSize(ConvertToHundredthsInch(50), ConvertToHundredthsInch(100))); //設置紙張大小50mm * 100mmbyte[] buf = sc.Convert(new ObjectConfig(), html);if (buf == null){context.Response.ContentType = "text/plain";context.Response.Write("Error converting!");}try{ context.Response.Clear();//方式1:提示瀏覽器下載pdf //context.Response.AddHeader("content-disposition", "attachment;filename=" + htmlFile + ".pdf");//context.Response.ContentType = "application/octet-stream";//context.Response.BinaryWrite(buf);//方式2:直接在瀏覽器打開pdfcontext.Response.ContentType = "application/pdf";context.Response.OutputStream.Write(buf, 0, buf.Length);context.Response.End();}catch(Exception e) {context.Response.ContentType = "text/plain";context.Response.Write(e.Message);}}}}public bool IsReusable{get{return false;}}private int ConvertToHundredthsInch(int millimeter){return (int)((millimeter * 10.0) / 2.54);}} }

注意事項:部署web項目時,要保證bin目錄下有以下相關dll文件

Common.Logging.dll
libeay32.dll
libgcc_s_dw2-1.dll
mingwm10.dll
Pechkin.dll
Pechkin.Synchronized.dll
ssleay32.dll
wkhtmltox0.dll?

總結

以上是生活随笔為你收集整理的Pechkin:html - pdf 利器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。