2X faster development
The ultimate ASP.NET Core UI toolkit to boost your development speed.
Syncfusion HTML to PDF for .NET used to convert webpages, SVG, MHTML, and HTML to PDF. Using this library, you can convert HTML to PDF in Azure using .NET Core. Steps to convert HTML to PDF in Azure programmatically:
C# using Syncfusion.HtmlConverter; using Syncfusion.Pdf; using System.IO; using Microsoft.AspNetCore.Hosting;
//To get content root path of the project private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } public IActionResult ExportToPDF() { //Initialize HTML to PDF converter HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); WebKitConverterSettings settings = new WebKitConverterSettings(); //Set WebKit path settings.WebKitPath = Path.Combine(_hostingEnvironment.ContentRootPath, "QtBinariesWindows"); //Assign WebKit settings to HTML converter htmlConverter.ConverterSettings = settings; //Convert URL to PDF PdfDocument document = htmlConverter.Convert("https://www.google.com"); MemoryStream stream = new MemoryStream(); document.Save(stream); return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "Sample.pdf"); }
A complete working sample can be downloaded from HTMLtoPDF_Azure.zip Take a moment to peruse the documentation for Converting HTML to PDF, where you will find various options for URL to PDF, HTML string to PDF, and Hyperlinks. |
2X faster development
The ultimate ASP.NET Core UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.
"HTML to PDF conversion will not work if the hosting plan is Free/Shared."=> why will the html to pdf conversion not work if the hosting plan is free\shared? Can you please advise?
Hi Ajit,
Our HTML to PDF converter internally using QtWebKit rendering engine and we will internally launch the browser process for converting HTML to PDF. Due to the access restrictions and limitation of Consumption/Free/Shared hosting plan, it prevents the loading of the browser process. So, the conversion will be failed in the Consumption/Free/Shared hosting plans on Azure environment. Other hosting plans does not have the restriction for loading the browser process.
Regards,
Gowthamraj K
Hello,
I am getting the following error, after I deploy my application to Azure App Service. The application works correctly on my local machine. I have a S1 hosting plan on Azure. Can you please advise what the possible issue could be? :
PdfException: No such file or directory Syncfusion.HtmlConverter.HtmlConverter.ConvertHtmlToPdf(string url, int width, int height) Syncfusion.HtmlConverter.HtmlToPdfConverter.Convert(string url) HTMLtoPDF_Azure.Controllers.HomeController.ExportToPDF() in C:\Users\AjitGoel\Downloads\Convert HTML to PDF in Azure using .NET Core-Syncfusion\HTMLtoPDF_Azure\HTMLtoPDF_Azure\Controllers\HomeController.cs lambda_method(Closure , object , Object[] ) Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+d12.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+d 10.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+d14.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d 22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d17.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d 15.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+d4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+d 7.MoveNext()
Hi Ajit,
After publishing the application to Azure App service Linux, the converter throws "No such file or directory exception". Please refer below steps to overcome this exception and get the conversion succeeded in Azure App service Linux.
Refer below steps to overcome the no such file or directory exception:
UG: Prerequisites for Linux
Using SSH from Azure portal:
Running the commands from C#:
Notes : Please make sure that the QtBinariesLinux folder is placed in the project folder.
Regards,
Gowthamraj K
Hi Gowthamraj, I am using HTML to PDF converter in my Asp.net Core 2.2 Api project. I have followed all the steps mentioned in this article and it's working perfectly fine in local machine. But I get 500 internal server error when I publish it to Azure and invoke the particular method.
Kindly help me in solving this issue.
Thanks, Subrata
Hi Subrata,
We are tried to reproduce the reported issue with provided details. We have created a simple Web API sample with provided details in Net core 2.2 and checked the conversion in Azure App service. When we host the Web API to Azure App service, we also get the HTTP Error 500 server in our side. This error may occurs due to missing of configuration settings in startup. We can overcome this issue by adding the appsettings.json to startup configuration. We have modified the configuration settings in below sample, now the conversion works properly without any issue. Please find the code snippet for startup.
While hosting the Web API application in Azure app service, we have to manually make the request to the API in the web browser to get the required result. Kindly please try the below sample in your end and let me know the result.
Sample: DotNetCoreApiSample
Please let us know if you need any further assistance on this.
Regards,
Gowthamraj K