125 lines
3.7 KiB
C#
125 lines
3.7 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using RestSharp;
|
|
using SharpCompress.Common;
|
|
using System.Diagnostics;
|
|
|
|
namespace IRaCIS.Core.Application.Helper;
|
|
|
|
public class FileConvertHelper
|
|
{
|
|
|
|
/// <summary>
|
|
/// 镜像里面打入libreoffice 的方案
|
|
/// </summary>
|
|
/// <param name="inputWordFilePath"></param>
|
|
/// <param name="outputPdfDir"></param>
|
|
static public void ConvertWordToPdf(string inputWordFilePath, string outputPdfDir)
|
|
{
|
|
// 设置 libreoffice 命令行参数
|
|
string arguments = $"--headless --invisible --convert-to pdf \"{inputWordFilePath}\" --outdir \"{outputPdfDir}\"";
|
|
|
|
// 启动 libreoffice 进程
|
|
using (Process process = new Process())
|
|
{
|
|
process.StartInfo.FileName = "libreoffice";
|
|
process.StartInfo.Arguments = arguments;
|
|
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
process.StartInfo.RedirectStandardOutput = true;
|
|
process.StartInfo.RedirectStandardError = true;
|
|
process.StartInfo.UseShellExecute = false;
|
|
process.StartInfo.CreateNoWindow = true;
|
|
|
|
process.Start();
|
|
|
|
// 等待进程结束
|
|
process.WaitForExit();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 返回文件字节数组
|
|
/// File.WriteAllBytes(outputFilePath, response.RawBytes); 直接将字节数组写入到本地某个路径
|
|
/// var stream = new MemoryStream(response.RawBytes); 直接变为内存流,给其他程序用也可以
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
static public async Task<byte[]> ThirdStirling_PDFWordToPdfAsync(string filePath)
|
|
{
|
|
|
|
var apiUrl = GetApiUrl();
|
|
|
|
var client = new RestClient(apiUrl);
|
|
|
|
var request = new RestRequest(apiUrl, Method.Post);
|
|
|
|
request.AddHeader("accept", "*/*");
|
|
|
|
// 直接上传文件,无需生成字节数组
|
|
request.AddFile("fileInput", filePath);
|
|
|
|
return await GetPDFByteArrayAsync(client, request);
|
|
}
|
|
|
|
static public async Task<byte[]> ThirdStirling_PDFWordToPdfAsync(byte[] fileBytes, string fileName)
|
|
{
|
|
|
|
var apiUrl = GetApiUrl();
|
|
var client = new RestClient(apiUrl);
|
|
|
|
var request = new RestRequest(apiUrl, Method.Post);
|
|
|
|
request.AddHeader("accept", "*/*");
|
|
|
|
// 添加文件流到请求
|
|
request.AddFile("fileInput", fileBytes, fileName);
|
|
|
|
return await GetPDFByteArrayAsync(client, request);
|
|
}
|
|
|
|
static private async Task<byte[]> GetPDFByteArrayAsync(RestClient client, RestRequest request)
|
|
{
|
|
try
|
|
{
|
|
|
|
|
|
|
|
// 发送请求并获取响应
|
|
var response = await client.ExecuteAsync(request);
|
|
|
|
// 检查请求是否成功
|
|
if (response.IsSuccessful)
|
|
{
|
|
return response.RawBytes ?? new byte[] { };
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"上传失败,错误代码: {response.StatusCode}, 错误消息: {response.ErrorMessage}");
|
|
|
|
return Array.Empty<byte>();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("发生异常: " + ex.Message);
|
|
|
|
return Array.Empty<byte>();
|
|
}
|
|
}
|
|
|
|
static private string GetApiUrl()
|
|
{
|
|
|
|
var enviromentName = Environment.GetEnvironmentVariables()["ASPNETCORE_ENVIRONMENT"]?.ToString();
|
|
|
|
var configuration = new ConfigurationBuilder().AddJsonFile($"appsettings.{enviromentName}.json", false, false).Build();
|
|
|
|
// 手动绑定配置
|
|
var appSettings = new ServiceVerifyConfigOption();
|
|
configuration.GetSection("BasicSystemConfig").Bind(appSettings);
|
|
|
|
return appSettings.ThirdPdfUrl;
|
|
}
|
|
|
|
|
|
}
|