Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matheusavi opened this issue Jun 8, 2018 · 10 comments
Open

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

matheusavi opened this issue Jun 8, 2018 · 10 comments

Comments

@matheusavi
Copy link

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   
@erwindamsma
Copy link

erwindamsma commented 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;
}

@matheusavi
Copy link
Author

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

@matheusavi
Copy link
Author

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

@lkolodziey
Copy link

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

@matheusavi
Copy link
Author

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

@psychos4
Copy link

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

@lkolodziey
Copy link

@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

@ghost
Copy link

ghost commented Jun 3, 2019

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

@matheusavi
Copy link
Author

We changed to a java API with jasper.

@gabrielcapanogyp
Copy link

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); 
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants