)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert HTML to PDF in Xamarin platform

Platform: Xamarin.Forms |
Control: PDF |
Published Date: January 9, 2017 |
Last Revised Date: July 2, 2020

The Syncfusion HTML to PDF converter for .NET is used to convert webpages, SVG, MHTML and HTML to PDF. HTML to PDF conversion is supported in Xamarin platform by using web service. Using this library, you can convert HTML to PDF in Xamarin platform.

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

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

The following steps are used to create a WCF service for conversion:

  1. Open Visual Studio and create a new WCF service project. Create new WCF application
  2. Install the Syncfusion.HtmlToPdfConverter.QtWebKit.WinForms NuGet package as a reference to your web service application from the NuGet.org. Install required nuget packages
  3. Copy the QtBinaries folder from the installed HtmltoPdfConverter package and paste it in the folder that contains the WebRoleWebKit.csproj file.  Select the QtBinaries path from nuGet package location. Copy the QtBinaries folder to project location.
  1. Include the HTML to PDF conversion part in the service.

4.1)   Include QtBinaries folder to the project. Include QtBinaries folder into project.

 

4.2)   Then, set Copy to output directory to copy if newer to all the QtBinaries (All files including inner folders and files). Set Copy if newer to all the QtBinaries

4.3)   Include new OperationContract in the IService1 interface.

[OperationContract]
MemoryStream GetData(string value);

OperationContract

4.4)    Include the following namespaces and code snippet in Service1.svc for converting HTML to PDF in web 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# Code]
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;
using System.Web.Hosting;

 

public MemoryStream GetData(string value)
{
return CreateDocument(value);
}
public MemoryStream CreateDocument(string pathURL)
{
//Create new HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
 
//Initialize the WebKit settings
WebKitConverterSettings webKitSettings = new WebKitConverterSettings();
 
//Set the WebKit path
webKitSettings.WebKitPath = Path.Combine( HostingEnvironment.ApplicationPhysicalPath, "QtBinaries");
 
//Set the WebKit conversion settings
htmlConverter.ConverterSettings = webKitSettings;
 
//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;
}
  1. Run the service in the local machine and test the conversion using simple console sample in local machine.
  2. After successful conversion, deploy the web service and refer the local service to the main project. This server can be hosted in 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.

  1. Create a new Xamarin Android project. Create a new Xamarin Android project.
  2. Add a web reference with the above local service in this project.   Add the hosted service as Web Reference

Paste the hosted service link and click Add reference.

  1. Invoke the GetData method from the service. Refer to the following code snippet.
    //Create new service
    localhost.Service1 service = new localhost.Service1();
     
    //Get the HTML as PDF stream
    localhost.MemoryStream stream = service.GetData("http://www.google.com");
     
    MemoryStream fileStream = new MemoryStream(stream._buffer);
     
    fileStream.Position = 0;
     
    //Save the stream to PDF file
    Save("Sample.pdf", "application/pdf", fileStream);
    

 

  1. By converting HTML to PDF, you will get the PDF document as follows. Screenshot of Output PDF document.

The samples of Web service and Xamarin are attached in 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.

See also:

How to convert HTML to PDF in UWP.

An online sample link to convert HTML to PDF.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Ricky Ombina
Jan 09, 2018

Hello,

Do we need a service to make this work?

Thanks,

Ricky

Reply
Prakash Viswanathan [Syncfusion]
Jan 10, 2018

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

Reply
Reihaneh Khaksaran
Dec 08, 2018

Hello,

Is it possible to convert an html string to PDF using syncfusion?

Thanks,

Reihaneh

Reply
Prakash Viswanathan [Syncfusion]
Dec 10, 2018

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

Reply
Michael Nasers
Aug 28, 2019

This is very disappointing.

Gowthamraj Kumar [Syncfusion]
Aug 29, 2019

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

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon