public static Web2PDFResponse UrlToPDF(Web2PDFRequest req) { UrlToPdf4CS.PDFParamA parama = new UrlToPdf4CS.PDFParamA(); parama.mbUrl = req.WebURL; parama.mbPdfPath = GetTempPDFFileName(); parama.mbHeaderHtml = req.HeaderPath; parama.mbFooterHtml = req.FooterPath; Web2PDFResponse rsp = new Web2PDFResponse(); rsp.PDFPath = parama.mbPdfPath; try { int nRet = UrlToPdf4CS.djhuUrlToPdfParamA(ref parama); if (nRet == 0) { rsp.ErrorCode = 0; rsp.ErrorInfo = "SUCCESS"; } else { rsp.ErrorCode = nRet; rsp.ErrorInfo = "FAILED"; } } catch (Exception ex) { Logger.LogEvent("", ex.Message); } return(rsp); }
// POST public HttpResponseMessage Post(Web2PDFRequest req) { //UrlToPdf4Web.InitUrlTOPdf4CS(); UrlToPdf4WebExec.InitUrlTOPdf4CS(); //Web2PDFResponse rsp = UrlToPdf4Web.UrlToPDF(req); Web2PDFResponse rsp = UrlToPdf4WebExec.UrlToPDF(req); String jsonString = JsonConvert.SerializeObject(rsp); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(jsonString, Encoding.GetEncoding("UTF-8"), "application/json") }; return(result); }
// GET public HttpResponseMessage Get(String WebURL, String HeaderPath, String FooterPath) { //UrlToPdf4Web.InitUrlTOPdf4CS(); UrlToPdf4WebExec.InitUrlTOPdf4CS(); Web2PDFRequest req = new Web2PDFRequest(); req.WebURL = WebURL; req.HeaderPath = HeaderPath; req.FooterPath = FooterPath; //Web2PDFResponse rsp = UrlToPdf4Web.UrlToPDF(req); Web2PDFResponse rsp = UrlToPdf4WebExec.UrlToPDF(req); String jsonString = JsonConvert.SerializeObject(rsp); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(jsonString, Encoding.GetEncoding("UTF-8"), "application/json") }; return(result); }
public static Web2PDFResponse UrlToPDF(Web2PDFRequest req) { Logger.LogEvent("UrlToPdf4WebExec.UrlToPDF called:", JsonConvert.SerializeObject(req)); Web2PDFResponse rsp = new Web2PDFResponse(); rsp.PDFPath = GetTempPDFFileName(); try { System.Diagnostics.Process process = new System.Diagnostics.Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = exePath; if (!HeaderSpacing.IsEmpty()) { startInfo.Arguments += " --header-spacing " + HeaderSpacing; } if (!FooterSpacing.IsEmpty()) { startInfo.Arguments += " --footer-spacing " + FooterSpacing; } if (!req.HeaderPath.IsEmpty()) { startInfo.Arguments += " --header-html " + req.HeaderPath; } if (!req.FooterPath.IsEmpty()) { startInfo.Arguments += " --footer-html " + req.FooterPath; } startInfo.Arguments += " " + req.WebURL + " " + rsp.PDFPath; startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; process.StartInfo = startInfo; process.Start(); Boolean bExit = process.WaitForExit(20000); if (bExit) { if (process.ExitCode == 0 && File.Exists(rsp.PDFPath)) { rsp.ErrorCode = 0; rsp.ErrorInfo = "SUCCESS"; } else { rsp.ErrorCode = process.ExitCode; rsp.ErrorInfo = "FAILED"; } } else { //超时的时候,会引发异常 rsp.ErrorCode = 1001; rsp.ErrorInfo = "TIMEOUT"; //杀死进程 process.Kill(); } } catch (Exception ex) { rsp.ErrorCode = -1; rsp.ErrorInfo = ex.Message; Logger.LogEvent("UrlToPdf4WebExec Error:", ex.Message); } return(rsp); }