We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert HTML to PDF using WCF service

Platform: WinForms |
Control: PDF |
Published Date: January 23, 2017 |
Last Revised Date: April 25, 2019 |
.NET Framework: 4.5
Tags: wcf, htmltopdf

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

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

  1. Create a WCF service for the conversion part and host it as local service.
  2. Then, the local service can be added as a service reference to your main application.

The following steps are used to create a WCF service for conversion:

  1. Open Visual Studio and create a new WCF service project. Create new WCF application
  2. Install the Syncfusion.HtmlToPdfConverter.QtWebKit.WinForms NuGet package as a reference to your WCF service application from the NuGet.org. Install required nuget packages
  3. Copy the QtBinaries folder from the installed HtmltoPdfConverter package and paste it in the folder that contains the WebRoleWebKit_WCFService.csproj file.  QtBinaries path QtBinaries path to copy
  1. Include the HTML to PDF conversion part in the service.

4.1)   Include QtBinaries folder to the project. Include QtBinaries in project

 

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

4.3)   Include new OperationContract in the IService1 interface.

[OperationContract]
byte[] ConvertHtmlToPdf(string url);

OperationContract

4.4)    Include the following namespaces and code snippet in Service1.svc for converting HTML to PDF in WCF service. Refer to the following link for more information.

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

// [C# Code]
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;
using System.Web.Hosting;

 

public byte[] ConvertHtmlToPdf(string url)
{
//Initialize HTML to PDF converter with WebKit rendering engine 
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
WebKitConverterSettings webConverterSettings = new WebKitConverterSettings();
 
//Set the QtBinaries folder path 
webConverterSettings.WebKitPath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "QtBinaries");
 
//Assign WebKit converter settings to HTML converter 
htmlConverter.ConverterSettings = webConverterSettings;
 
//Convert URL to PDF 
PdfDocument document = htmlConverter.Convert(url);
 
MemoryStream ms = new MemoryStream();
// Save and close the document.
document.Save(ms);
document.Close(true);
ms.Position = 0;
return ms.ToArray();
}
  1. Run the service in the local machine and test the conversion using simple console sample in local machine.
  2. After successful conversion, deploy the WCF service and refer the local service to the main project. This server can be hosted in IIS server or Azure cloud service. Then, you can use it in the main project.

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

  1. Create a new console project. Create new Console Project
  2. Add a service reference with the above local service in this project. Add Service reference  Add service reference popup
  3. 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://localhost:64749/Service1.svc");
    //Initializing Service 
    Service1Client client = new Service1Client(binding, remoteAddress);
     
    //Conversion goes here 
    byte[] ms = client.ConvertHtmlToPdf("http://www.google.com/");
     
    Stream stream = new MemoryStream(ms);
     
    //Saving the Output PDF document
    using (FileStream file = new FileStream("Sample.pdf", FileMode.Create, System.IO.FileAccess.Write))
    {
         byte[] bytes = new byte[stream.Length];
         stream.Read(bytes, 0, (int)stream.Length); 
         file.Write(bytes, 0, bytes.Length);
         stream.Close();
    }
    

 

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

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: WebRoleWebKit_WCFService.zip.

Console sample: WebKitConsole_Sample.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.

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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon