Exemplo n.º 1
0
        public static IPDFTaskRepository GetRepository()
        {
            if (repo == null)
            {
                var repoType = ConfigurationManager.AppSettings["PDFTaskRepositoryType"];
                if (!string.IsNullOrEmpty(repoType))
                {
                    Type type = Type.GetType(repoType, true, true);
                    repo = Activator.CreateInstance(type) as IPDFTaskRepository;
                }
                else
                {
                    throw new Exception("请配置PDFTaskRepositoryType节");
                }
            }

            return(repo);
        }
Exemplo n.º 2
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;
                }
            }
        }