简体   繁体   English

Xamarin.Forms 无法使用 EvoHtmlToPdfclient 将 html 字符串转换为 pdf 文件

[英]Xamarin.Forms failing to use EvoHtmlToPdfclient in order to convert html string to a pdf file

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());我正在使用 Xamarin.Forms,我正在尝试使用 EvoPdfConverter 将 html 字符串转换为 pdf 文件,但问题是当我尝试这样做时,在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!在下面的代码片段中,应用程序只是冻结,什么也不做,似乎它想连接到给定的 IP,但它不能,但是我没有收到任何错误或异常! 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;                
            }
}

Could you try to set a base URL to ConvertHtmlToFile call as the second parameter?您可以尝试将 ConvertHtmlToFile 调用的基本 URL 设置为第二个参数吗? You passed an empty string.您传递了一个空字符串。 That helps to resolve the relative URLs found in HTML to full URLs.这有助于将 HTML 中的相对 URL 解析为完整 URL。 The converter might have delays when trying to retrieve content from invalid resources URLs.尝试从无效资源 URL 检索内容时,转换器可能会出现延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM