Articles in this section
Category / Section

Converting HTML to PDF in Azure using ASP.NET Core PDF

5 mins read

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:

  1. Create a new ASP.NET Core MVC application.
    ejdotnetcore-1501_img1.png
  2. Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package to reference your project using the Package Manager Console.
    package_manager_console_nuget.png
  3. Add a new button in the Index.cshtml as follows.
@{
   Html.BeginForm("ExportToPDF", "Home", FormMethod.Get);
   {
       <div>
           <input type="submit" value="Export To PDF" style="width:200px;height:27px" />
       </div>
   }
   Html.EndForm();
}
  1. Include the following namespaces in HomeController.cs.
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
  1. Add a new action method named ExportToPDF in the HomeController.cs file and include the following code example to convert HTML to PDF document in HomeController.cs.
public IActionResult ExportToPDF() 
{
   //Initialize the HTML to PDF converter. 
   HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);         
   //Convert URL to PDF.
   PdfDocument document = htmlConverter.Convert("https://www.google.com");
   //Saving the PDF to the MemoryStream.
   MemoryStream stream = new MemoryStream();
   document.Save(stream);
   return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "Sample.pdf");
}
  1. Copy the OpenSSL assemblies and paste them into the runtimes/win-x64/native folder containing the HTML_to_PDF_Azure_app_service.csproj file.image.png
  2. Set copy to the output directory as “Copy if newer ”.image.png
  3. Right-click the project and select the Publish option.image.png
  4. Click Add a Publish Profile.image.png
  5. Select the publish target as Azure.image.png
  6. Select the Specific target as Azure App Service (Windows).image.png
  7. Click the Create new option to create a new app service.image.png
  8. Click Create to proceed with the App Service creation.Screenshot (1000).png
  9. Click Finish to finalize the App Service creation.Screenshot (1002).png
  10. Click Close.Screenshot (1003).png
  11. Click Publish.Screenshot (1004).png
  12. Now, publish has succeeded.Screenshot (1005).png
  13. Now, the published website will open in the browser, then you can export the HTML to PDF.Screenshot (1006).png
    By exporting HTML to PDF, you will get the PDF document as follows. image.png

You can download a complete working sample from the 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.

Conclusion

I hope you enjoyed learning about how to convert MVC View to PDF using C#.

You can refer to our ASP.NET Core PDF feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied