Closed xiaolei000129 closed 1 year ago
There's probably an issue in how you're using this in your code, you need to make sure you only have one instance of the SynchronizedConverter.
I'm using a static constructor for a static class
It's thread safe. Static constructors are executed only once the first time the class is accessed, and only by one thread in a multithreaded environment. Thus, static constructors initialize data thread-safe.
I was wondering if this was the case, but during the testing process, I found that my expectations met
Looks correct, but please share the code that is causing the problems.
public static class HtmlToPdfHelper { private static IConverter _htmlToPdfConverter = null; static HtmlToPdfHelper() { _htmlToPdfConverter = new SynchronizedConverter(new PdfTools()); }
public static byte[] ConvertHtmlToPdf(HtmlToPdfBase request)
{
if (request.HtmlContent.IsNullOrWhiteSpace() && request.URL.IsNullOrWhiteSpace())
throw new BusinessException("html content can't be equal to empty");
var myWriter = new StringWriter();
HttpUtility.HtmlDecode(request.HtmlContent, myWriter);
var doc = new HtmlToPdfDocument()
{
GlobalSettings =
{
ColorMode = request.ColorMode,
Orientation = request.IsPortrait ? Orientation.Portrait : Orientation.Landscape,
PaperSize = request.PaperSize,
Margins= request.Margins,
},
Objects =
{
new ObjectSettings()
{
PagesCount = request.IsShowPagesCount,
HtmlContent = myWriter.ToString(),
ProduceForms=request.IsGenerateForms,
WebSettings = {
DefaultEncoding = request.DefaultEncoding,
LoadImages = request.IsLoadImage
},
HeaderSettings = {
FontSize = request.FontSize,
Spacing = request.Spacing
},
}
}
};
return _htmlToPdfConverter.Convert(doc);
}
}
The code above is my code
BTW what do you mean with thread getting stuck? So the PDF generation is single-threaded, so it will only allow one PDF to be generated at a time, when it's finished the next will be processed and so on. It doesn't matter how many threads you'll throw at it, it won't run faster.
The phenomenon is that all timeout freezes as long as the method is called
But the first one will eventually complete or? How large are is the HTML content?
The problem is occasional
html size is uncertain
It shouldn't be very big
I would recommend that you add some logging to figure out what is going on, from a quick glance at your code it looks correct. My guess is that you're running a large converter job, and then you're getting timeouts on other requests because the first request is still processing.
Ok, thank you. I'll keep an eye on it
I know what the problem is Because I implemented an htmlto image myself with libwkhtmltox There may be multiple threads accessing the same libwkhtmltox.dll The current solution is to copy a set of libwkhtmltox.dll files for htmlto image
Whether you need to support html to image
Yeah, there can only be one instance (per appdomain). Or change your image method to use the same singleton.
Will you provide html to image implementation?
Otherwise I can only have two sets of instances
I don't think it's in this wrapper currently, but if the native library supports it then I'm sure it's possible to add it, feel free to submit PR.
Uh-huh