Hi
I am trying to convert html string to pdf byte array using Asynchronous Conversion through
ConvertAsync(String, [Optional] Uri, [Optional] ConversionOptions)
.
Sample codes looks like as shown below:
Main thread function : pass Task<byte[]> to a Thread as parameter
void somefunction(...)
Task<byte[]> convertTask = ConvertHtmlToPdfAsync(htmlBody, msedgePath, conversionOptions);
Thread t = new Thread(new ParameterizedThreadStart(PrintingTimeoutThreadProc));
PrintingParams p = new PrintingParams(printerName, pdfData, pdfFilename, printJobName, NumberOfCopies, PageOrientation, convertTask);
t.Start(p);
...
...
//the functionreturn a Task<byte[]>
public async Task<byte[]> ConvertHtmlToPdfAsync(string htmlBody, ConversionOptions conversionOptions)
{
try
{
byte[] fileData = await Converter.ConvertAsync(htmlBody, basePathUri, conversionOptions);
return fileData;
}
catch (Exception ex)
{
throw new Exception("Task to asynchronously convert HTML to PDF is null.", ex);
}
}
// thread function which wait for task to be finished. Once finished, send the byte array to a printer
private void PrintingTimeoutThreadProc(object data)
{
PrintingParams printingParams = data as PrintingParams;
...
byte[] fileData = null;
if (printingParams.ConvertTask != null)
{
try
{
printingParams.ConvertTask.Wait();
fileData = printingParams.ConvertTask.Result;
if ((fileData == null) || (fileData.Length == 0))
{
WasPrintJobSuccessful = false;
throw new Exception("Failed to convert html to pdf.");
}
}
}
// send
InputPdf inputPdf = LoadPdfData(fileData );
Printer printer = printerDiscovery.LoadPrinter(printingParams.PrinterName, printingParams.IsCitrixEnvironment);
PrintJob printJob = new PrintJob(printer, inputPdf);
printJob.Print();
...
}
User keep printing form to time and it works but somhehow after certain time like 72 hours it startsto throw following exception shown in event viewer:
Log Name: Application
Source: .NET Runtime
Date: 8/13/2024 8:22:40 AM
Event ID: 1026
Task Category: None
Level: Error
Keywords: Classic
User: N/A
Computer: 1611ED127.US.chs.net
Description:
The description for Event ID 1026 from source .NET Runtime cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
If the event originated on another computer, the display information had to be saved with the event.
The following information was included with the event:
Application: Ems.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.Sockets.SocketException
at System.Net.Sockets.Socket.InternalEndConnect(System.IAsyncResult)
at System.Net.Sockets.Socket.EndConnect(System.IAsyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean, System.Net.Sockets.Socket, System.Net.Sockets.Socket, System.Net.Sockets.Socket ByRef, System.Net.IPAddress ByRef, ConnectSocketState, System.IAsyncResult, System.Exception ByRef)
Exception Info: System.Net.WebException
at System.Net.HttpWebRequest.EndGetResponse(System.IAsyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(System.IAsyncResult)
Exception Info: System.Net.Http.HttpRequestException
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
at #Qrh.#Prh+#t1o.#pm()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
at ceTe.DynamicPDF.HtmlConverter.Converter+#ETp.#pm()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading.Tasks.Task)
at ceTe.DynamicPDF.HtmlConverter.Converter+#CTp.#pm()
Exception Info: ceTe.DynamicPDF.HtmlConverter.HtmlConverterException
at ceTe.DynamicPDF.HtmlConverter.Converter+#CTp.#pm()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
at ceTe.DynamicPDF.HtmlConverter.Converter+#gIh+#Jsh.#pm()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
at ceTe.DynamicPDF.HtmlConverter.Converter+#ATp.#pm()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
at MhPdfPrintControl.PrintPdfAsynchronous+<ConvertHtmlToPdfAsync>d__37.MoveNext()
Exception Info: System.AggregateException
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean)
at System.Threading.Tasks.Task.Wait(Int32, System.Threading.CancellationToken)
at MhPdfPrintControl.PrintPdfAsynchronous.PrintingTimeoutThreadProc(System.Object)
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart(System.Object)
1. Environment under which the application is running.: Window 10
2. Operating system details: Name, x86 or x64
3. Exact version and build number of the DynamicPDF.HtmlConverter: 2.2.0.51625
Questions:
1. Does DynamicPDF.HtmlConverter uses http request to send the ask the installed browser specified in ChromiumProcessPath (in WINDOWS IS MsEdge.exe) to do the conversion?
2. The following logs were generated:
----------------------------------------------------------------
8/13/2024 8:22:29 AM
----------------------------------------------------------------
[019168][Warning ][Html ] Retrying after error creating new session: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made