Articles in this section
Category / Section

How to convert HTML to PDF using WCF service in .NET WebForms application?

7 mins read

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

Refer to the following steps to convert an HTML to PDF in the WCF service.

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

Steps to create a WCF Service for converting HTML to PDF

  1. Create a new WCF Service project. wcf project application in WinForms PDF

wcf project naming in WinForms PDF

 

  1. Install Syncfusion.HtmlToPdfConverter.WinForms NuGet package as a reference to your WCF service application from NuGet.org.

nuget screenshot in WinForms PDF

  1. Include the HTML to PDF conversion part in the service.
  1. Include the new OperationContract in the IService1 interface.
    [OperationContract]
    byte[] ConvertHtmlToPdf(string url);
    

 

OperationContract in WinForms PDF

 

  1. Include the following namespaces and code samples in Service1.svc for converting HTML to PDF in the 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#

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;

 

public byte[] ConvertHtmlToPdf(string url)
{
       //Initialize HTML to PDF converter. 
       HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
       //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 a simple console sample in the local machine.
  2. After a successful conversion, deploy the WCF service and refer the local service to the main project. This server can be hosted in an IIS server or Azure cloud service. Then, use it in the main project.

Steps for converting HTML to PDF using the above local service.

  1. Create a new console application project. console app in WinForms PDF

console project naming in WinForms PDF

  1. Add a service reference with the above local service in this project. Add service reference in WinForms PDFservice link in WinForms PDF

 

  1. Invoke the ConvertHtmlToPdf method from the service. Refer to the following code sample.
    //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:57491/Service1.svc");
    //Initializing the 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 a PDF document as follows. output

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

WCF Service: HTML_to_PDF_WCF_Service.zip.

Console sample: HTML_to_PDF_Console.zip.

Take a moment to peruse the documentation, where you will find converting HTML pages to PDF documents along with respective customization options and features.

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

An online sample link to convert HTML to PDF document.

 

Note:

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

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 forumsDirect-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 (0)
Please sign in to leave a comment
Access denied
Access denied