public void ToPng(TaskCompletionSource <ToFileResult> taskCompletionSource, string html, string fileName, int width) { //var size = new Size(8.5, 11); //var externalPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath; //using (var dir = new Java.IO.File(externalPath)) //using (var file = new Java.IO.File(dir + "/" + fileName + ".png")) //{ // if (!dir.Exists()) // dir.Mkdir(); // if (file.Exists()) // file.Delete(); var webView = new Android.Webkit.WebView(Android.App.Application.Context); webView.Settings.JavaScriptEnabled = true; #pragma warning disable CS0618 // Type or member is obsolete webView.DrawingCacheEnabled = true; #pragma warning restore CS0618 // Type or member is obsolete webView.SetLayerType(LayerType.Software, null); //webView.Layout(0, 0, (int)((size.Width - 0.5) * 72), (int)((size.Height - 0.5) * 72)); webView.Layout(0, 0, width, width); webView.SetWebViewClient(new WebViewCallBack(taskCompletionSource, fileName, new PageSize { Width = width }, null, OnPageFinished)); webView.LoadData(html, "text/html; charset=utf-8", "UTF-8"); //} }
public void ToPdf(TaskCompletionSource <ToFileResult> taskCompletionSource, string html, string fileName, PageSize pageSize, PageMargin margin) { var externalPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath; using (var dir = new Java.IO.File(externalPath)) using (var file = new Java.IO.File(dir + "/" + fileName + ".pdf")) { if (!dir.Exists()) { dir.Mkdir(); } if (file.Exists()) { file.Delete(); } var webView = new Android.Webkit.WebView(Android.App.Application.Context); webView.Settings.JavaScriptEnabled = true; #pragma warning disable CS0618 // Type or member is obsolete webView.DrawingCacheEnabled = true; #pragma warning restore CS0618 // Type or member is obsolete webView.SetLayerType(LayerType.Software, null); //webView.Layout(0, 0, (int)((size.Width - 0.5) * 72), (int)((size.Height - 0.5) * 72)); webView.Layout(0, 0, (int)System.Math.Ceiling(pageSize.Width), (int)System.Math.Ceiling(pageSize.Height)); webView.LoadData(html, "text/html; charset=utf-8", "UTF-8"); webView.SetWebViewClient(new WebViewCallBack(taskCompletionSource, fileName, pageSize, margin, OnPageFinished)); } }
//配置webview private void setSettings(Android.Webkit.WebView webView) { androidWebView = webView; webView.SetLayerType(Android.Views.LayerType.Hardware, new Android.Graphics.Paint()); WebSettings set = webView.Settings; set.JavaScriptEnabled = true; set.JavaScriptCanOpenWindowsAutomatically = true; set.SetSupportZoom(true); set.UseWideViewPort = true; set.SetPluginState(PluginState.On); set.PluginsEnabled = true; set.AllowFileAccess = true; set.CacheMode = CacheModes.NoCache; set.SetLayoutAlgorithm(LayoutAlgorithm.SingleColumn); set.LoadWithOverviewMode = true; set.BuiltInZoomControls = true; set.SetAppCacheEnabled(false); set.SetGeolocationEnabled(true); set.DomStorageEnabled = true; set.MixedContentMode = MixedContentHandling.AlwaysAllow; set.MediaPlaybackRequiresUserGesture = false; }
public void ToPng(TaskCompletionSource <ToFileResult> taskCompletionSource, Xamarin.Forms.WebView xfWebView, string fileName) { if (Platform.CreateRendererWithContext(xfWebView, Settings.Context) is IVisualElementRenderer renderer) { Android.Webkit.WebView droidWebView = renderer.View as Android.Webkit.WebView; if (droidWebView == null && renderer.View is WebViewRenderer xfWebViewRenderer) { droidWebView = xfWebViewRenderer.Control; } if (droidWebView != null) { //var size = new Size(8.5, 11); var externalPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath; using (var dir = new Java.IO.File(externalPath)) using (var file = new Java.IO.File(dir + "/" + fileName + ".png")) { if (!dir.Exists()) { dir.Mkdir(); } if (file.Exists()) { file.Delete(); } droidWebView.SetLayerType(LayerType.Software, null); droidWebView.Settings.JavaScriptEnabled = true; #pragma warning disable CS0618 // Type or member is obsolete droidWebView.DrawingCacheEnabled = true; droidWebView.BuildDrawingCache(); #pragma warning restore CS0618 // Type or member is obsolete droidWebView.SetWebViewClient(new WebViewCallBack(taskCompletionSource, fileName, OnPageFinished)); } } } }