.Net 把網頁Html轉PDF文件

.Net 把網頁Html轉PDF文件

此篇主要利用 wkhtmltopdf  進行轉換。html

 

1、控制檯直接轉換app

首先到官網http://wkhtmltopdf.org/下載wkhtmltopdf  ,下後主要有3個文件,asp.net

 

wkhtmltoimage.exe 主要是把URL轉成圖片ui

wkhtmltopdf.exe  主要是把 URL 轉成PDFurl

 

咱們只要打CMD. 輸入 wkhtmltopdf http://oschina.net cc.pdf 就會在當前目前產生一個cc.pdf 文件。spa

 

 

2、asp.net 包裝 wkhtmltopdf .net

 

項目結構code

 

 

主要代碼:orm

 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HtmltoX.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult index(string url, string type)
        {
            return View();
        }


        [HttpPost]
        public ActionResult Gen(string url, string type)
        {

            if (string.IsNullOrWhiteSpace(url))
            {
                return Content("參數異常...");
            }

            string contentType = "application/pdf";
            string ext = ".pdf";
            string folder = "Pdfs";
            string genExe = "wkhtmltopdf.exe ";

            if ("image".Equals(type, StringComparison.InvariantCultureIgnoreCase))
            {
                contentType = "image/jpeg";
                ext = ".jpg";
                folder = "Images";
                genExe = "wkhtmltoimage.exe ";
            }

            var rootUrl = Server.MapPath("/");

            var file = rootUrl + @"AutoGen\wkhtmltoX\" + genExe;

            string fName = Guid.NewGuid().ToString();

            string flileName = rootUrl + "AutoGen/" + folder + "/" + fName + ext;

            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();

                startInfo.FileName = file;
                startInfo.Arguments = string.Format(" {0} {1}", url, flileName);
                startInfo.CreateNoWindow = true;
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                var cc = Process.Start(startInfo);

                cc.WaitForExit();
                cc.Close();
            }
            catch
            {
                return Content("生成失敗. 請多試幾回...");
            }

            if (System.IO.File.Exists(flileName))
            {
                var f=new FileStreamResult(new FileStream(flileName, FileMode.Open), contentType);
                f.FileDownloadName = fName+ext;

                return f;
            }

            return Content("生成失敗. 請多試幾回...");
        }

    }
}

 

 

3、      Pechkinhtm

 

Pechkin主要是把 wkhtmltopdf  所用的DLL文件做了C#的封裝。

 

Nuget地址:

https://www.nuget.org/packages/Pechkin.Synchronized/

https://www.nuget.org/packages/Pechkin/ 

安裝: 

Install-Package Pechkin

Install-Package Pechkin.Synchronized 

 

 

using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pechkin;
using Pechkin.Synchronized;

namespace MvcApplication1.Controllers
{
    public class DefaultController : Controller
    {
        //
        // GET: /Default/

        public ActionResult Index()
        {
            var config = new GlobalConfig();
            var pechkin = new SimplePechkin(config);
            ObjectConfig oc = new ObjectConfig();

            oc.SetPrintBackground(true).SetRunJavascript(true).SetScreenMediaType(true)
                .SetLoadImages(true)
                .SetPageUri("http://oschina.net");
            byte[] pdf = pechkin.Convert(oc);
return File(pdf, "application/pdf", "download.pdf"); } public ActionResult c() { SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig() .SetMargins(new Margins() {Left = 0, Right = 0, Top = 0, Bottom = 0})); //設置邊距 ObjectConfig oc = new ObjectConfig(); oc.SetPrintBackground(true).SetRunJavascript(true).SetScreenMediaType(true) .SetLoadImages(true) .SetPageUri("http://oschina.net"); byte[] buf = sc.Convert(oc); return File(buf, "application/pdf", "download.pdf"); } } }
相關文章
相關標籤/搜索