2

I am attempting to generate a multi-page pdf document in my ASP.Net application using wkhtmltopdf by passing in multiple URLs.

Here is the code that gets called -

Public Shared Function WKHtmlToPdf(ByVal url As String, ByVal OutputDir As String, ByVal OutputFileName As String) As Boolean

    Dim blnReturn As Boolean = False
    Dim wkhtml As String = HttpContext.Current.Server.MapPath("~/resources/wkhtmltopdf/wkhtmltopdf.exe")
    Dim p As New Process()

    p.StartInfo.CreateNoWindow = True
    p.StartInfo.RedirectStandardOutput = True
    p.StartInfo.RedirectStandardError = True
    p.StartInfo.RedirectStandardInput = True
    p.StartInfo.UseShellExecute = False
    p.StartInfo.FileName = wkhtml
    p.StartInfo.WorkingDirectory = OutputDir

    p.StartInfo.Arguments = url & " " & OutputFileName
    p.Start()
    p.WaitForExit(60000)

    Dim returnCode As Integer = p.ExitCode
    p.Close()

    If returnCode = 0 Then
        blnReturn = True
    End If

    Return blnReturn

End Function

Now the problem I am having is it errors at

p.WaitForExit(60000)

with the following error message.

Process must exit before requested information can be determined.

However, this error only seems to occur when I pass in more than 4 or 5 URLs, it seems random, sometimes its less, sometimes its more. This causes the process to take a long time to generate the PDF and thus error at p.WaitForExit...

I wondered if there some kind of Request limit being hit? I can generate the PDF with as many URLs I need when using the command prompt directly.

1 Answers1

2

The issue is the process is waiting for the exit code which is never returned under normal settings (60 secs).

If you are using the latest version, it supports 'quiet' parameter, this allows the exit code to be returned correctly.

https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

--disable-javascript --quiet --disable-smart-shrinking --print-media-type --margin-top 10mm --margin-bottom 10mm --margin-right 10mm --margin-left 10mm --page-size A4 "https://www.google.com" google.pdf
Sheng
  • 21
  • 3