You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

90 lines
3.5 KiB

using log4net;
using OT.COM.ArsenalDB;
using System;
using System.Diagnostics;
using System.IO;
namespace CounsellorBL
{
public static class ReportExtension
{
private static ILog _inst = null;
private static ILog Logger
{
get
{
if (_inst == null)
{
_inst = LogManager.GetLogger(typeof(ReportExtension));
}
return _inst;
}
}
/// <summary>
/// 轉換為Pdf
/// </summary>
/// <param name="timeout">轉換逾時秒數</param>
/// <param name="tmpFileFullPath">檔案路徑</param>
/// <returns></returns>
public static string ConvertToPdf(int timeout, string tmpFileFullPath, out string pdfFileFullPath)
{
// 經由轉換程式處理
var pdfProcess = new Process();
pdfProcess.StartInfo.FileName = CustomizeDBMgr.SettingData["PdfConverterFileName"];
// LibreOffice有bug,如果路徑用"\"無法判別,必須改用"/",例如"C:\ABC\"要用"C:/ABC/"
pdfProcess.StartInfo.Arguments = string.Format(CustomizeDBMgr.SettingData["PdfConverterArguments"],
tmpFileFullPath.Replace('\\', '/'),
Path.GetDirectoryName(tmpFileFullPath).Replace('\\', '/'));
pdfProcess.StartInfo.CreateNoWindow = false;
pdfProcess.StartInfo.UseShellExecute = false;
Logger.Info("FileName:"+pdfProcess.StartInfo.FileName+ " "+ pdfProcess.StartInfo.Arguments);
try
{
// PDF開始轉換的時間
var startConvertTime = DateTime.Now;
pdfProcess.Start();
if (pdfProcess.WaitForExit(timeout * 1000)) // 逾時秒數
{
// 取回PDF資料
pdfFileFullPath = Path.ChangeExtension(tmpFileFullPath, ".pdf");
var duration = DateTime.Now - startConvertTime;
//Logger.InfoFormat("轉換PDF [{0}] 花費 {1:n2} 秒 ({2:n2} 毫秒)", pdfFileFullPath, duration.TotalSeconds, duration.TotalMilliseconds);
// 因為lag關係,可能要等一段時間檔案才會真正產生
// 增加一點等待時間
for (var retry = 0; retry < 10; retry++)
{
var exist = File.Exists(pdfFileFullPath);
if (exist)
{
return null; // 回傳null算正確
}
System.Threading.Thread.Sleep(200);
}
// 等候太久,回傳失敗
Logger.Error("無法取回PDF " + pdfFileFullPath);
return "轉換逾時,無法取回PDF";
}
else
{
// 等候太久,可能是轉換失敗
Logger.Error("轉換PDF逾時 " + tmpFileFullPath);
pdfFileFullPath = null;
return "轉換逾時";
}
}
catch (Exception e)
{
Logger.Error("轉換PDF發生錯誤", e);
pdfFileFullPath = null;
return "轉換PDF時發生錯誤" + e.Message;
}
finally
{
pdfProcess.Dispose();
}
}
}
}