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.

122 lines
4.1 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. namespace CounsellorBL
  8. {
  9. public class PDFService : MessageBase
  10. {
  11. public string createPdfByHtml(string sHtml, string sFileName, string outputFolder)
  12. {
  13. string sRoot = System.Web.HttpContext.Current.Server.MapPath("/OutFiles");
  14. ServiceCom.fnCreateDir(sRoot + "\\" + outputFolder);//如果沒有該目錄就創建目錄
  15. string sOutputPath = sRoot + "\\" + outputFolder + "\\" + sFileName;
  16. string sHtmlPath = sOutputPath + ".html";
  17. string sPdfPath = sOutputPath + ".pdf";
  18. string sContent = System.Web.HttpContext.Current.Server.HtmlDecode(sHtml);
  19. if (System.IO.File.Exists(sHtmlPath))
  20. {
  21. System.IO.File.Delete(sHtmlPath);
  22. }
  23. using (FileStream fs = new FileStream(sHtmlPath, FileMode.Create))
  24. {
  25. using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
  26. {
  27. w.WriteLine(sContent);
  28. }
  29. }
  30. try
  31. {
  32. if (!string.IsNullOrEmpty(sHtmlPath) || !string.IsNullOrEmpty(sPdfPath))
  33. {
  34. string resource = sRoot + "\\Resoure";
  35. string dllstr = string.Format(resource + "\\wkhtmltopdf.exe");
  36. if (System.IO.File.Exists(dllstr))
  37. {
  38. string strParam = sHtmlPath + " " + sPdfPath;
  39. ProcessStartInfo pInfo = new ProcessStartInfo();
  40. pInfo.FileName = dllstr;
  41. pInfo.Arguments = strParam.ToString();
  42. pInfo.UseShellExecute = false;
  43. pInfo.RedirectStandardOutput = true;
  44. //pInfo.RedirectStandardError = true;
  45. pInfo.RedirectStandardInput = true;
  46. pInfo.CreateNoWindow = false;
  47. using (Process p = new Process())
  48. {
  49. p.StartInfo = pInfo;
  50. p.Start();
  51. p.WaitForExit();
  52. try
  53. {
  54. if (!p.HasExited)
  55. {
  56. p.Kill();
  57. }
  58. }
  59. catch
  60. { }
  61. }
  62. }
  63. }
  64. System.IO.File.Delete(sHtmlPath);//刪除html文件
  65. }
  66. catch (Exception ex)
  67. {
  68. throw new Exception(ex.ToString());
  69. }
  70. return sPdfPath;
  71. }
  72. public bool delPdf(string sPath)
  73. {
  74. bool blRet = true;
  75. try
  76. {
  77. if (System.IO.File.Exists(sPath))
  78. {
  79. System.IO.File.Delete(sPath);
  80. }
  81. }
  82. catch
  83. {
  84. blRet = false;
  85. }
  86. return blRet;
  87. }
  88. //字符串转流
  89. public static MemoryStream StringToStream(string s)
  90. {
  91. // convert string to stream
  92. byte[] byteArray = Encoding.Default.GetBytes(s);
  93. MemoryStream stream = new MemoryStream(byteArray);
  94. return stream;
  95. }
  96. //流转字符串
  97. public static string StreamToString(Stream stream)
  98. {
  99. StreamReader reader = new StreamReader(stream);
  100. string text = reader.ReadToEnd();
  101. return text;
  102. }
  103. //字符串转字节数组
  104. public static Byte[] StringToByteArray(string s)
  105. {
  106. return Encoding.Default.GetBytes(s);
  107. }
  108. //字节数组转字符串
  109. public static string ByteArrayToString(Byte[] bytes)
  110. {
  111. return Encoding.Default.GetString(bytes);
  112. }
  113. }
  114. }