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

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

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

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

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

代碼篇

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

下面我們來選擇一個網頁打印下,保存為PDF,而且實現流拋出保存,假設我們選擇“http://m.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://m.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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湘潭市| 嘉峪关市| 宁都县| 东明县| 尚义县| 交城县| 温泉县| 大埔县| 砚山县| 化德县| 叶城县| 临洮县| 赤水市| 达州市| 珲春市| 马边| 祁门县| 蛟河市| 阿克苏市| 抚宁县| 宁德市| 清苑县| 犍为县| 库尔勒市| 五台县| 武胜县| 邵阳市| 黎平县| 镶黄旗| 彩票| 岳普湖县| 松潘县| 克东县| 仁化县| 博野县| 仙游县| 禹州市| 驻马店市| 永仁县| 镇远县| 榆中县|