public void Execute(IJobExecutionContext context) { try { FormToPDFTaskRepository repo = new FormToPDFTaskRepository(); repo.AddTask(); var taskId = ""; var jobDataTaskId = context.Trigger.JobDataMap["taskId"]; if (jobDataTaskId != null) { taskId = jobDataTaskId.ToString(); } FormToPDFTask task = repo.GetTask(taskId); while (task != null) { try { repo.BeginTask(task.ID); var uri = FormToWordApiUrl + string.Format("FormToWordAPI/{0}?TmplCode={1}", task.FormID, task.TempCode); byte[] wordBuffer = WebApiClientHelper.DoFileRequest(uri); StringBuilder fileNames = new StringBuilder(); //1. 转pdf byte[] pdfFile = FileConverter.Word2PDF(wordBuffer); int j = 0; var length = "0KB"; if (pdfFile.Length < 1000) { length = pdfFile.Length + "B"; } else if (pdfFile.Length >= 1000 && pdfFile.Length < 1000000) { length = pdfFile.Length / 1000 + "KB"; } else { length = pdfFile.Length / 1000000 + "M"; } var src = string.IsNullOrEmpty(ConfigurationManager.AppSettings["Src"]) ? "" : ConfigurationManager.AppSettings["Src"]; string pdfFileID = FileStoreHelper.SaveFile(pdfFile, task.FormID + ".pdf", src, true); //2.转jpg List <byte[]> results = FileConverter.Word2JPG(wordBuffer); foreach (var item in results) { int p = 0; length = "0KB"; if (item.Length < 1000) { length = item.Length + "B"; } else if (item.Length >= 1000 && item.Length < 1000000) { length = item.Length / 1000 + "KB"; } else { length = item.Length / 1000000 + "M"; } string fileID = FileStoreHelper.SaveFile(item, task.FormID + "_" + (p++).ToString() + ".jpg", src, true); fileNames.Append(fileID); fileNames.Append("_"); fileNames.Append(length); fileNames.Append(","); } repo.EndTask(task.ID, pdfFileID, fileNames.ToString().TrimEnd(',')); task = repo.GetTask(); } catch (Exception ex) { repo.Log(task.ID, ex.Message); LogWriter.Error("[出错记录]:" + task.ID + " [错误信息]:" + ex.Message); continue; } } } catch (Exception e) { LogWriter.Error(e.Message); } finally { //防止和浏览转图程序关闭进程冲突,不能在转图浏览服务器上同时部署 CloseExit(); System.Environment.Exit(0); } }
public void Execute(IJobExecutionContext context) { var taskId = ""; var jobDataTaskId = context.Trigger.JobDataMap["taskId"]; if (jobDataTaskId != null) { taskId = jobDataTaskId.ToString(); } IPDFTaskRepository repo = IPDFTaskRepositoryFactory.GetRepository(); var task = repo.GetTask(taskId); if (task != null) { // 修改开始执行时间 repo.StartTask(task.ID); try { var pdfFileId = ""; var directoryPath = AppDomain.CurrentDomain.BaseDirectory; Regex r = new Regex(@"^[1-9]\d*_"); var mainFileName = r.Replace(Path.GetFileNameWithoutExtension(task.FileID), "", 1); #region 转换PDF byte[] pdfBuffer = null; if (!string.IsNullOrEmpty(task.FileType)) { if (task.FileType.ToLower() == "pdf") { pdfFileId = task.FileID; pdfBuffer = FileStoreHelper.GetFile(pdfFileId); } else if (task.FileType.ToLower() == "docx" || task.FileType.ToLower() == "doc") { var pdfFileName = mainFileName + ".pdf"; // 获取文件 var butter = FileStoreHelper.GetFile(task.FileID); pdfBuffer = FileConverter.Word2PDF(butter); pdfFileId = FileStoreHelper.SaveFile(pdfBuffer, pdfFileName); } else if (task.FileType.ToLower() == "xlsx" || task.FileType.ToLower() == "xls") { var pdfFileName = mainFileName + ".pdf"; // 获取文件 var buffer = FileStoreHelper.GetFile(task.FileID); pdfBuffer = FileConverter.Excel2PDF(buffer); pdfFileId = FileStoreHelper.SaveFile(pdfBuffer, pdfFileName); } else if (task.FileType.ToLower() == "jpg") { // 获取文件 pdfBuffer = FileStoreHelper.GetFile(task.FileID); } else { throw new Exception("未知文件类型,没有相关的转换器"); } } else { throw new Exception("未定义文件类型"); } #endregion if (pdfBuffer == null) { throw new Exception("PDF文件不存在"); } var pdfPageCount = task.PDFPageCount = FileConverter.GetPageCount(pdfBuffer); //var isSplit = task.IsSplit = IsSplitPDF(pdfBuffer.Length, pdfPageCount); var isSplit = false; repo.UpdatePDFFileID(task.ID, pdfFileId, pdfPageCount, isSplit); #region 生成SWF var pdfFilePath = directoryPath + pdfFileId; var swfFilePath = directoryPath + Path.GetFileNameWithoutExtension(task.FileID) + ".swf"; if (!File.Exists(pdfFilePath)) { FileConverter.SaveFileBuffer(pdfBuffer, pdfFilePath); } FileConverter.PDF2SWF(pdfFilePath, swfFilePath, pdfPageCount, task.IsSplit); repo.UpdateSWFFileID(task.ID, Path.GetFileNameWithoutExtension(task.FileID) + ".swf"); UploadSWFFiles(task.FileID, Path.GetFileNameWithoutExtension(task.FileID), pdfPageCount, task.IsSplit); #endregion #region 生成缩略图 var jpgFileId = directoryPath + mainFileName + ".jpg"; byte[] snapBuffer = FileConverter.ConvertToSnap(pdfBuffer, System.IO.Path.GetExtension(jpgFileId).Trim('.')); string fsJpgFileID = FileStoreHelper.SaveFile(snapBuffer, mainFileName + ".jpg"); repo.UpdateSnapFileID(task.ID, fsJpgFileID); #endregion repo.EndTask(task.ID, "Success"); } catch (Exception ex) { // 记录日志 repo.EndTask(task.ID, "Error", ex.Message); throw ex; } } }