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.

202 lines
7.0 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. namespace EasyBL
  7. {
  8. public class PdfService : ServiceBase
  9. {
  10. #region Excel轉Pdf
  11. /// <summary>
  12. /// 函式名稱:ExcelToPdf
  13. /// 函式說明:系統登入
  14. /// </summary>
  15. /// <param name="i_crm">todo: describe i_crm parameter on ExcelToPdf</param>
  16. /// <returns>
  17. /// 回傳 rm(Object)
  18. ///</returns>
  19. public ResponseMessage ExcelToPdf(RequestMessage i_crm)
  20. {
  21. ResponseMessage rm = null;
  22. string sError = null;
  23. string sOutputPath = null;
  24. try
  25. {
  26. do
  27. {
  28. var sInputPath = _fetchString(i_crm, "InputPath");
  29. var sOutputFolder = _fetchString(i_crm, "OutputFolder");
  30. var sFileName = _fetchString(i_crm, "FileName");
  31. var bOk = ExcelToPdf(out sOutputPath, sInputPath, sFileName, sOutputFolder);
  32. rm = new SuccessResponseMessage(null, i_crm);
  33. rm.DATA.Add(BLWording.REL, sOutputPath);
  34. } while (false);
  35. }
  36. catch (Exception ex)
  37. {
  38. sError = Util.GetLastExceptionMsg(ex);
  39. LogAndSendEmail(sError + "Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(PdfService), nameof(ExcelToPdf), "ExcelToPdf(Excel 轉PDF)", "", "", "");
  40. }
  41. finally
  42. {
  43. if (null != sError)
  44. {
  45. rm = new ErrorResponseMessage(sError, i_crm);
  46. }
  47. }
  48. return rm;
  49. }
  50. #endregion Excel轉Pdf
  51. public static string HtmlToPdf(string sHtml, string sFileName, string outputFolder)
  52. {
  53. var sRoot = System.Web.HttpContext.Current.Server.MapPath("/EurotranFile");
  54. Common.FnCreateDir(sRoot + "\\" + outputFolder);//如果沒有該目錄就創建目錄
  55. var sOutputPath = sRoot + "\\" + outputFolder + "\\" + sFileName;
  56. var sHtmlPath = sOutputPath + ".html";
  57. var sPdfPath = sOutputPath + ".pdf";
  58. var sContent = System.Web.HttpContext.Current.Server.HtmlDecode(sHtml);
  59. if (File.Exists(sHtmlPath))
  60. {
  61. File.Delete(sHtmlPath);
  62. }
  63. using (FileStream fs = new FileStream(sHtmlPath, FileMode.Create))
  64. {
  65. using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
  66. {
  67. w.WriteLine(sContent);
  68. }
  69. }
  70. try
  71. {
  72. if (!string.IsNullOrEmpty(sHtmlPath) || !string.IsNullOrEmpty(sPdfPath))
  73. {
  74. var resource = sRoot + "\\Resoure";
  75. var dllstr = string.Format(resource + "\\wkhtmltopdf.exe");
  76. if (File.Exists(dllstr))
  77. {
  78. var strParam = sHtmlPath + " " + sPdfPath;
  79. var pInfo = new ProcessStartInfo
  80. {
  81. FileName = dllstr,
  82. Arguments = strParam.ToString(),
  83. UseShellExecute = false,
  84. RedirectStandardOutput = true,
  85. //pInfo.RedirectStandardError = true;
  86. RedirectStandardInput = true,
  87. CreateNoWindow = false
  88. };
  89. using (var p = new Process
  90. {
  91. StartInfo = pInfo
  92. })
  93. {
  94. p.Start();
  95. p.WaitForExit();
  96. try
  97. {
  98. if (!p.HasExited)
  99. {
  100. p.Kill();
  101. }
  102. }
  103. catch { }
  104. }
  105. }
  106. }
  107. File.Delete(sHtmlPath);//刪除html文件
  108. }
  109. catch (Exception ex)
  110. {
  111. throw new Exception(ex.ToString());
  112. }
  113. return sPdfPath.Replace("\\", "/");
  114. }
  115. public static bool ExcelToPdf(out string outputPath, string inputPath, string sFileName = null, string sTempFolder = null)
  116. {
  117. if (string.IsNullOrEmpty(sFileName)) { sFileName = Guid.NewGuid().ToString(); }
  118. if (string.IsNullOrEmpty(sTempFolder)) { sTempFolder = "Temp"; }
  119. var bOk = false;
  120. var sRoot = System.Web.HttpContext.Current.Server.MapPath("/EurotranFile");
  121. Common.FnCreateDir(sRoot + "\\" + sTempFolder);//如果沒有該目錄就創建目錄
  122. var sOutputPath = sRoot + "\\" + sTempFolder + "\\" + sFileName;
  123. var sPdfPath = sOutputPath + ".pdf";
  124. try
  125. {
  126. //Spire.Xls.Workbook xls = new Spire.Xls.Workbook();
  127. //xls.ConverterSetting.SheetFitToPage = true;
  128. //xls.LoadFromFile(inputPath, Spire.Xls.ExcelVersion.Version97to2003);
  129. //xls.SaveToFile(sPdfPath, Spire.Xls.FileFormat.PDF);
  130. //bOk = true;
  131. }
  132. catch (Exception ex)
  133. {
  134. throw new Exception(ex.ToString());
  135. }
  136. outputPath = sPdfPath.Replace("\\", "/");
  137. return bOk;
  138. }
  139. public static bool DelFile(string sPath)
  140. {
  141. var blRet = false;
  142. try
  143. {
  144. if (sPath.StartsWith("Document") || sPath.StartsWith("OutFiles"))
  145. {
  146. sPath = System.Web.HttpContext.Current.Server.MapPath("/" + sPath);
  147. }
  148. if (File.Exists(sPath))
  149. {
  150. File.Delete(sPath);
  151. blRet = true;
  152. }
  153. }
  154. catch
  155. {
  156. blRet = false;
  157. }
  158. return blRet;
  159. }
  160. //字符串转流
  161. public static MemoryStream StringToStream(string s)
  162. {
  163. // convert string to stream
  164. var byteArray = Encoding.Default.GetBytes(s);
  165. var stream = new MemoryStream(byteArray);
  166. return stream;
  167. }
  168. //流转字符串
  169. public static string StreamToString(Stream stream)
  170. {
  171. using (var reader = new StreamReader(stream))
  172. {
  173. var text = reader.ReadToEnd();
  174. return text;
  175. }
  176. }
  177. //字符串转字节数组
  178. public static Byte[] StringToByteArray(string s)
  179. {
  180. return Encoding.Default.GetBytes(s);
  181. }
  182. //字节数组转字符串
  183. public static string ByteArrayToString(Byte[] bytes)
  184. {
  185. return Encoding.Default.GetString(bytes);
  186. }
  187. }
  188. }