Articles in this section
Category / Section

How to convert HTML to PDF in Xamarin platform?

3 mins read

The HTML to PDF converter is a .NET library used for converting webpages, SVG, MHTML, and HTML files to PDF using C#. It uses the famous rendering engine Blink (Google Chrome). It is reliable and accurate. The result preserves all graphics, images, text, fonts, and the layout of the original HTML document or webpage.

The HTML to PDF conversion is supported in the Xamarin platform by using web service.

Refer to the following steps to convert HTML to PDF in web service:

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

Steps to create a WCF service for converting HTML to PDF

1. Create a new WCF service project.

Create a new WCF service project

 

2. In the project configuration windows, name your project and select Create.

Name your project in project configuration window

 

3. Install Syncfusion.HtmlToPdfConverter.WinForms NuGet package as a reference to your web service application from the NuGet.org.

NuGet package installation

4. Include the new OperationContract in the IService1 interface.       

[OperationContract]
byte[] GetData(string value);

Add operation Contract

5. Include the following namespaces and code samples in Service1.svc for converting HTML to PDF in the web service. For more information, refer to the following link.

Convert URL to PDF

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

 

public byte[] GetData(string value)
{
    return CreateDocument(value);
}
 
public byte[] CreateDocument(string pathURL)
{
//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
 
//Convert URL to pdf.
PdfDocument document = htmlConverter.Convert(pathURL);
 
//Save the document.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Sets the stream position.
stream.Position = 0;
//Close the PDF document.
document.Close();
 
//Return the PDF stream.
return stream.ToArray();
}

6. Run the service in the local machine and test the conversion using a simple console sample in the local machine.

After the successful conversion, deploy the web service and refer the local service to the main project. This server can be hosted in an IIS server. Then, you can use it in the main project.

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

7. Create a new Xamarin Android App project.

Android App project creation

8. In the project configuration windows, name your project and select Create.

Name your project in project configuration window

9. Add a web reference with the above local service in this project.   Add the hosted service as Web Reference in Xamarin PDF

 

Note:

Kindly publish the WCF service/web API to the server with public IP or cloud service. So that mobile devices can access the service for the conversion.

 

Paste the hosted service link and click Add reference in Xamarin PDF

10. Invoke the GetData method from the service. Refer to the following code sample.

//Create new service
hostName.Service1 service = new hostName.Service1();
 
//Get the HTML as PDF stream
byte[] stream = service.GetData("https://www.syncfusion.com");
MemoryStream fileStream = new MemoryStream(stream);
fileStream.Position = 0;
 
//Save the stream to PDF file
Save("Sample.pdf", "application/pdf", fileStream);

 

11. By converting HTML to PDF, you will get the PDF document as follows.

Output document

The samples of Web service and Xamarin are attached to this article for your reference. Find the samples from the following zip files.

Web service: WebRoleWebKit.zip

Xamarin sample: HTMLtoPDFConversion.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.

Note:

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 how to generate and register the Syncfusion license key in your application to use the components without a trail message.

See also

HTML to PDF in Windows Forms

HTML to PDF in ASP.NET Core Windows

HTML to PDF in ASP.NET Core Linux

HTML to PDF in ASP.NET Core Mac

HTML to PDF in UWP

HTML to PDF in Azure

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied