I have a web-application built in ASP.NET Webforms VB.NET. I am creating a method for exporting the pages via urls utilising the WKHTMLTOPDF and NReco PDF Generator libraries.
During my local development, everything seems to work ok, other than the odd "Process is already in use" when i have rebooted my PC and not even performed anything prior.
When I have uploaded the site to our server, the PDF library seems to never work and I am faced with a not so obvious error. When I attempt to export any page to anything, or even text to a pdf, I get this within our logs:
Inner Exception Type: NReco.PdfGenerator.WkHtmlToPdfException
Inner Exception: Exit with code 1, due to unknown error. (exit code: 1)
Inner Source: NReco.PdfGenerator
Inner Stack Trace:
at NReco.PdfGenerator.HtmlToPdfConverter.CheckExitCode(Int32 exitCode, String lastErrLine, Boolean outputNotEmpty)
at NReco.PdfGenerator.HtmlToPdfConverter.InvokeWkHtmlToPdf(PdfSettings pdfSettings, String inputContent, Stream outputStream)
at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(WkHtmlInput[] htmlFiles, String inputContent, String coverHtml, String outputPdfFilePath, Stream outputStream)
Exception Type: System.Exception
Exception: Cannot generate PDF: Exit with code 1, due to unknown error. (exit code: 1)
Source: Exporting
Stack Trace:
at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(WkHtmlInput[] htmlFiles, String inputContent, String coverHtml, String outputPdfFilePath, Stream outputStream)
at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent, String coverHtml, String outputPdfFilePath)
at PROJECT.Export.DownloadFiles(List`1 list) in C:\Users\NAME\source\repos\PROJECT\Export.aspx.vb:line 252.
I have ensured the project contains all the relevant files, I have even installed WKHTMLTOPDF on both server and development machine. Utilising the CMD I can generate ANY PDF but within my application, it seems to always fail on our live production server.
My code for generating the PDF is quite simplistic:
If check = True Then
Try
'Generate PDF
'htmlToPdf.GeneratePdfFromFile(exp.URL, Nothing, "..\" & pathURL)
htmlToPdf.GeneratePdf("<h3> TEST PDF </h3> ", Nothing, "..\" & pathURL)
'Set download
download = True
'Catch exception, log it
Catch ex As Exception
ExceptionUtility.LogException(ex, "Exporting")
End Try
End If
Can anyone offer any insight as to how I can overcome this? I spent all day yesterday searching through forums and not coming to a solution!
Any help is highly appreciated.
Exit code 1 means that wkhtmltopdf was unable to render your HTML input for some reason. To get more details (full wkhtmltopdf's console log output) you may handle
LogReceived
event.PdfGenerator executes wkhtmltopdf with System.Diagnostics.Process API, so it should work exactly like if you run it (on the same server) in the command line. Differences may occur if this is ASP.NET app hosted in IIS, and in some cases app pool may be configured to use rather restrictive credentials.