Articles in this section
Category / Section

How to convert HTML to PDF in Azure using Blink?

9 mins read

Syncfusion HTML to PDF for .NET is used to convert webpages, SVG, MHTML, and HTML to PDF. Using this library, you can convert HTML to PDF in Azure using C# and VB.NET.

 

We can use the Html to PDF converter with .NET Core library in Azure App Service Linux or Azure Functions Linux. Please refer below links for more information,

Azure App Service Linux: https://www.syncfusion.com/kb/12909/how-to-convert-html-to-pdf-in-azure-app-service-linux-with-blink-rendering-engine

Azure Functions Linux: https://www.syncfusion.com/kb/12908/how-to-convert-html-to-pdf-using-blink-in-azure-functions-linux

 

Blink rendering engine supports conversion in the Linux docker container, and it can be deployed as Azure website using the Azure container instance. Please refer to the following KB link for more information,

https://www.syncfusion.com/kb/11472/how-to-convert-html-to-pdf-in-azure-app-service-using-the-blink-with-linux-docker

 

Refer to the following steps to convert a HTML to PDF in Azure cloud service:

  1. Create a Web Role (WCF service) for the conversion part and host it as an Azure cloud service.
  2. Then, the Web Role (Azure cloud service) can be added as a service reference in your main application.

The following steps are used to create a Web Role for conversion:

  1. Open Visual Studio and create a new Azure Cloud Service project.

Create Azure Cloud service

  1. Create an Azure cloud service for the conversion (In this article, WCF service has been used).

 

Select WCF service Role

 

  1. Install the Syncfusion.HtmlToPdfConverter.Blink.AspNet.Mvc5 NuGet package as reference to your WCF service application from NuGet.org. Install required Nuget Packages

 

  1. Copy the BlinkBinaries folder from the installed HtmltoPdfConverter package and paste it in the folder that contains the WCFServiceWebRole1.csproj file. Refer to the following link for pre-requisites of Blink rendering engine. 

UG: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/blink#prerequisites

 

BlinkBinaries loaction

Paste BlinkBinaries to this location

 

  1. Include the HTML to PDF conversion part in the service.

5.1) Include BlinkBinaries folder to the project.

Include BlinkBinaries in project

5.2) Then, set Copy to output directory to copy if newer to all the BlinkBinaries (All files including inner folders and files).

Copy the output directory

5.3) Include new OperationContract in IService1 interface.

[OperationContract]
MemoryStream ConvertHtmlToPdf(string url);

Convert HTML to PDF

5.4) Include the following namespaces and code snippet in Service1.svc for converting HTML to PDF in Azure, refer to the following link for more information.

UG: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/blink#url-to-pdf

C#:

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;
using System.Web.Hosting;
public MemoryStream ConvertHtmlToPdf(string url)
{
//Initialize HTML to PDF converter with Blink rendering engine 
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
 
//Set the BlinkBinaries folder path 
blinkConverterSettings.BlinkPath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "BlinkBinaries");
 
//Assign Blink converter settings to HTML converter 
htmlConverter.ConverterSettings = blinkConverterSettings;
//Convert URL to PDF 
PdfDocument document = htmlConverter.Convert(url);
MemoryStream stream = new MemoryStream();
 
//Save and close the PDF document  
document.Save(stream);
document.Close(true);
 
stream.Position = 0;
return stream;
}

 

  1. Run the service in local machine and test the conversion using simple console sample in local machine.
  2. After successful conversion, deploy the service to Azure cloud service and refer the service to the main project.
    Note:

    The Newtonsoft.Json package version is not 6.0.8, then include the following assembly binding redirection in the web.config file.

 

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Refer to the following steps to publish the service to Azure could service:

  1. Right-click the service and select Publish.

Publish the project

  1. Sign in and select a subscription.

Microsoft Azure Account sign in

  1. Provide the Azure publish settings based on the requirement and click Publish.

Provide azure settings and publish it.

Publish summary

  1. After publishing the application successfully, you can refer the service from the published URL and convert the HTML to PDF using the published service URL.

Published url details

Refer to the following steps for converting HTML to PDF using the above web role service.

  1. Create a new console project.

Create new console application

  1. Add a service reference with the published Azure cloud service in this project.

Add service reference

  1. Invoke the ConvertHtmlToPdf method from the service. Refer to the following code snippet.
    //Initializing Basic HTTP Binding
    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
    binding.MaxReceivedMessageSize = int.MaxValue;
    binding.MaxBufferPoolSize = 26843545600;
    binding.MaxBufferSize = int.MaxValue;
    binding.SendTimeout = TimeSpan.MaxValue;
    binding.ReceiveTimeout = TimeSpan.MaxValue;
    binding.OpenTimeout = TimeSpan.MaxValue;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
    EndpointAddress remoteAddress = new EndpointAddress("http://azureblinktest.cloudapp.net/Service1.svc");
    //Initializing Service 
    Service1Client client = new Service1Client(binding, remoteAddress);
    //Conversion goes here 
    MemoryStream ms = client.ConvertHtmlToPdf("https://www.google.com/") as MemoryStream;
    //Saving the Output PDF document
    using (FileStream file = new FileStream("Sample.pdf", FileMode.Create, System.IO.FileAccess.Write))
    {
          byte[] bytes = new byte[ms.Length];
          ms.Read(bytes, 0, (int)ms.Length);
          file.Write(bytes, 0, bytes.Length);
          ms.Close();
    }
    

 

  1. By converting HTML to PDF, you will get the PDF document as follows.

Output PDF document in the C# .Net Application

 

The samples of WCF service and console sample are attached in this article for your reference. Find the samples from the following zip files.

WCF service: AzureCloudServiceBlink.zip.

Console sample: BlinkAzureSample.zip.

 

Refer here to convert HTML to PDF in Azure using WebKit rendering engine.

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.

 

Refer here to explore the rich set of Syncfusion Essential PDF features.

 

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