How to convert HTML to PDF in Xamarin platform?
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:
- Create a WCF service for the conversion part and host it as a local service.
- 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.
2. In the project configuration windows, name your project and select Create.
3. Install Syncfusion.HtmlToPdfConverter.WinForms NuGet package as a reference to your web service application from the NuGet.org.
4. Include the new OperationContract in the IService1 interface.
[OperationContract]
byte[] GetData(string value);
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.
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.
8. In the project configuration windows, name your project and select Create.
9. Add a web reference with the above local service in this project.
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.
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.
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.
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 ASP.NET Core Windows
HTML to PDF in ASP.NET Core Linux
HTML to PDF in ASP.NET Core Mac
Hi Ricky,
Since HTML to PDF conversion does not have native support in
Xamarin, we need to create a web service to make use of this work around.
Regards,
Prakash V
Hello,
Is it possible to convert an html string to PDF using syncfusion?
Thanks,
Reihaneh
Hi Reihaneh,
Yes, it is possible to convert HTML string to PDF using HTML converter. As our HTML converter does not have native support in Xamarin, we need to create a web service for converting a HTML string to PDF. Kindly refer the following link for converting HTML string to PDF.
KB: https://www.syncfusion.com/kb/9890/
Regards,
Prakash V
Hi Michael,
Currently we do not have native support for converting HTML to PDF in Xamarin. As a workaround, we can achieve convert HTML to PDF by using web service. We will revamp this KB article with complete steps and details to convert HTML to PDF in Xamarin platform.
Please let us know if you need any further assistance on this.
Regards,
Gowthamraj K
Hello,
Do we need a service to make this work?
Thanks,
Ricky