0

I'm using Xamarin.Forms and I am trying to convert an html string to a pdf file using EvoPdfConverter, but the problem is that when I try to do so, on the line htmlToPdfConverter.ConvertHtmlToFile(htmlData, "", myDir.ToString()); in the code snippet below, the app just freezes and does nothing, seems like it wants to connect to the given IP, but it can't, however I don't get any errors or exceptions! not even catch!! does anybody know what I should do to resolve this issue? and here is my code for this:

public  void ConvertHtmlToPfd(string htmlData)
{
 ServerSocket s = new ServerSocket(0);
 HtmlToPdfConverter htmlToPdfConverter = new 
 HtmlToPdfConverter(GetLocalIPAddress(),(uint)s.LocalPort);
 htmlToPdfConverter.TriggeringMode = TriggeringMode.Auto;
 htmlToPdfConverter.PdfDocumentOptions.CompressCrossReference = true;
 htmlToPdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Best;

  if (ContextCompat.CheckSelfPermission(Android.App.Application.Context, Manifest.Permission.WriteExternalStorage) != Permission.Granted)
   {
      ActivityCompat.RequestPermissions((Android.App.Activity)Android.App.Application.Context, new String[] { Manifest.Permission.WriteExternalStorage }, 1);
   }
  if (ContextCompat.CheckSelfPermission(Android.App.Application.Context, Manifest.Permission.ReadExternalStorage) != Permission.Granted)
   {
      ActivityCompat.RequestPermissions((Android.App.Activity)Android.App.Application.Context, new String[] { Manifest.Permission.ReadExternalStorage }, 1);
   }

    try
        {

          // create the HTML to PDF converter object
          if (Android.OS.Environment.IsExternalStorageEmulated)
            {
              root = Android.OS.Environment.ExternalStorageDirectory.ToString();
            }
                htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";
                htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
                htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = PdfPageOrientation.Portrait;
                Java.IO.File myDir = new Java.IO.File(root + "/Reports");
                try
                {
                    myDir.Mkdir();
                }
                catch (Exception e)
                {
                    string message = e.Message;
                }
                Java.IO.File file = new Java.IO.File(myDir, filename);

                if (file.Exists()) file.Delete();

               htmlToPdfConverter.ConvertHtmlToFile(htmlData, "", myDir.ToString());

            }
            catch (Exception ex)
            {
                string message = ex.Message;                
            }
}
  • Do you refer to this doc(http://www.evopdf.com/xamarin-html-to-pdf-converter.aspx)? – Junior Jiang Dec 10 '18 at 05:50
  • yes, but there's no guidance, I mean yes there's a sample, but the sample is converting a URL using `ConvertIrl()` method, but I wanna convert an HTML string uisng `ConvertHtmlToFile()` method, which is a void method and doesn't return anything, besides I have run the sample on my device, that doesn't work either! – Reihaneh Khaksaran Dec 10 '18 at 06:01
  • Ok , If you don't mind, you can try pdftron(https://www.pdftron.com/documentation/xamarin/guides/basics/html-to-pdf/#from-html-string). – Junior Jiang Dec 10 '18 at 06:20
  • Thank you, I'll give it a try, see if it works – Reihaneh Khaksaran Dec 10 '18 at 08:59

1 Answers1

0

Could you try to set a base URL to ConvertHtmlToFile call as the second parameter? You passed an empty string. That helps to resolve the relative URLs found in HTML to full URLs. The converter might have delays when trying to retrieve content from invalid resources URLs.

EvoPdf
  • 523
  • 3
  • 9