Html to PDF Converter - FileNotFoundException System.Windows.Forms

Trying to set up the Html to PDF Converter in an Azure Function.  Had it working locally using Blink libraries, then read that Blink doesn't work in an Azure Function, so I tried switching to WebKit.  When I switched to WebKit, I am getting a System.IO.FileNotFoundException 'Could not load file or assembly System.Windows.Forms'​.


This is the line that I'm receiving the error on... (where doc is of type PdfDocument​, and myStream​ is of type either MemoryStream​ or FileStream​ - neither seem to work).

doc.Save(myStream);


Now, if I try to switch back to Blink libraries that isn't working either.  I get the same exception (looking for System.Windows.Forms) but on this line instead:

HtmlToPdfConverter converter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);


The Nuget package I have installed is Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core.  I have spent so much time troubleshooting this and looking through the various Syncfusion tutorials to get this working.  Can't figure out what has gone wrong.  At this point I just want to get it working locally, then I'll deal with the issues that people seems to have getting it to work directly in Azure.


Environment:

  • Windows 10
  • .NET 6
  • Visual Studio 2022
  • Function Runtime Version ~4

5 Replies 1 reply marked as answer

GK Gowthamraj Kumar Syncfusion Team April 15, 2022 12:02 PM UTC

Hi AB,


We did not face this type of exception before. We have created a simple Net 6.0 sample for converting Html to PDF documents using the WebKit rendering engine, it works properly and generates a PDF document in both memory stream and file stream. We request you to refer to the same product version of Syncfusion assemblies across different projects (dependent ones). We have attached the sample with the output document for your reference, please try the below sample on your end and let us know the result.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/HtmlNet6.01959953116


Please refer to the below documentation,
UG: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/webkit#url-to-pdf

Blogs: https://www.syncfusion.com/blogs/post/html-to-pdf-conversion-in-csharp.aspx


Please let us know if you need any further assistance in this.


Regards,

Gowthamraj K



AB AB replied to Gowthamraj Kumar April 16, 2022 03:03 PM UTC

Thank you for the sample. I had a look and I have everything set up exactly the same. What I ended up doing was creating an entirely new Azure Functions project in Visual Studio and then copied all of my classes over to the new project, installed the appropriate Nuget packages, and now it's working again locally. So something must have been corrupt in the old project, especially since I had it working at one point.


Now however, I am unable to get it working when published to Azure. I am getting the dreaded "Html conversion failed" message. I am on a B1 app service plan, have set to 64-bit runtime, and I have confirmed in the console that the "bin\QtBinariesWindows" folder has all of the files copied into it. Any idea what else may be causing such an issue when it is working fine locally?


Marked as answer

GK Gowthamraj Kumar Syncfusion Team April 18, 2022 09:58 AM UTC

Hi AB,

We have further analyzed about the reported issue, it is occurring due to QtBinariesWindows folder is not copied properly to the Azure SDK. So, the reported HTML Conversion failed exception occurs.


We can resolve this issue by copying all the files in the QtBinariesWindows and set the proper path. Please ensure all the files and inner folder are properly copied in Azure server using console in Azure portal. If it is not copied, kindly add all the files manually to resolve that issue. This is not an issue in our library.  Please follow the below steps to copy the files properly to resolve the reported issue.  

  1. Open the Azure portal in browser.
  2. Navigate to the deployed Azure function in Azure portal.
  3. Open the console of Azure function.

              

  

  1. As we said earlier, QtBinaries assemblies are not copied properly while publish to Azure functions.
  2. So, we need to manually copy all the assemblies from azure portal.
  3. QtBinariesWindows from location “c:\home\site\wwwroot\QtBinariesWindows” does not have all the assemblies, so we need to copy all the assemblies and inner folder from bin folder.
  1. Using cd command, navigate to the location “c:\home\site\wwwroot\bin\QtBinariesWindows” and run the below command.

cp *.* c:\home\site\wwwroot\QtBinariesWindows\ 


  


  1. Then we need to copy the files from “platform” and “imageformats” folders using the same approach.
  1. Using cd command, navigate to the location “c:\home\site\wwwroot\bin\QtBinariesWindows\platforms” and run the below command.

cp *.* c:\home\site\wwwroot\QtBinariesWindows\platforms 


  1. Using cd command, navigate to the location “c:\home\site\wwwroot\bin\QtBinariesWindows\imageformats” and run the below command.

cp *.* c:\home\site\wwwroot\QtBinariesWindows\imageformats 


  1. Ensure the all the files are copied to “c:\home\site\wwwroot\QtBinariesWindows” location using dir command. Also, ensure the inner folders (imageformats, platforms) has all the files.

            

  1. Now try the conversion from Azure function URL.
  2. We need to copy this assemblies for the first time only.

Please refer to the below links to troubleshooting the WebKit HTML converter.   

Troubleshooting: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/webkit#troubleshooting   


Please refer the below KB steps for more information,

Azure (V2)https://www.syncfusion.com/kb/10301/how-to-convert-html-to-pdf-in-azure-functions-2-0  


Our HTML converter internally creates and launches a surrogate process from a temporary folder. So, the converter is required to read/write and execute permission of the temporary folder. So, please check the temporary folder has read/write/execute permission for the respective user group. For converting HTML to PDF in Azure, we internally launch the browser process from the QtBinariesWindows folder. We need to publish the function without a run from the package option (refer below screenshot) and let us know the result.



Please try the above suggestion and let us know the result.


Regards,

Gowthamraj K



AB AB replied to Gowthamraj Kumar April 18, 2022 05:23 PM UTC

Thank you - the trick here was not running the function from a package file so that I could complete the commands in the console.  Everything is working well now.



GK Gowthamraj Kumar Syncfusion Team April 19, 2022 06:12 AM UTC

Hi AB,


Thank you for your update. We are glad to know that your problem has been solved. Please let us know if you require further assistance with this.


Regards,

Gowthamraj K


Loader.
Up arrow icon