question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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   

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
erwindamsmacommented, Jun 10, 2018

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;
}
1reaction
gabrielcapanogypcommented, Apr 7, 2021

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); 
            }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Handing EXTREMELY Large Strings in PHP When ...
I found that if my blocks of HTML went over 20,000 characters the PDF would take well over 2 minutes to generate. I...
Read more >
Print to PDF in Outlook Creates Endless Notepad Files w
I am creating a document in Word, then printing it to PDF. Then, Note pad explodes with an endless creation of the below...
Read more >
How to AUTOMATICALLY Fill PDF Forms Using ... - YouTube
GET THIS +300 OF MY BEST TEMPLATES HERE▻ https://bit.ly/300WKBK-Desc BRING AI INTO EXCEL WITH THIS INCREDIBLE ADD- IN ▻ ...
Read more >
Printing PDF's, and how to size and resize. - YouTube
How to print a PDF pattern or document and how to resize it if you wish. How to print pdf files from computer...
Read more >
Known issues with PaperCut MF, NG, Hive ...
PaperCut provides simple and affordable print management software for ... longer than 1 minute to install show as "failed" in the Print Deploy...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found