How to convert HTML to PDF in Azure using Blink?
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,
Refer to the following steps to convert a HTML to PDF in Azure cloud service:
- Create a Web Role (WCF service) for the conversion part and host it as an Azure cloud service.
- 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:
- Open Visual Studio and create a new Azure Cloud Service project.
- Create an Azure cloud service for the conversion (In this article, WCF service has been used).
- Install the Syncfusion.HtmlToPdfConverter.Blink.AspNet.Mvc5 NuGet package as reference to your WCF service application from NuGet.org.
- 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
- Include the HTML to PDF conversion part in the service.
5.1) Include BlinkBinaries folder to the project.
5.2) Then, set Copy to output directory to copy if newer to all the BlinkBinaries (All files including inner folders and files).
5.3) Include new OperationContract in IService1 interface.
[OperationContract] MemoryStream ConvertHtmlToPdf(string url);
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; }
- Run the service in local machine and test the conversion using simple console sample in local machine.
- 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:
- Right-click the service and select Publish.
- Sign in and select a subscription.
- Provide the Azure publish settings based on the requirement and click Publish.
- 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.
Refer to the following steps for converting HTML to PDF using the above web role service.
- Create a new console project.
- Add a service reference with the published Azure cloud service in this project.
- 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(); }
- By converting HTML to PDF, you will get the PDF document as follows.
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.