public RtvMsg RotativaFun() { //欲轉換PDF的URL位置 string pdfurl = "https://www.google.com/"; //指定檔案名稱 string fileName = "FromApi.pdf"; //選擇儲存Server端路徑 string pdfTemp = System.Web.HttpContext.Current.Server.MapPath("~/Rotativa") + @"\" + fileName; //wkhtmltopdf.exe 路徑 string wkhtmltopdfPath = System.Web.HttpContext.Current.Server.MapPath("~/Rotativa"); //wkhtmltopdf.exe 欲執行的指令 string Command = "--zoom 2 --margin-top 15mm --margin-bottom 15mm --ma rgin-right 15mm --margin-left 15mm --page-size A4 "; //透過Rotativa所提供的靜態類別中的Convert執行轉換HTML TO PDF byte[] GenPdfByte = WkhtmltopdfDriver.Convert(wkhtmltopdfPath, Command + pdfurl); System.IO.File.WriteAllBytes(pdfTemp, GenPdfByte); RtvMsg Rtv = new RtvMsg(); if (System.IO.File.Exists(pdfTemp)) { string UrlPath = @"http://" + this.Request.RequestUri.Authority + "/Rotativa/" + fileName; Rtv.Path = UrlPath; Rtv.Message = "已產生檔案"; } else { Rtv.Path = ""; Rtv.Message = "檔案未產生"; } return(Rtv); }
public RtvMsg EXEwkhtmltopdf() { RtvMsg Rtv = new RtvMsg(); //欲轉換PDF的URL位置 string pdfurl = "https://www.google.com/"; //指定檔案名稱 string fileName = "FromApiEXEwkhtmltopdf.pdf"; //選擇儲存Server端路徑 string pdfTemp = System.Web.HttpContext.Current.Server.MapPath("~/Rotativa") + @"\" + fileName; //wkhtmltopdf.exe 路徑 string wkhtmltopdfPath = System.Web.HttpContext.Current.Server.MapPath("~/Rotativa") + @"\wkhtmltopdf.exe"; //wkhtmltopdf.exe 欲執行的指令 string Command = "--zoom 2 --margin-top 15mm --margin-bottom 15mm --margin-right 15mm --margin-left 15mm --page-size A4 "; System.Diagnostics.ProcessStartInfo Pss = new ProcessStartInfo(); Pss.FileName = wkhtmltopdfPath;//HTML轉PDF執行檔 Pss.Arguments = string.Format("{0} {1} {2}", Command, pdfurl, pdfTemp); Pss.UseShellExecute = false; Pss.RedirectStandardInput = true; Pss.RedirectStandardOutput = true; using (System.Diagnostics.Process p = new Process()) { p.StartInfo = Pss; p.Start(); p.WaitForExit(60000); //判斷執行結果 if (p.ExitCode == 0) { p.Close(); if (System.IO.File.Exists(pdfTemp)) { string UrlPath = @"http://" + this.Request.RequestUri.Authority + "/Rotativa/" + fileName; Rtv.Path = UrlPath; Rtv.Message = "已產生檔案"; } else { Rtv.Path = ""; Rtv.Message = "檔案未產生"; } } } return(Rtv); }
/// <summary>第五種透過BuildPdf方法再Server端產生PDF </summary> /// <returns></returns> public JsonResult LocalPDFGen() { //透過URL位置產生PDF UrlAsPdf GenUrlPDF = new UrlAsPdf("https://tw.news.yahoo.com/sports/"); //設定檔案名稱 GenUrlPDF.FileName = "dotblogs_url_test.pdf"; //設定Margin GenUrlPDF.PageMargins = new Margins() { Top = 10, Bottom = 10, Left = 10, Right = 10 }; //指定大小預設為A4 GenUrlPDF.PageSize = Size.A4; //BuildPdf此方法提供使用者再SEVER端產出PDF //需綁定再System.Web.Mvc.ControllerContext之下 byte[] GenPDFinSever = GenUrlPDF.BuildPdf(this.ControllerContext); //存入Server端的位置 string SavePath = GenUrlPDF.WkhtmltopdfPath + @"\" + GenUrlPDF.FileName; //產生PDF檔 System.IO.File.WriteAllBytes(SavePath, GenPDFinSever); RtvMsg Rtv = new RtvMsg(); if (System.IO.File.Exists(SavePath)) { string UrlPath = @"http://" + Request.Url.Authority + "/Rotativa/" + GenUrlPDF.FileName; Rtv.Path = UrlPath; Rtv.Message = "已產生檔案"; } else { Rtv.Path = ""; Rtv.Message = "檔案未產生"; } return(Json(Rtv, JsonRequestBehavior.AllowGet)); }