DinkToPdf icon indicating copy to clipboard operation
DinkToPdf copied to clipboard

PDF generation reaches up to 100% and prints after 4~5 minutes

Open matheusavi opened this issue 7 years ago • 10 comments

ASP.NET core version: 1.1.2.

On other machines this code works normally. I've restarted and tried various configurations without success. I also printed an empty document, with no header and footer.

Startup.cs

services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

Class that generates the report

//On constructor
_converter = new SynchronizedConverter(new PdfTools());


//On the method
var  _diarioDeOperacao = new HtmlToPdfDocument()
{
    GlobalSettings = {
        ColorMode = ColorMode.Color,
        Orientation = Orientation.Portrait,
        PaperSize = PaperKind.A4Plus
    }
};

_diarioDeOperacao.Objects.Add(new ObjectSettings()
{
        PagesCount = true,
        HtmlContent = "Teste",
        WebSettings = { DefaultEncoding = "utf-8", LoadImages = false },
        UseExternalLinks = false
});
_converter.Error += (sender, args) => _logger.LogError("Erro no relatório: Diário de operações\n" + args.Message);
_converter.Warning += (sender, args) => _logger.LogWarning("Warning no relatório: Diário de operações\n" + args.Message);
_converter.ProgressChanged += (sender, args) => _logger.LogWarning(args.Description);
_converter.Finished += (sender, args) => _logger.LogWarning("Sucesso " + args.Success);

//when debugging, here is where it hangs
byte[] pdfBytes = _converter.Convert(_diarioDeOperacao);


Response.ContentType = "Application/pdf";
Response.Headers.Remove("Content-Disposition");
Response.Headers.Add("Content-Disposition", $@"attachment; filename=Diario de Operacoes {DateTime.Now.ToString()}.pdf");
var buffer = pdfBytes;
Response.StatusCode = 200;

Response.Body.Write(buffer, 0, buffer.Length);
Response.Body.Flush();
Response.Body.Dispose();

Console output

#when the method is called it produces
Qt: Could not initialize OLE (error 80010106) 
warn: PortoImbituba.Controllers.AccountController[0] 0%
warn: PortoImbituba.Controllers.AccountController[0] 10%
warn: PortoImbituba.Controllers.AccountController[0] 50% 
warn: PortoImbituba.Controllers.AccountController[0] 100%
   
#after 4 minutes and 8 seconds

warn: PortoImbituba.Controllers.AccountController[0] Object 1 of 1
warn: PortoImbituba.Controllers.AccountController[0] Object 1 of 1
warn: PortoImbituba.Controllers.AccountController[0] Preparing
warn: PortoImbituba.Controllers.AccountController[0] Page 1 of 1
warn: PortoImbituba.Controllers.AccountController[0] Sucesso True   

matheusavi avatar Jun 08 '18 18:06 matheusavi

I had a similar problem a while ago.

You're not using the the converter you added as singleton in Startup.cs. You're creating a new SynchronizedConverter and PdfTools every time your class instantiates. I think this causes issues because you're ending up creating multiple instances of PdfTools without disposing them. (PdfTools implements IDisposable).

I would change your constructor to retrieve the converter from the services by dependency injection instead.

Perhaps something like this:

public MyClassName(IConverter converter)
{
  _converter = converter;
}

erwindamsma avatar Jun 10 '18 21:06 erwindamsma

I did what you said. It worked for 30 minutes. Then I had the same effect, including with the test project (DinkToPdf.TestWebServer).

matheusavi avatar Jun 11 '18 12:06 matheusavi

Interestingly, it works on IIS. The client has been using it for some time and I have done several tests on my machine.

matheusavi avatar Jun 11 '18 19:06 matheusavi

As erwindamsma said, I put as Singleton and works fine.

lkolodziey avatar Sep 04 '18 13:09 lkolodziey

It seems to be a problem with https://github.com/wkhtmltopdf/wkhtmltopdf , because it ocours in other librarys that use wkhtmltopdf.

matheusavi avatar Sep 04 '18 14:09 matheusavi

My Problem is, the first time it works perfectly. If I start the second document to convert, it will never end. Any Solutions?

dotnet core version: 2.1.3

psychos4 avatar Sep 14 '18 09:09 psychos4

@psychos4 For example I created a web application using dotnet core 2.1, take a look because when generate pdf I can push button many times and many PDFs are created. https://github.com/lkolodziey/UsingDinkToPdf_Example

lkolodziey avatar Sep 14 '18 13:09 lkolodziey

Hi, @lkolodziey Have you solved your problem? I am facing the same problem with Dotnet Core SDK 3.0.100-preview5-011568.

ghost avatar Jun 03 '19 04:06 ghost

We changed to a java API with jasper.

matheusavi avatar Jun 03 '19 09:06 matheusavi

Well, it seems to be problem with the DinkToPdf...

I guess that is something about making a singleton instance receive a IDisposable class (PdfTools)

I just made this code and worked like a charm:

      using(var tools  = new PdfTools())
            {
                var _pdfConverter = new SynchronizedConverter(tools);


                if(!_pdfConverter.Tools.IsLoaded){
                    _pdfConverter.Error += (object sender, ErrorArgs e) => logger.LogError(e.Message);
                    _pdfConverter.Finished += (object sender, FinishedArgs e) => logger.LogInformation(e.Success.ToString());
                    _pdfConverter.Warning += (object sender, WarningArgs e) => logger.LogWarning(e.Message);
                    _pdfConverter.ProgressChanged += (object sender, ProgressChangedArgs e) => logger.LogInformation(e.Description);
                    _pdfConverter.PhaseChanged += (object sender, PhaseChangedArgs e) => logger.LogInformation(e.Description);
                
                }
                
                var doc = new HtmlToPdfDocument()
                {
                    Objects =
                    {

                        new ObjectSettings
                        {
                            HtmlContent = html,

                        }
                    }
                };
                byte[] pdfBytes = _pdfConverter.Convert(doc); 
            }

gabrielcapanogyp avatar Apr 07 '21 00:04 gabrielcapanogyp