2

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.

Kyran
  • 21
  • 3
  • this looks like a similar problem - did you check it out? https://stackoverflow.com/questions/52980828/nreco-pdfgenerator-wkhtmltopdfexception-exit-code-1073741521 – Homungus Jul 22 '20 at 12:31
  • I am going to attempt to install any missing C++ files and try again! – Kyran Jul 22 '20 at 13:04
  • FYI The current files I have on the server are in the image: https://imgur.com/a/jQE4vVQ. – Kyran Jul 22 '20 at 13:18
  • Also, as I am unsure, would wkhtmltopdf work via the command line on the server if these files didn't exist? I am able to generate PDFs via the CMD but not through the hosted web app. – Kyran Jul 22 '20 at 13:23
  • @Homungus I managed to install any missing files and I have found no change unfortunately. As I suspected, wkhtmltopdf works via the command line, just not through my hosted site, when my local version works flawlessly. – Kyran Jul 22 '20 at 13:38
  • I'm sorry the link didn't help you :( – Homungus Jul 22 '20 at 13:39
  • @Homungus No need to apologise! This issue is very frustrating though, I cannot find ANY references to my exact error and I have been working on this for 2 days straight, I hope I find it soon. – Kyran Jul 22 '20 at 13:40
  • did you read the techical limitations https://www.nrecosite.com/pdf_generator_net.aspx#faq7? Component cannot be used (does not work) in the following cases: partial-trust environments where starting new processes is prohibited (shared ASP.NET hostings) Windows Servers with restricted access to GDI API: Azure Apps (only Free/Shared subscriptions) are NOT supported. Azure Apps VM-based plans (Basic, Standard, Premium) can use NReco.PdfGenerator with some limitations (avoid usage of custom fonts). .NET platforms where System.Diagnostics.Process API is not available (like UWP/Univeral Apps) – Homungus Jul 22 '20 at 13:44
  • @Homungus I have read this, and everything else on their website. Our server is a dedicated managed server so I believe this is not the reason. Thank you regardless. – Kyran Jul 22 '20 at 15:14

1 Answers1

0

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.

Vitaliy Fedorchenko
  • 8,447
  • 3
  • 37
  • 34
  • Thank you for replying. I will look into this LogReceived Event. We are also hosting this via IIS and not through any other means. – Kyran Jul 22 '20 at 15:48
  • For some reason LogRecieved doesn't seem to exist as a member of HtmlToPdf. I am using NReco for this, but cannot find a way to generate this log you have mentioned. – Kyran Jul 22 '20 at 15:58
  • LogReceived is a member of HtmlToPdfConverter class: https://www.nrecosite.com/doc/NReco.PdfGenerator/?topic=html/E_NReco_PdfGenerator_HtmlToPdfConverter_LogReceived.htm – Vitaliy Fedorchenko Jul 22 '20 at 15:59
  • Thanks for responding, I can see this exists through your website. However, with my code like the following, I cannot see this option in my code... Dim htmlToPdf2 = New NReco.PdfGenerator.HtmlToPdfConverter() htmlToPdf2.LogRecieved - Doesn't exist :( – Kyran Jul 22 '20 at 16:05
  • @Kyran I don't know why you cannot see it, "LogReceived" event is 100% present in HtmlToPdfConverter class (in all versions). – Vitaliy Fedorchenko Jul 23 '20 at 05:39
  • I have found the code within the .dll file however I am very new to working with Events and it's very clear that I have no idea how to implement this in ASP.NET VB. My code is telling me I should be using RaiseEvent and I have no idea on how to work with this at all. Could you provide a very basic example? – Kyran Jul 23 '20 at 07:41
  • I've been able to try and implement something into my app and also secondly a console app but I am not seeing how this is going to work? My code fails on the GeneratePdf line, it won't be able to reach the anything past this point. – Kyran Jul 23 '20 at 09:39
  • @Kyran you need to subscribe on "LogReceived" event _before_ "GeneratePdf" call, of course. Sorry, I'm not familiar with VB and cannot help you. – Vitaliy Fedorchenko Jul 23 '20 at 10:54