国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

wkhtmtopdf--高分辨率HTML轉PDF(三)

2019-11-15 02:30:02
字體:
來源:轉載
供稿:網友

wkhtmtopdf--高分辨率HTML轉PDF(三)

代碼篇

瀏覽了很多實例,總找不到既能把HTML保存為PDF,同時實現流拋出的,所以自己琢磨了許久,終于實現了這樣兩個需求的結合體,下面來貢獻一下吧~~

下面我們來選擇一個網頁打印下,保存為PDF,而且實現流拋出保存,假設我們選擇“http://www.survivalescaperooms.com/ITGirl00/

頁面截圖如:

image

目標:我們需要做出上面這個效果的PDF。

1.步驟

  • 首先新建一個項目HTMLtoPDFOutPutStream
  • 新建目錄output;作為臨時輸出目錄
  • 新建resoure目錄,用于保存wkhtmltopdf.exe等各個組件
  • 接著添加一個WebForm1.aspx,在頁面上添加一個按鈕
  • 最后在按鈕的點擊事件上寫代碼

2.按鈕的點擊處理代碼:

  string fileName = Guid.NewGuid().ToString();
            string outputPath = Server.MapPath("output");
            string savepath = string.Format(outputPath + "http://" + fileName + ".pdf");//最終保存
            string url = "http://www.survivalescaperooms.com/ITGirl00/";
            try
            {
                if (!string.IsNullOrEmpty(url) || !string.IsNullOrEmpty(savepath))
                {
                    PRocess p = new Process();
                    string resource = HttpContext.Current.Server.MapPath("resoure");
                    string dllstr = string.Format(resource + "http://wkhtmltopdf.exe");
                    if (System.IO.File.Exists(dllstr))
                    {
                        p.StartInfo.FileName = dllstr;
                        p.StartInfo.Arguments = " /"" + url + "/"  /"" + savepath + "/"";
                        p.StartInfo.UseShellExecute = false;
                        p.StartInfo.RedirectStandardInput = true;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.StartInfo.RedirectStandardError = true;
                        p.StartInfo.CreateNoWindow = true;
                        p.Start();
                        p.WaitForExit();
                        try
                        {
                            FileStream fs = new FileStream(savepath, FileMode.Open);
                            byte[] file = new byte[fs.Length];
                            fs.Read(file, 0, file.Length);
                            fs.Close();
                            Response.Clear();
                            Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".pdf");//強制下載
                            Response.ContentType = "application/octet-stream";
                            Response.BinaryWrite(file);
                        }
                        catch (Exception ee)
                        {
                            throw new Exception(ee.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
3.效果圖image===小小提示===

(1)使用wkhtmltopdf時,PDF保存的文件夾不能有非Ansi字符,如中文、日文等,且轉換gb2312、韓文charset、日文charset等非utf-8/ansi等網頁時,會出現亂碼

(2)網頁上圖片無法正確顯示是由于圖片有鏈接

Technorati 標簽: wkhtmtopdf,HTMLTOPDF,HTML轉換PDF,outputStream
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 布尔津县| 汉中市| 桂东县| 尉犁县| 高淳县| 东乌珠穆沁旗| 海门市| 融水| 翁牛特旗| 阳西县| 邵阳市| 西安市| 旬邑县| 蓬莱市| 沾益县| 长白| 金湖县| 洪江市| 巢湖市| 蕉岭县| 涞水县| 凌源市| 惠安县| 四平市| 顺义区| 湖口县| 南丰县| 始兴县| 旌德县| 武义县| 略阳县| 济宁市| 芦山县| 永福县| 邯郸市| 禄丰县| 康平县| 卢氏县| 衡水市| 贵德县| 天等县|