PdfUtils.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.Net;
  13. using System.Reflection;
  14. using System.Security.Cryptography.X509Certificates;
  15. namespace LisPacsDataUpload.business
  16. {
  17. /// <summary>
  18. /// pdf 工具类
  19. /// </summary>
  20. public class PdfUtils
  21. {
  22. /// <summary>
  23. /// 根据lis版本获取请求生成pdf报告的参数 0中联老版lis 1 中联新版lis 2 专业版临生免 10 标准服务老版lis 11标准服务新版lis 3 三方lis
  24. /// </summary>
  25. /// <returns></returns>
  26. public static string getPdfParamByLisVersion(int lisVersion, string reportNumber,string bbid,string yzid)
  27. {
  28. string dicLis = AppDomain.CurrentDomain.BaseDirectory + "Lis\\";
  29. if (!Directory.Exists(dicLis))
  30. {
  31. Directory.CreateDirectory(dicLis);
  32. }
  33. string path = dicLis + "LIS" + bbid+ ".pdf";
  34. Paramater paramter = new Paramater
  35. {
  36. RePortCode = reportNumber,
  37. PdfPath = $"pdf={path}",
  38. InvokeType = 4
  39. };
  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. paramter.ZlSysCode = 100;
  47. paramter.Params.Add(new SubParameter { Param = $"医嘱id={yzid}" });
  48. paramter.Params.Add(new SubParameter { Param = $"标本id={bbid}" });
  49. string strJson = JsonConvert.SerializeObject(paramter);
  50. LogHelper.Info("strJson==" + strJson);
  51. if (!Init())
  52. {
  53. LogHelper.Info("Init初始化异常");
  54. return "Init初始化异常";
  55. }
  56. //生成pdf成功
  57. if (OutPdf(strJson))
  58. {
  59. return path;
  60. }
  61. }
  62. else if (lisVersion == 1)
  63. {
  64. paramter.ZlSysCode = 2500;
  65. paramter.Params.Add(new SubParameter { Param = $"标本id={bbid}" });
  66. // 新版lis
  67. /* Select distinct 'ZLLISBILL00' || Decode(b.病人来源, 2, c.住院单据, 3, c.体检单据, 4, c.院外单据, c.门诊单据) || '-2' 报表编号, a.Id 标本id
  68. From 检验报告记录 A, 检验申请组合 B, 检验仪器记录 C
  69. Where a.Id = b.标本id And a.仪器id = c.Id And b.医嘱id = :id*/
  70. string strJson= JsonConvert.SerializeObject(paramter);
  71. LogHelper.Info("strJson=="+ strJson);
  72. if (!Init())
  73. {
  74. LogHelper.Info("Init初始化异常");
  75. return "Init初始化异常";
  76. }
  77. //生成pdf成功
  78. if (OutPdf(strJson))
  79. {
  80. return path;
  81. }
  82. }
  83. else if (lisVersion == 2)
  84. {
  85. //专业版临生免
  86. string sql_zlhr = @"select standard_url from 互认配置表";
  87. DataTable dt_lis = OracleHelper<object>.RunQueryDS(sql_zlhr);
  88. string standard_url = dt_lis.Rows[0]["standard_url"].ToString();
  89. 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}\"}}}";
  90. // 使用字符串插值替换占位符
  91. string data = jsonTemplate
  92. .Replace("{rpt_id}", bbid)
  93. .Replace("{time}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  94. .Replace("{action_no}", Guid.NewGuid().ToString());
  95. string responseString = Tools.WSDataToZlsoftInterface(standard_url, data);
  96. ZlsoftOutVO zlsoftOutVO = JsonConvert.DeserializeObject<ZlsoftOutVO>(responseString);
  97. if (!"A".Equals(zlsoftOutVO.input.ack_info.exe_status))
  98. {
  99. return zlsoftOutVO.input.ack_info.err_msg;
  100. }
  101. return zlsoftOutVO.input.file_info.rpt_content;
  102. }
  103. else if(lisVersion == 3)
  104. {
  105. // 三方lis
  106. string three_sql = @"select pdfUrl from three_lis_pdf where yzid =" + bbid;
  107. LogHelper.Info("three_sql "+ three_sql);
  108. DataRow data = SqlServerHelper.ExecuteSingleRowQuery(three_sql);
  109. if (data!=null)
  110. {
  111. string jpgUrl = data["pdfUrl"].ToString();
  112. LogHelper.Info("三方原始jpgUrl" + jpgUrl);
  113. GetPdfFromJpg(jpgUrl, path);
  114. return path;
  115. }
  116. return "三方lispdf报告获取失败";
  117. }
  118. else if (lisVersion == 10)
  119. {
  120. // 三方lis
  121. if (DownloadPdf(reportNumber, path))
  122. {
  123. return path;
  124. };
  125. return "标准服务lispdf报告获取失败";
  126. }
  127. return "失败";
  128. }
  129. public static void GetPdfFromJpg(string jpgUrl,string pdfUrl)
  130. {
  131. LogHelper.Info("开始从jpg转换pdf");
  132. //string jpgFilePath = "path_to_your_jpg.jpg";
  133. //string pdfFilePath = "output.pdf";
  134. using (FileStream fs = new FileStream(pdfUrl, FileMode.Create))
  135. {
  136. // 创建一个文档对象
  137. Document document = new Document();
  138. // 创建一个 PDF 写入器,将文档写入文件流
  139. PdfWriter writer = PdfWriter.GetInstance(document, fs);
  140. document.Open();
  141. // 创建一个图像对象并加载 JPG 文件
  142. Image image = Image.GetInstance(jpgUrl);
  143. // 设置图像在 PDF 中的尺寸(可以根据需要调整)
  144. image.ScaleToFit(document.PageSize.Width - 20, document.PageSize.Height - 20);
  145. // 将图像添加到 PDF 文档中
  146. document.Add(image);
  147. document.Close();
  148. }
  149. LogHelper.Info("JPG 转换为 PDF 成功!");
  150. }
  151. public static string GeneratePdf(int lisVersion, string reportNumber, string bbid, string yzid)
  152. {
  153. string pdfUrl = getPdfParamByLisVersion(lisVersion, reportNumber, bbid, yzid);
  154. LogHelper.Info("GeneratePdf==" + pdfUrl);
  155. return pdfUrl;
  156. }
  157. private static bool AcceptAllCertificates(object sender, X509Certificate certificate, X509Chain chain,
  158. bool sslPolicyErrors)
  159. {
  160. return true;
  161. }
  162. public static bool DownloadPdf(string pdfUrl, string localFilePath)
  163. {
  164. // 忽略证书验证
  165. try
  166. {
  167. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(pdfUrl);
  168. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  169. if (response.StatusCode == HttpStatusCode.OK)
  170. {
  171. using (Stream responseStream = response.GetResponseStream())
  172. {
  173. using (FileStream fileStream = new FileStream(localFilePath, FileMode.Create))
  174. {
  175. byte[] buffer = new byte[1024];
  176. int bytesRead;
  177. while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
  178. {
  179. fileStream.Write(buffer, 0, bytesRead);
  180. }
  181. }
  182. }
  183. return true;
  184. }
  185. else
  186. {
  187. LogHelper.Info($"下载失败,状态码: {response.StatusCode}");
  188. return false;
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. LogHelper.Info($"下载过程中出现错误: {ex.Message}");
  194. return false;
  195. }
  196. }
  197. static object Instance = null;
  198. public static bool Init()
  199. {
  200. if (Instance != null )
  201. {
  202. return true;
  203. }
  204. var para = JsonConvert.SerializeObject(new UserLogin
  205. {
  206. UserName = ConfigurationManager.AppSettings["UserName"],
  207. PassWord = ConfigurationManager.AppSettings["PasWord"],
  208. Server = ConfigurationManager.AppSettings["Server"]
  209. });
  210. LogHelper.Info("初始化入参" + para);
  211. string strReval = string.Empty;
  212. try
  213. {
  214. Process.GetProcessesByName("zlgyReport").ToList().ForEach(x => x.Kill());
  215. }
  216. catch (Exception ex) {
  217. LogHelper.Info("进程结束异常" + ex.Message); }
  218. var type = Type.GetTypeFromProgID("zlgyReport.clsGyReport");
  219. Instance = Activator.CreateInstance(type);
  220. ParameterModifier modifier = new ParameterModifier(2);
  221. modifier[1] = true;
  222. var result = type.InvokeMember("Init", BindingFlags.Default | BindingFlags.InvokeMethod, null, Instance, new object[] { para, strReval },
  223. new ParameterModifier[] { modifier },
  224. null,
  225. new string[] { "strJson", "strResult" });
  226. LogHelper.Info("出参:" + strReval);
  227. LogHelper.Info("返回:" + result.ToString());
  228. return (bool)result;
  229. }
  230. public static bool OutPdf(string para)
  231. {
  232. var type = Type.GetTypeFromProgID("zlgyReport.clsGyReport");
  233. ParameterModifier modifier = new ParameterModifier(2);
  234. modifier[1] = true;
  235. string strReval = string.Empty;
  236. var result = type.InvokeMember("ReportOpen", BindingFlags.Default | BindingFlags.InvokeMethod, null, Instance, new object[] { para, strReval },
  237. new ParameterModifier[] { modifier },
  238. null,
  239. new string[] { "strJson", "strResult" });
  240. LogHelper.Info("出参:" + strReval);
  241. LogHelper.Info("返回:" + result.ToString());
  242. return (bool)result;
  243. }
  244. public class UserLogin
  245. {
  246. public string UserName { get; set; }
  247. public string PassWord { get; set; }
  248. public string Server { get; set; }
  249. }
  250. }
  251. }