How to convert HTML to PDF in UWP using WCF Service?
Syncfusion HTML to PDF converter for .NET library is used to convert webpages, SVG, MHTML, and HTML to PDF. This library converts HTML to PDF in UWP using the WCF service.
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.
- Then, add the local service as a service reference to your UWP application.
Steps to convert HTML to PDF using WCF Service
- Open Visual Studio and 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 new OperationContract in the IService1 interface.
// [C# Code] [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.
// [C# Code]
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;
using System.Web.Hosting;
// [C# Code]
public byte[] ConvertHtmlToPdf(string url)
{
//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert URL to PDF.
PdfDocument document = htmlConverter.Convert(url);
// Save and close the document.
MemoryStream ms = new MemoryStream();
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 UWP project. This server can be hosted in IIS or Azure cloud service. Then, use it in the UWP project.
Steps to convert HTML to PDF in UWP using the local service
- Create a new UWP application.
- Add a service reference with the above local service in this project.
- Add a URL text box and button to convert the URL to PDF from the UWP application.
- Invoke the ConvertHtmlToPdf method from the service. Refer to the following code sample.
// [C# Code] //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:60558/Service1.svc"); //Initializing Service. Service1Client client = new Service1Client(binding, remoteAddress); //Conversion goes here. byte[] ms = await client.ConvertHtmlToPdfAsync("http://www.google.com/"); Stream stream = new MemoryStream(ms); Save(stream, "Sample.pdf");
// [C# Code]
public async void Save(Stream stream, string filename)
{
stream.Position = 0;
StorageFile stFile;
if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.DefaultFileExtension = ".pdf";
savePicker.SuggestedFileName = "Output";
savePicker.FileTypeChoices.Add("Adobe PDF Document", new List<string>() { ".pdf" });
stFile = await savePicker.PickSaveFileAsync();
}
else
{
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
}
if (stFile != null)
{
Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);
Stream st = fileStream.AsStreamForWrite();
st.SetLength(0);
st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
st.Flush();
st.Dispose();
fileStream.Dispose();
MessageDialog messageDialog = new MessageDialog("Do you want to view the Document?", "File created.");
UICommand yesCmd = new UICommand("Yes");
messageDialog.Commands.Add(yesCmd);
UICommand noCmd = new UICommand("No");
messageDialog.Commands.Add(noCmd);
IUICommand cmd = await messageDialog.ShowAsync();
if (cmd == yesCmd)
{
// Launch the retrieved file.
bool success = await Windows.System.Launcher.LaunchFileAsync(stFile);
}
}
}
- By converting HTML to PDF, you will get a PDF document as follows.
The WCF service and UWP samples are attached to this article for your reference. Find the samples from the following zip files.
WCF Service: WCFService_HtmlToPdf.zip
UWP Sample: UWPApp_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 to here to explore the rich set of Syncfusion Essential PDF features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion license key in your application to use the components without a trial message.