Convert HTML to PDF using WCF service in WF application.
The Syncfusion® HTML to PDF converter for WinForms .NET PDF 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.
- Create a WCF service for the conversion part and host it as a local service.
- 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
- Create a new WCF Service project.
Install Syncfusion.HtmlToPdfConverter.WinForms NuGet package as a reference to your WCF service application from NuGet.org.
- Include the HTML to PDF conversion part in the service.
- Include the new OperationContract in the IService1 interface.
[OperationContract] byte[] ConvertHtmlToPdf(string url);
- 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(); }
- Run the service in the local machine and test the conversion using a simple console sample in the local machine.
- 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.
- Create a new console application project.
- Add a service reference with the above local service in this project.
- 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(); }
- By converting HTML to PDF, you will get a PDF document as follows.
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.
Conclusion
I hope you enjoyed learning about how to convert part of a webpage to PDF in WinForms.
You can refer to our WinForms PDF’s feature
tour
page to know about its other
groundbreaking feature representations. You can also explore our WinForms PDF
documentation to understand how to present
and manipulate data.
For current customers, you can check out our WinForms 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 WinForms PDF and otherWinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!