You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text;
namespace CounsellorBL { public class PDFService : MessageBase { public string createPdfByHtml(string sHtml, string sFileName, string outputFolder) { string sRoot = System.Web.HttpContext.Current.Server.MapPath("/OutFiles"); ServiceCom.fnCreateDir(sRoot + "\\" + outputFolder);//如果沒有該目錄就創建目錄
string sOutputPath = sRoot + "\\" + outputFolder + "\\" + sFileName; string sHtmlPath = sOutputPath + ".html"; string sPdfPath = sOutputPath + ".pdf";
string sContent = System.Web.HttpContext.Current.Server.HtmlDecode(sHtml); if (System.IO.File.Exists(sHtmlPath)) { System.IO.File.Delete(sHtmlPath); } using (FileStream fs = new FileStream(sHtmlPath, FileMode.Create)) { using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) { w.WriteLine(sContent); } } try { if (!string.IsNullOrEmpty(sHtmlPath) || !string.IsNullOrEmpty(sPdfPath)) { string resource = sRoot + "\\Resoure"; string dllstr = string.Format(resource + "\\wkhtmltopdf.exe");
if (System.IO.File.Exists(dllstr)) { string strParam = sHtmlPath + " " + sPdfPath; ProcessStartInfo pInfo = new ProcessStartInfo(); pInfo.FileName = dllstr; pInfo.Arguments = strParam.ToString(); pInfo.UseShellExecute = false; pInfo.RedirectStandardOutput = true; //pInfo.RedirectStandardError = true;
pInfo.RedirectStandardInput = true; pInfo.CreateNoWindow = false;
using (Process p = new Process()) { p.StartInfo = pInfo; p.Start(); p.WaitForExit(); try { if (!p.HasExited) { p.Kill(); } } catch { } } } } System.IO.File.Delete(sHtmlPath);//刪除html文件
} catch (Exception ex) { throw new Exception(ex.ToString()); } return sPdfPath; } public bool delPdf(string sPath) { bool blRet = true; try { if (System.IO.File.Exists(sPath)) { System.IO.File.Delete(sPath); } } catch { blRet = false; } return blRet; }
//字符串转流
public static MemoryStream StringToStream(string s) { // convert string to stream
byte[] byteArray = Encoding.Default.GetBytes(s); MemoryStream stream = new MemoryStream(byteArray); return stream; }
//流转字符串
public static string StreamToString(Stream stream) { StreamReader reader = new StreamReader(stream); string text = reader.ReadToEnd(); return text; }
//字符串转字节数组
public static Byte[] StringToByteArray(string s) { return Encoding.Default.GetBytes(s); }
//字节数组转字符串
public static string ByteArrayToString(Byte[] bytes) { return Encoding.Default.GetString(bytes); } } }
|