/// <summary> /// Creates a PNG from the contents of a Xamarin.Forms.WebView /// </summary> /// <param name="webView">Xamarin.Forms.WebView</param> /// <param name="fileName">Name (not path), excluding suffix, of PDF file</param> /// <param name="pageSize">PDF page size, in points. (default based upon user's region)</param> /// <param name="margin">PDF page's margin, in points. (default is zero)</param> /// <returns>Forms9Patch.ToFileResult</returns> public static async Task <ToFileResult> ToPdfAsync(this Xamarin.Forms.WebView webView, string fileName, PageSize pageSize = default, PageMargin margin = default) { _platformToPdfService = _platformToPdfService ?? DependencyService.Get <IToPdfService>(); if (_platformToPdfService == null) { throw new NotSupportedException("Cannot get HtmlService: must not be supported on this platform."); } ToFileResult result = null; using (var indicator = ActivityIndicatorPopup.Create()) { if (pageSize is null || pageSize.Width <= 0 || pageSize.Height <= 0) { pageSize = PageSize.Default; } margin = margin ?? new PageMargin(); if (pageSize.Width - margin.HorizontalThickness < 1 || pageSize.Height - margin.VerticalThickness < 1) { return(new ToFileResult(true, "Page printable area (page size - margins) has zero width or height.")); } result = await _platformToPdfService.ToPdfAsync(webView, fileName, pageSize, margin); await indicator.CancelAsync(); } await Task.Delay(50); return(result); }
/// <summary> /// Creates a PNG from the contents of a Xamarin.Forms.WebView /// </summary> /// <param name="webView"></param> /// <param name="fileName"></param> /// <returns></returns> public static async Task <ToFileResult> ToPdfAsync(this Xamarin.Forms.WebView webView, string fileName) { _platformToPdfService = _platformToPdfService ?? DependencyService.Get <IToPdfService>(); if (_platformToPdfService == null) { throw new NotSupportedException("Cannot get HtmlService: must not be supported on this platform."); } ToFileResult result = null; using (var indicator = ActivityIndicatorPopup.Create()) { result = await _platformToPdfService.ToPdfAsync(webView, fileName); } await Task.Delay(50); return(result); }
/// <summary> /// Creates a PNG from the contents of a Xamarin.Forms.WebView /// </summary> /// <param name="webView">Xamarin.Forms.WebView</param> /// <param name="fileName">Name (not path), excluding suffix, of PNG file</param> /// <param name="width">Width of resulting PNG (in pixels).</param> /// <returns></returns> public static async Task <ToFileResult> ToPngAsync(this Xamarin.Forms.WebView webView, string fileName, int width = -1) { _platformToPngService = _platformToPngService ?? DependencyService.Get <IToPngService>(); if (_platformToPngService == null) { throw new NotSupportedException("Cannot get HtmlService: must not be supported on this platform."); } ToFileResult result = null; using (var indicator = ActivityIndicatorPopup.Create()) { if (width <= 0) { width = (int)Math.Ceiling((PageSize.Default.Width - (12 * 25.4)) * 4); } result = await _platformToPngService.ToPngAsync(webView, fileName, width); } await Task.Delay(50); return(result); }