wordtopdf

59 阅读1分钟

        /// <summary>
        /// wordtopdf
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetWordToPdf([FromServices] IWebHostEnvironment environment) {
            var path = GetParameter<string>("path", "").ToString();
            var _path = path.Replace(".pdf", ".docx");
            var filePath = environment.WebRootPath + @"\resource\word\" + _path;
            var savePath = environment.WebRootPath + @"\resource\product\pdf\" + path;
            if (System.IO.File.Exists(filePath) == true) {
                return Json((int)ReturnCode.Succeed, ReturnCode.Succeed.ToString(), "文件已存在");
            }
            var url = MySystemInfo._configuration["FtpConnection:Url"];
            var username = MySystemInfo._configuration["FtpConnection:UserName"];
            var password = MySystemInfo._configuration["FtpConnection:PassWord"];
            WebClient request = new WebClient();
            request.Credentials = new NetworkCredential(username, password);
            try {
                request.DownloadFile($"{url}{DateTime.Now.Year + "//"}{_path}", filePath);
                Document doc = new Document(filePath);
                doc.Save(savePath, SaveFormat.Pdf);
                return Json((int)ReturnCode.Succeed, ReturnCode.Succeed.ToString(), "文件已生成");
            }
            catch (Exception ex) {
                return Json((int)ReturnCode.Fail, ReturnCode.Fail.ToString(), "暂无文档");
            }

        }