0

我正在使用 Xamarin.Forms,我正在尝试使用 EvoPdfConverter 将 html 字符串转换为 pdf 文件,但问题是当我尝试这样做时,在htmlToPdfConverter.ConvertHtmlToFile(htmlData, "", myDir.ToString());下面的代码片段中,应用程序只是冻结并执行什么都没有,似乎它想连接到给定的 IP,但它不能,但是我没有收到任何错误或异常!连抓都抓不到!!有人知道我应该怎么做才能解决这个问题吗?这是我的代码:

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;                
            }
}
4

1 回答 1

0

您能否尝试将基本 URL 设置为 ConvertHtmlToFile 调用作为第二个参数?您传递了一个空字符串。这有助于将 HTML 中的相对 URL 解析为完整 URL。转换器在尝试从无效资源 URL 检索内容时可能会出现延迟。

于 2019-11-16T12:49:28.983 回答