private void OnException(List <string> message, WKHtmltopdfParameters parameters, int exitCode, Exception caughtException) { var exceptionMessage = GetExceptionMessage(message); var excetpion = new WKHtmltopdfException(exceptionMessage, caughtException, exitCode); OnConversionError(new ConversionErrorEventArgs(excetpion, parameters.InputFiles, parameters.OutputFile)); }
public async Task ExecuteAsync(string arguments, CancellationToken cancellationToken = default) { var parameters = new WKHtmltopdfParameters { CustomArguments = arguments }; await ExecuteAsync(parameters, cancellationToken); }
public async Task ExecuteAsync(WKHtmltopdfParameters parameters, string wkhtmptopdfPath, CancellationToken cancellationToken = default) { var argumentBuilder = new WKHtmltopdfArgumentBuilder(); var arguments = argumentBuilder.Build(parameters); var startInfo = GenerateStartInfo(wkhtmptopdfPath, arguments); await ExecuteAsync(startInfo, parameters, cancellationToken); }
private void WKHtmltopdfProcessOutputDataReceived(ConversionDataEventArgs e, WKHtmltopdfParameters parameters, List <string> message) { if (e.Data == null) { return; } message.Add(e.Data); }
private async Task ExecuteAsync(WKHtmltopdfParameters parameters, CancellationToken cancellationToken = default) { var wkhtmltopdfProcess = new WKHtmltopdfProcess(); wkhtmltopdfProcess.Progress += (e) => Progress?.Invoke(this, e); wkhtmltopdfProcess.Completed += (e) => Complete?.Invoke(this, e); wkhtmltopdfProcess.Error += (e) => Error?.Invoke(this, e); wkhtmltopdfProcess.Data += (e) => Data?.Invoke(this, e); await wkhtmltopdfProcess.ExecuteAsync(parameters, _wkhtmltopdfPath, cancellationToken); }
public async Task <string> GetVersionAsync(CancellationToken cancellationToken = default) { var parameters = new WKHtmltopdfParameters { Task = Enums.WKHtmltopdfTask.Version }; await ExecuteAsync(parameters, cancellationToken); return(parameters.OutputMessage); }
public async Task <ConvertFile> ConvertAsync(List <InputBase> inputs, ConvertFile output, GlobalOptions globalOptions, PageOptions pageOptions, CancellationToken cancellationToken = default) { var parameters = new WKHtmltopdfParameters { Task = Enums.WKHtmltopdfTask.Convert, InputFiles = inputs.ToArray(), OutputFile = output, GlobalOptions = globalOptions, PageOptions = pageOptions }; await ExecuteAsync(parameters, cancellationToken); return(parameters.OutputFile); }
private async Task ExecuteAsync(ProcessStartInfo startInfo, WKHtmltopdfParameters parameters, CancellationToken cancellationToken = default) { var message = new List <string>(); var successMessage = new List <string>(); Exception caughtException = null; using (var process = new Process() { StartInfo = startInfo }) { process.ErrorDataReceived += (sender, e) => OnData(new ConversionDataEventArgs(e.Data, parameters.InputFiles, parameters.OutputFile)); process.ErrorDataReceived += (sender, e) => WKHtmltopdfProcessOnErrorDataReceived(e, parameters, ref caughtException, message); process.OutputDataReceived += (sender, e) => WKHtmltopdfProcessOutputDataReceived(new ConversionDataEventArgs(e.Data, parameters.InputFiles, parameters.OutputFile), parameters, successMessage); Task <int> task = null; try { task = process.WaitForExitAsync(null, cancellationToken); await task; } catch { if (task.IsCanceled) { throw new TaskCanceledException(task); } throw; } if (caughtException != null || process.ExitCode != 0) { OnException(message, parameters, process.ExitCode, caughtException); } else { parameters.OutputMessage = string.Join(";", successMessage); OnConversionCompleted(new ConversionCompleteEventArgs(parameters.InputFiles, parameters.OutputFile, parameters.OutputMessage)); } } }
private void WKHtmltopdfProcessOnErrorDataReceived(DataReceivedEventArgs e, WKHtmltopdfParameters parameters, ref Exception exception, List <string> message) { var totalMediaDuration = new TimeSpan(0, 0, 120); if (e.Data == null) { return; } try { message.Insert(0, e.Data); //if(parameters.InputFile!=null) //{ //} var progreeData = new ProgressData(TimeSpan.FromSeconds(10), TimeSpan.FromMinutes(10), null, null, null, null); OnProgressChanged(new ConversionProgressEventArgs(progreeData, parameters.InputFiles, parameters.OutputFile)); } catch (Exception ex) { exception = ex; } }