123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- using Common;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using LisPacsDataUpload.Models.Report;
- using Newtonsoft.Json;
- using System;
- using System.Configuration;
- using System.Data;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- namespace LisPacsDataUpload.business
- {
- /// <summary>
- /// pdf 工具类
- /// </summary>
- public class PdfUtils
- {
- /// <summary>
- /// 根据lis版本获取请求生成pdf报告的参数 0中联老版lis 1 中联新版lis 2 专业版临生免 10 标准服务老版lis 11标准服务新版lis 3 三方lis
- /// </summary>
- /// <returns></returns>
- public static string getPdfParamByLisVersion(int lisVersion, string reportNumber,string bbid)
- {
- string dicLis = AppDomain.CurrentDomain.BaseDirectory + "Lis\\";
- if (!Directory.Exists(dicLis))
- {
- Directory.CreateDirectory(dicLis);
- }
- string path = dicLis + "LIS" + bbid+ ".pdf";
- Paramater paramter = new Paramater
- {
- ZlSysCode = 2500,
- RePortCode = reportNumber,
- PdfPath = $"pdf={path}",
- InvokeType = 4
- };
- paramter.Params.Add(new SubParameter { Param = $"标本id={bbid}" });
- if (lisVersion==0)
- {
- //老版lis
- /* Select Distinct 'ZLCISBILL' || Trim(To_Char(b.编号, '00000')) || '-2' 报表编号, a.相关id, d.id 标本id
- From 病人医嘱记录 A,检验标本记录 d, 病历文件列表 B, 病历单据应用 C
- Where a.诊疗项目id = c.诊疗项目id And a.病人来源 = c.应用场合 And c.病历文件id = b.Id And a.Id = :id and a.相关id = d.医嘱id*/
- var id = ""; // 标本id
- paramter.Params.Add(new SubParameter { Param = $"标本id={id}" });
- }
- else if (lisVersion == 1)
- {
- // 新版lis
- /* Select distinct 'ZLLISBILL00' || Decode(b.病人来源, 2, c.住院单据, 3, c.体检单据, 4, c.院外单据, c.门诊单据) || '-2' 报表编号, a.Id 标本id
- From 检验报告记录 A, 检验申请组合 B, 检验仪器记录 C
- Where a.Id = b.标本id And a.仪器id = c.Id And b.医嘱id = :id*/
- string strJson= JsonConvert.SerializeObject(paramter);
- LogHelper.Info("strJson=="+ strJson);
- if (!Init())
- {
- LogHelper.Info("Init初始化异常");
- return "Init初始化异常";
- }
- //生成pdf成功
- if (OutPdf(strJson))
- {
- return path;
- }
- }
- else if (lisVersion == 2)
- {
- //专业版临生免
- string sql_zlhr = @"select standard_url from 互认配置表";
- DataTable dt_lis = OracleHelper<object>.RunQueryDS(sql_zlhr);
- string standard_url = dt_lis.Rows[0]["standard_url"].ToString();
- 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}\"}}}";
- // 使用字符串插值替换占位符
- string data = jsonTemplate
- .Replace("{rpt_id}", bbid)
- .Replace("{time}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
- .Replace("{action_no}", Guid.NewGuid().ToString());
- string responseString = Tools.WSDataToZlsoftInterface(standard_url, data);
- ZlsoftOutVO zlsoftOutVO = JsonConvert.DeserializeObject<ZlsoftOutVO>(responseString);
- if (!"A".Equals(zlsoftOutVO.input.ack_info.exe_status))
- {
- return zlsoftOutVO.input.ack_info.err_msg;
- }
- return zlsoftOutVO.input.file_info.rpt_content;
- }
- else if(lisVersion == 3)
- {
- // 三方lis
- string three_sql = @"select pdfUrl from three_lis_pdf where yzid =" + bbid;
- LogHelper.Info("three_sql "+ three_sql);
- DataRow data = SqlServerHelper.ExecuteSingleRowQuery(three_sql);
- if (data!=null)
- {
- string jpgUrl = data["pdfUrl"].ToString();
- LogHelper.Info("三方原始jpgUrl" + jpgUrl);
- GetPdfFromJpg(jpgUrl, path);
- return path;
- }
- return "三方lispdf报告获取失败";
- }
- return "失败";
- }
- public static void GetPdfFromJpg(string jpgUrl,string pdfUrl)
- {
- LogHelper.Info("开始从jpg转换pdf");
- //string jpgFilePath = "path_to_your_jpg.jpg";
- //string pdfFilePath = "output.pdf";
- using (FileStream fs = new FileStream(pdfUrl, FileMode.Create))
- {
- // 创建一个文档对象
- Document document = new Document();
- // 创建一个 PDF 写入器,将文档写入文件流
- PdfWriter writer = PdfWriter.GetInstance(document, fs);
- document.Open();
- // 创建一个图像对象并加载 JPG 文件
- Image image = Image.GetInstance(jpgUrl);
- // 设置图像在 PDF 中的尺寸(可以根据需要调整)
- image.ScaleToFit(document.PageSize.Width - 20, document.PageSize.Height - 20);
- // 将图像添加到 PDF 文档中
- document.Add(image);
- document.Close();
- }
- LogHelper.Info("JPG 转换为 PDF 成功!");
- }
- public static string GeneratePdf(int lisVersion, string reportNumber, string bbid)
- {
- string pdfUrl = getPdfParamByLisVersion(lisVersion, reportNumber, bbid);
- LogHelper.Info("GeneratePdf==" + pdfUrl);
- return pdfUrl;
- }
- //调用exe
- public static bool CallingProgram(string strJson)
- {
- // 定义要启动的外部程序的路径
- string exePath = @"C:\APPSOFT\ReportPrint\zlgyReport.exe";
- // 定义要传递给外部程序的参数
- string arguments = "strJson " + strJson;
- // 创建一个新的 ProcessStartInfo 对象
- ProcessStartInfo startInfo = new ProcessStartInfo
- {
- FileName = exePath, // 设置要启动的程序路径
- Arguments = arguments, // 设置传递给程序的参数
- RedirectStandardOutput = false, // 是否重定向标准输出(根据需要设置)
- RedirectStandardError = false, // 是否重定向标准错误(根据需要设置)
- UseShellExecute = true, // 是否使用操作系统的 shell 启动进程(通常设置为 true)
- CreateNoWindow = false // 是否创建窗口(如果不需要显示窗口,可以设置为 true)
- };
- try
- {
- // 启动外部程序
- using (Process process = Process.Start(startInfo))
- {
- // 如果需要等待程序结束,可以使用 process.WaitForExit()
- LogHelper.Info("生成lis报告中");
- process.WaitForExit();
- LogHelper.Info("成功生成lis报告");
- // 如果需要读取输出或错误,可以在这里处理
- string output = process.StandardOutput.ReadToEnd();
- string error = process.StandardError.ReadToEnd();
- LogHelper.Info("output==" + output);
- LogHelper.Info("error==" + error);
- }
- }
- catch (Exception ex)
- {
- // 处理异常,例如文件未找到、路径错误等
- LogHelper.Info("启动外部程序时出错: " + ex.Message);
- }
- return true;
- }
- static object Instance = null;
- public static bool Init()
- {
- if (Instance != null )
- {
- return true;
- }
-
- var para = JsonConvert.SerializeObject(new UserLogin
- {
- UserName = ConfigurationManager.AppSettings["UserName"],
- PassWord = ConfigurationManager.AppSettings["PasWord"],
- Server = ConfigurationManager.AppSettings["Server"]
- });
- LogHelper.Info("初始化入参" + para);
- string strReval = string.Empty;
- try
- {
- Process.GetProcessesByName("zlgyReport").ToList().ForEach(x => x.Kill());
- }
- catch (Exception ex) {
- LogHelper.Info("进程结束异常" + ex.Message); }
- var type = Type.GetTypeFromProgID("zlgyReport.clsGyReport");
- Instance = Activator.CreateInstance(type);
- ParameterModifier modifier = new ParameterModifier(2);
- modifier[1] = true;
- var result = type.InvokeMember("Init", BindingFlags.Default | BindingFlags.InvokeMethod, null, Instance, new object[] { para, strReval },
- new ParameterModifier[] { modifier },
- null,
- new string[] { "strJson", "strResult" });
- LogHelper.Info("出参:" + strReval);
- LogHelper.Info("返回:" + result.ToString());
- return (bool)result;
- }
- public static bool OutPdf(string para)
- {
- var type = Type.GetTypeFromProgID("zlgyReport.clsGyReport");
- ParameterModifier modifier = new ParameterModifier(2);
- modifier[1] = true;
- string strReval = string.Empty;
- var result = type.InvokeMember("ReportOpen", BindingFlags.Default | BindingFlags.InvokeMethod, null, Instance, new object[] { para, strReval },
- new ParameterModifier[] { modifier },
- null,
- new string[] { "strJson", "strResult" });
- LogHelper.Info("出参:" + strReval);
- LogHelper.Info("返回:" + result.ToString());
- return (bool)result;
- }
- public class UserLogin
- {
- public string UserName { get; set; }
- public string PassWord { get; set; }
- public string Server { get; set; }
- }
- }
- }
|