PdfUtils.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using Common;
  2. using iTextSharp.text;
  3. using iTextSharp.text.pdf;
  4. using LisPacsDataUpload.Models.Report;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Reflection;
  13. namespace LisPacsDataUpload.business
  14. {
  15. /// <summary>
  16. /// pdf 工具类
  17. /// </summary>
  18. public class PdfUtils
  19. {
  20. /// <summary>
  21. /// 根据lis版本获取请求生成pdf报告的参数 0中联老版lis 1 中联新版lis 2 专业版临生免 10 标准服务老版lis 11标准服务新版lis 3 三方lis
  22. /// </summary>
  23. /// <returns></returns>
  24. public static string getPdfParamByLisVersion(int lisVersion, string reportNumber,string bbid)
  25. {
  26. string dicLis = AppDomain.CurrentDomain.BaseDirectory + "Lis\\";
  27. if (!Directory.Exists(dicLis))
  28. {
  29. Directory.CreateDirectory(dicLis);
  30. }
  31. string path = dicLis + "LIS" + bbid+ ".pdf";
  32. Paramater paramter = new Paramater
  33. {
  34. ZlSysCode = 2500,
  35. RePortCode = reportNumber,
  36. PdfPath = $"pdf={path}",
  37. InvokeType = 4
  38. };
  39. paramter.Params.Add(new SubParameter { Param = $"标本id={bbid}" });
  40. if (lisVersion==0)
  41. {
  42. //老版lis
  43. /* Select Distinct 'ZLCISBILL' || Trim(To_Char(b.编号, '00000')) || '-2' 报表编号, a.相关id, d.id 标本id
  44. From 病人医嘱记录 A,检验标本记录 d, 病历文件列表 B, 病历单据应用 C
  45. Where a.诊疗项目id = c.诊疗项目id And a.病人来源 = c.应用场合 And c.病历文件id = b.Id And a.Id = :id and a.相关id = d.医嘱id*/
  46. var id = ""; // 标本id
  47. paramter.Params.Add(new SubParameter { Param = $"标本id={id}" });
  48. }
  49. else if (lisVersion == 1)
  50. {
  51. // 新版lis
  52. /* Select distinct 'ZLLISBILL00' || Decode(b.病人来源, 2, c.住院单据, 3, c.体检单据, 4, c.院外单据, c.门诊单据) || '-2' 报表编号, a.Id 标本id
  53. From 检验报告记录 A, 检验申请组合 B, 检验仪器记录 C
  54. Where a.Id = b.标本id And a.仪器id = c.Id And b.医嘱id = :id*/
  55. string strJson= JsonConvert.SerializeObject(paramter);
  56. LogHelper.Info("strJson=="+ strJson);
  57. if (!Init())
  58. {
  59. LogHelper.Info("Init初始化异常");
  60. return "Init初始化异常";
  61. }
  62. //生成pdf成功
  63. if (OutPdf(strJson))
  64. {
  65. return path;
  66. }
  67. }
  68. else if (lisVersion == 2)
  69. {
  70. //专业版临生免
  71. string sql_zlhr = @"select standard_url from 互认配置表";
  72. DataTable dt_lis = OracleHelper<object>.RunQueryDS(sql_zlhr);
  73. string standard_url = dt_lis.Rows[0]["standard_url"].ToString();
  74. string jsonTemplate = "{\"input\":{\"req_info\":{\"rpt_id\":\"{rpt_id}\",\"rpt_type\":\"0\"},\"head\":{\"bizno\":\"S4021\",\"sysno\":\"zlhr\",\"tarno\":\"ZLHIS\",\"time\":\"{time}\",\"action_no\":\"{action_no}\"}}}";
  75. // 使用字符串插值替换占位符
  76. string data = jsonTemplate
  77. .Replace("{rpt_id}", bbid)
  78. .Replace("{time}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  79. .Replace("{action_no}", Guid.NewGuid().ToString());
  80. string responseString = Tools.WSDataToZlsoftInterface(standard_url, data);
  81. ZlsoftOutVO zlsoftOutVO = JsonConvert.DeserializeObject<ZlsoftOutVO>(responseString);
  82. if (!"A".Equals(zlsoftOutVO.input.ack_info.exe_status))
  83. {
  84. return zlsoftOutVO.input.ack_info.err_msg;
  85. }
  86. return zlsoftOutVO.input.file_info.rpt_content;
  87. }
  88. else if(lisVersion == 3)
  89. {
  90. // 三方lis
  91. string three_sql = @"select pdfUrl from three_lis_pdf where yzid =" + bbid;
  92. LogHelper.Info("three_sql "+ three_sql);
  93. DataRow data = SqlServerHelper.ExecuteSingleRowQuery(three_sql);
  94. if (data!=null)
  95. {
  96. string jpgUrl = data["pdfUrl"].ToString();
  97. LogHelper.Info("三方原始jpgUrl" + jpgUrl);
  98. GetPdfFromJpg(jpgUrl, path);
  99. return path;
  100. }
  101. return "三方lispdf报告获取失败";
  102. }
  103. return "失败";
  104. }
  105. public static void GetPdfFromJpg(string jpgUrl,string pdfUrl)
  106. {
  107. LogHelper.Info("开始从jpg转换pdf");
  108. //string jpgFilePath = "path_to_your_jpg.jpg";
  109. //string pdfFilePath = "output.pdf";
  110. using (FileStream fs = new FileStream(pdfUrl, FileMode.Create))
  111. {
  112. // 创建一个文档对象
  113. Document document = new Document();
  114. // 创建一个 PDF 写入器,将文档写入文件流
  115. PdfWriter writer = PdfWriter.GetInstance(document, fs);
  116. document.Open();
  117. // 创建一个图像对象并加载 JPG 文件
  118. Image image = Image.GetInstance(jpgUrl);
  119. // 设置图像在 PDF 中的尺寸(可以根据需要调整)
  120. image.ScaleToFit(document.PageSize.Width - 20, document.PageSize.Height - 20);
  121. // 将图像添加到 PDF 文档中
  122. document.Add(image);
  123. document.Close();
  124. }
  125. LogHelper.Info("JPG 转换为 PDF 成功!");
  126. }
  127. public static string GeneratePdf(int lisVersion, string reportNumber, string bbid)
  128. {
  129. string pdfUrl = getPdfParamByLisVersion(lisVersion, reportNumber, bbid);
  130. LogHelper.Info("GeneratePdf==" + pdfUrl);
  131. return pdfUrl;
  132. }
  133. //调用exe
  134. public static bool CallingProgram(string strJson)
  135. {
  136. // 定义要启动的外部程序的路径
  137. string exePath = @"C:\APPSOFT\ReportPrint\zlgyReport.exe";
  138. // 定义要传递给外部程序的参数
  139. string arguments = "strJson " + strJson;
  140. // 创建一个新的 ProcessStartInfo 对象
  141. ProcessStartInfo startInfo = new ProcessStartInfo
  142. {
  143. FileName = exePath, // 设置要启动的程序路径
  144. Arguments = arguments, // 设置传递给程序的参数
  145. RedirectStandardOutput = false, // 是否重定向标准输出(根据需要设置)
  146. RedirectStandardError = false, // 是否重定向标准错误(根据需要设置)
  147. UseShellExecute = true, // 是否使用操作系统的 shell 启动进程(通常设置为 true)
  148. CreateNoWindow = false // 是否创建窗口(如果不需要显示窗口,可以设置为 true)
  149. };
  150. try
  151. {
  152. // 启动外部程序
  153. using (Process process = Process.Start(startInfo))
  154. {
  155. // 如果需要等待程序结束,可以使用 process.WaitForExit()
  156. LogHelper.Info("生成lis报告中");
  157. process.WaitForExit();
  158. LogHelper.Info("成功生成lis报告");
  159. // 如果需要读取输出或错误,可以在这里处理
  160. string output = process.StandardOutput.ReadToEnd();
  161. string error = process.StandardError.ReadToEnd();
  162. LogHelper.Info("output==" + output);
  163. LogHelper.Info("error==" + error);
  164. }
  165. }
  166. catch (Exception ex)
  167. {
  168. // 处理异常,例如文件未找到、路径错误等
  169. LogHelper.Info("启动外部程序时出错: " + ex.Message);
  170. }
  171. return true;
  172. }
  173. static object Instance = null;
  174. public static bool Init()
  175. {
  176. if (Instance != null )
  177. {
  178. return true;
  179. }
  180. var para = JsonConvert.SerializeObject(new UserLogin
  181. {
  182. UserName = ConfigurationManager.AppSettings["UserName"],
  183. PassWord = ConfigurationManager.AppSettings["PasWord"],
  184. Server = ConfigurationManager.AppSettings["Server"]
  185. });
  186. LogHelper.Info("初始化入参" + para);
  187. string strReval = string.Empty;
  188. try
  189. {
  190. Process.GetProcessesByName("zlgyReport").ToList().ForEach(x => x.Kill());
  191. }
  192. catch (Exception ex) {
  193. LogHelper.Info("进程结束异常" + ex.Message); }
  194. var type = Type.GetTypeFromProgID("zlgyReport.clsGyReport");
  195. Instance = Activator.CreateInstance(type);
  196. ParameterModifier modifier = new ParameterModifier(2);
  197. modifier[1] = true;
  198. var result = type.InvokeMember("Init", BindingFlags.Default | BindingFlags.InvokeMethod, null, Instance, new object[] { para, strReval },
  199. new ParameterModifier[] { modifier },
  200. null,
  201. new string[] { "strJson", "strResult" });
  202. LogHelper.Info("出参:" + strReval);
  203. LogHelper.Info("返回:" + result.ToString());
  204. return (bool)result;
  205. }
  206. public static bool OutPdf(string para)
  207. {
  208. var type = Type.GetTypeFromProgID("zlgyReport.clsGyReport");
  209. ParameterModifier modifier = new ParameterModifier(2);
  210. modifier[1] = true;
  211. string strReval = string.Empty;
  212. var result = type.InvokeMember("ReportOpen", BindingFlags.Default | BindingFlags.InvokeMethod, null, Instance, new object[] { para, strReval },
  213. new ParameterModifier[] { modifier },
  214. null,
  215. new string[] { "strJson", "strResult" });
  216. LogHelper.Info("出参:" + strReval);
  217. LogHelper.Info("返回:" + result.ToString());
  218. return (bool)result;
  219. }
  220. public class UserLogin
  221. {
  222. public string UserName { get; set; }
  223. public string PassWord { get; set; }
  224. public string Server { get; set; }
  225. }
  226. }
  227. }