static void Main(string[] args) { ToolEnvironment.CheckDebugOption("shellbridge"); string command = ToolEnvironment.GetSettingString("cmd"); string argument = ToolEnvironment.GetSettingString("arg"); // convert path to local argument = argument.Replace('\\', '/'); try { Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.WorkingDirectory = Directory.GetCurrentDirectory(); startInfo.FileName = command; startInfo.Arguments = argument; startInfo.UseShellExecute = false; startInfo.RedirectStandardError = true; startInfo.RedirectStandardOutput = true; process.StartInfo = startInfo; process.Start(); string output = process.StandardOutput.ReadToEnd(); Console.WriteLine(output); output = process.StandardError.ReadToEnd(); Console.WriteLine(output); } catch (Exception exp) { Console.WriteLine("shellbridge excetpion: {0}", exp.Message); } }
static int Main(string[] args) { ToolEnvironment.CheckDebugOption("copyfile"); string srcPath = ToolEnvironment.GetSettingString("src"); if (string.IsNullOrEmpty(srcPath)) { Console.WriteLine("copyfile: Invalid src parameter is specified: {0}", srcPath); return(2); } string destPath = ToolEnvironment.GetSettingString("dest"); if (string.IsNullOrEmpty(destPath)) { Console.WriteLine("copyfile: Invalid dest parameter is specified : {0}", destPath); return(2); } bool isRecursive = ToolEnvironment.GetSetting <bool>("Recursive"); try { PathTool.CopyFiles(ToolEnvironment.GetSettingString("BASE_PATH") + srcPath, ToolEnvironment.GetSettingString("BASE_PATH") + destPath, isRecursive); } catch (Exception exp) { Console.WriteLine("copyfile failed: exception: {0}", exp.Message); return(3); } return(0); }
static int Main(string[] args) { ToolEnvironment.CheckDebugOption("createpath"); if (args.Length < 1) { Console.WriteLine("CreatePath: No parameter is specified"); return(1); } string path = args[0]; if (string.IsNullOrEmpty(path)) { Console.WriteLine("CreatePath: Invalid parameter is specified"); return(2); } if (!path.EndsWith("/") || !path.EndsWith("\\")) { path += "/"; } PathTool.NormalizePathAndCreate(path); return(0); }
static int Main(string[] args) { ToolEnvironment.CheckDebugOption("httpget"); string outputPath = ToolEnvironment.GetSettingString("O"); string url = ToolEnvironment.GetSettingString("url"); string user = ToolEnvironment.GetSettingString("user"); string password = ToolEnvironment.GetSettingString("password"); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; try { if (string.IsNullOrEmpty(url)) { return(-1); } // Take last part as file name if (string.IsNullOrEmpty(outputPath)) { var lastSlash = url.LastIndexOf('/'); if (lastSlash < 0) { return(-2); } outputPath = url.Substring(lastSlash + 1); } using (var client = new WebClient()) { if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(password)) { client.Credentials = new NetworkCredential(user, password); } System.Console.WriteLine("Downloading {0}", url); client.DownloadFile(url, outputPath); System.Console.WriteLine("Downloaded to {0}", outputPath); } } catch (Exception exp) { Console.WriteLine("httpget excetpion: {0}", exp.Message); } return(0); }
public static void FromDocx(string docxPath, string htmlPath) { var environment = new ToolEnvironment { TempFolderPath = Path.GetTempPath(), ToolPath = $"{nameof(HtmlUtils)}/docx2html/Puppy.DocxToHtml.exe".GetFullPath(null), Timeout = 60000 }; var paramsBuilder = new StringBuilder(); docxPath = docxPath?.GetFullPath(); htmlPath = htmlPath?.GetFullPath(); paramsBuilder.Append($"{docxPath} {htmlPath}"); try { StringBuilder output = new StringBuilder(); StringBuilder error = new StringBuilder(); using (Process process = new Process()) { process.StartInfo.FileName = environment.ToolPath; process.StartInfo.Arguments = paramsBuilder.ToString(); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false)) using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false)) { DataReceivedEventHandler outputHandler = (sender, e) => { if (e.Data == null) { outputWaitHandle.Set(); } else { output.AppendLine(e.Data); } }; DataReceivedEventHandler errorHandler = (sender, e) => { if (e.Data == null) { errorWaitHandle.Set(); } else { error.AppendLine(e.Data); } }; process.OutputDataReceived += outputHandler; process.ErrorDataReceived += errorHandler; try { process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); if (process.WaitForExit(environment.Timeout) && outputWaitHandle.WaitOne(environment.Timeout) && errorWaitHandle.WaitOne(environment.Timeout)) { if (process.ExitCode != 0 && !File.Exists(htmlPath)) { throw new HtmlConvertException($"Html to PDF conversion of '{docxPath}' failed. Puppy.DocxToHtml output: \r\n{error}"); } } else { if (!process.HasExited) { process.Kill(); } throw new HtmlConvertTimeoutException(); } } finally { process.OutputDataReceived -= outputHandler; process.ErrorDataReceived -= errorHandler; } } } } catch { // ignored } }
public static void ToPdf(HtmlToPdf.PdfDocument document, HtmlToPdf.PdfOutput pdfOutput) { var environment = new ToolEnvironment { TempFolderPath = Path.GetTempPath(), ToolPath = $"{nameof(HtmlUtils)}/wkhtml/wkhtmltopdf.exe".GetFullPath(null), Timeout = 60000 }; string outputPdfFilePath; bool delete; if (pdfOutput.OutputFilePath != null) { outputPdfFilePath = pdfOutput.OutputFilePath; delete = false; } else { outputPdfFilePath = Path.Combine(environment.TempFolderPath, $"{Guid.NewGuid():N}.pdf"); delete = true; } if (!File.Exists(environment.ToolPath)) { throw new HtmlConvertException($"File '{environment.ToolPath}' not found. Check if wkhtmltopdf application is installed."); } var paramsBuilder = new StringBuilder(); paramsBuilder.AppendFormat("--page-size {0} ", document.PaperType); if (!string.IsNullOrWhiteSpace(document.HeaderUrl)) { paramsBuilder.AppendFormat("--header-html {0} ", document.HeaderUrl); paramsBuilder.Append("--margin-top 25 "); paramsBuilder.Append("--header-spacing 5 "); } if (!string.IsNullOrWhiteSpace(document.FooterUrl)) { paramsBuilder.AppendFormat("--footer-html {0} ", document.FooterUrl); paramsBuilder.Append("--margin-bottom 25 "); paramsBuilder.Append("--footer-spacing 5 "); } if (!string.IsNullOrWhiteSpace(document.HeaderLeft)) { paramsBuilder.AppendFormat("--header-left \"{0}\" ", document.HeaderLeft); } if (!string.IsNullOrWhiteSpace(document.HeaderCenter)) { paramsBuilder.AppendFormat("--header-center \"{0}\" ", document.HeaderCenter); } if (!string.IsNullOrWhiteSpace(document.HeaderRight)) { paramsBuilder.AppendFormat("--header-right \"{0}\" ", document.HeaderRight); } if (!string.IsNullOrWhiteSpace(document.FooterLeft)) { paramsBuilder.AppendFormat("--footer-left \"{0}\" ", document.FooterLeft); } if (!string.IsNullOrWhiteSpace(document.FooterCenter)) { paramsBuilder.AppendFormat("--footer-center \"{0}\" ", document.FooterCenter); } if (!string.IsNullOrWhiteSpace(document.FooterRight)) { paramsBuilder.AppendFormat("--footer-right \"{0}\" ", document.FooterRight); } if (!string.IsNullOrWhiteSpace(document.HeaderFontSize)) { paramsBuilder.AppendFormat("--header-font-size \"{0}\" ", document.HeaderFontSize); } if (!string.IsNullOrWhiteSpace(document.FooterFontSize)) { paramsBuilder.AppendFormat("--footer-font-size \"{0}\" ", document.FooterFontSize); } if (!string.IsNullOrWhiteSpace(document.HeaderFontName)) { paramsBuilder.AppendFormat("--header-font-name \"{0}\" ", document.HeaderFontName); } if (!string.IsNullOrWhiteSpace(document.FooterFontName)) { paramsBuilder.AppendFormat("--footer-font-name \"{0}\" ", document.FooterFontName); } if (document.ExtraParams != null) { foreach (var extraParam in document.ExtraParams) { paramsBuilder.AppendFormat("--{0} {1} ", extraParam.Key, extraParam.Value); } } if (document.Cookies != null) { foreach (var cookie in document.Cookies) { paramsBuilder.AppendFormat("--cookie {0} {1} ", cookie.Key, cookie.Value); } } paramsBuilder.AppendFormat("\"{0}\" \"{1}\"", document.Url, outputPdfFilePath); try { StringBuilder output = new StringBuilder(); StringBuilder error = new StringBuilder(); using (Process process = new Process()) { process.StartInfo.FileName = environment.ToolPath; process.StartInfo.Arguments = paramsBuilder.ToString(); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false)) using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false)) { DataReceivedEventHandler outputHandler = (sender, e) => { if (e.Data == null) { outputWaitHandle.Set(); } else { output.AppendLine(e.Data); } }; DataReceivedEventHandler errorHandler = (sender, e) => { if (e.Data == null) { errorWaitHandle.Set(); } else { error.AppendLine(e.Data); } }; process.OutputDataReceived += outputHandler; process.ErrorDataReceived += errorHandler; try { process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); if (process.WaitForExit(environment.Timeout) && outputWaitHandle.WaitOne(environment.Timeout) && errorWaitHandle.WaitOne(environment.Timeout)) { if (process.ExitCode != 0 && !File.Exists(outputPdfFilePath)) { throw new HtmlConvertException($"Html to PDF conversion of '{document.Url}' failed. Wkhtmltopdf output: \r\n{error}"); } } else { if (!process.HasExited) { process.Kill(); } throw new HtmlConvertTimeoutException(); } } finally { process.OutputDataReceived -= outputHandler; process.ErrorDataReceived -= errorHandler; } } } if (pdfOutput.OutputStream != null) { using (Stream fs = new FileStream(outputPdfFilePath, FileMode.Open)) { byte[] buffer = new byte[32 * 1024]; int read; while ((read = fs.Read(buffer, 0, buffer.Length)) > 0) { pdfOutput.OutputStream.Write(buffer, 0, read); } } } if (pdfOutput.OutputCallback != null) { byte[] pdfFileBytes = File.ReadAllBytes(outputPdfFilePath); pdfOutput.OutputCallback(document, pdfFileBytes); } } finally { if (delete && File.Exists(outputPdfFilePath)) { FileHelper.SafeDelete(outputPdfFilePath); } } }