Convert HTML to PDF in Xamarin

This tutorial only applies to Xamarin.iOS. See Xamarin.Android equivalent here .

Internet connection is not required when converting local HTML files. However, it is required for converting HTTP/HTTPS links.

The platform-specific conversion API also supports HTML to PDF conversion, either with the +convertOfficeToPDF:paperSize:completion: method for .html files or the +convertHTMLStringToPDF:baseURL:paperSize:completion: for raw HTML text.

The following code shows how to convert an HTML string to PDF:

C#

1pdftronprivate.PTConvert.ConvertHTMLStringToPDF(htmlString, baseURL, CGSize.Empty, (pathToPDF) => {
2 if (pathToPDF == null) {
3 // Failed to convert HTML to PDF.
4 return;
5 }
6
7 var documentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
8
9 var urlToPdf = new NSUrl(pathToPDF);
10 var destinationURL = (new NSUrl(documentDirectory)).Append(urlToPdf.LastPathComponent, false);
11 var destinationFileURL = NSUrl.CreateFileUrl(destinationURL.Path, null);
12
13 NSError error = null;
14 var result = NSFileManager.DefaultManager.Copy(urlToPdf, destinationFileURL, out error);
15 if (!result) {
16 // Failed to copy PDF to persistent location.
17 }
18
19 // Do something with PDF output.
20});

where baseURL is the URL to use as the page's base URL for resolving relative links in the document.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales