wkhtmltopdf / wkhtmltopdf

Convert HTML to PDF using Webkit (QtWebKit)
https://wkhtmltopdf.org
GNU Lesser General Public License v3.0
13.96k stars 1.82k forks source link

System.DivideByZeroException error when PagesCount = 0 #4177

Closed fabs-rodriguez closed 5 years ago

fabs-rodriguez commented 5 years ago

wkhtmltopdf version(s) affected: 0.12.4.0

OS information Windows 10 Home version 1809, 64 bit.

Description On DinkToPdf, when setting PagesCount = false in ObjectSettings in an ASP .Net Core 2.1 Web API, and the Convert() method is called, I get the following exception:

System.DivideByZeroException: Attempted to divide by zero.

Stack Trace:

_at DinkToPdf.WkHtmlToXBindings.wkhtmltopdf_convert(IntPtr converter) at DinkToPdf.PdfTools.DoConversion(IntPtr converter) in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\PdfTools.cs:line 116 at DinkToPdf.BasicConverter.Convert(IDocument document) in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\BasicConverter.cs:line 53 at DinkToPdf.SynchronizedConverter.<>n0(IDocument document) at DinkToPdf.SynchronizedConverter.<>cDisplayClass5_0.b_0() in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\SynchronizedConverter.cs:line 27 at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

According to DinkToPdf it's a bug in wkhtmltopdf: https://github.com/rdvojmoc/DinkToPdf/issues/22

How to reproduce Set PagesCount = false in ObjectSettings, and call Convert() method:

            var globalSettings = new GlobalSettings
            {
                ColorMode = ColorMode.Color,
                Orientation = Orientation.Portrait,
                PaperSize = PaperKind.A4,
                Margins = new MarginSettings { Top = 10 },
                DocumentTitle = "test",
            };

            var objectSettings = new ObjectSettings
            {
                PagesCount = false,
                HtmlContent = html,
                WebSettings = { DefaultEncoding = "utf-8" },
            };

            var pdf = new HtmlToPdfDocument()
            {
                GlobalSettings = globalSettings,
                Objects = { objectSettings }
            };

            byte[] file = _converter.Convert(pdf);

Expected behavior It should generate the PDF without the page count at the top.

Tomsgu commented 5 years ago

Hi @fabs-rodriguez, this is wkhtmltopdf project, not DinkToPdf.

fabs-rodriguez commented 5 years ago

Thanks for the reply Tomsgu. Yes, this happened on DinkToPdf. But DinkToPdf uses wkhtmltopdf, and according to https://github.com/rdvojmoc/DinkToPdf/issues/22 the bug is in wkhtmltopdf?

Tomsgu commented 5 years ago

Can you please then post an example of the CLi command that creates the issue? Not a wrapper code.