예제 #1
0
        public async Task <string> ConvertHtmlToPDF(string html, string fileName)
        {
            var dir  = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/Download/");
            var file = new Java.IO.File(dir + "/" + fileName + ".pdf");

            if (!dir.Exists())
            {
                dir.Mkdirs();
            }

            int x = 0;

            while (file.Exists())
            {
                x++;
                file = new Java.IO.File(dir + "/" + fileName + "(" + x + ")" + ".pdf");
            }

            var webpage = new Android.Webkit.WebView(MainActivity.Instance);
            //var windowManager = MainActivity.Instance.GetSystemService(Android.Content.Context.WindowService);
            //DisplayMetrics outMetrics = new DisplayMetrics();
            //windowManager.DefaultDisplay.GetMetrics(outMetrics);
            //int widthPixels = outMetrics.WidthPixels;
            //int heightPixels = outMetrics.HeightPixels;

            //int width = widthPixels;
            //int height = heightPixels;
            int width  = 2102;
            int height = 2937;

            webpage.Layout(0, 0, width, height);

            var client      = new WebViewCallBack(file.ToString());
            var tokenSource = new CancellationTokenSource();
            var task        = Task.Run(() =>
            {
                if (tokenSource.Token.IsCancellationRequested)
                {
                    return;
                }
                while (true)
                {
                    if (tokenSource.Token.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }, tokenSource.Token);

            client.OnPageLoadFinished += (s, e) =>
            {
                tokenSource.Cancel();
            };
            webpage.SetWebViewClient(client);
            webpage.LoadDataWithBaseURL("", html, "text/html", "UTF-8", null);

            await task;

            return(file.ToString());
        }
예제 #2
0
        public async Task <string> ConvertHtmlToPDF(string html, string filename)
        {
            UIWebView webView = new UIWebView(new CGRect(0, 0, 6.5 * 72, 9 * 72));

            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var file      = Path.Combine(documents, filename + ".pdf");

            var webDelegate = new WebViewCallBack(file);

            webView.Delegate               = webDelegate;
            webView.ScalesPageToFit        = true;
            webView.UserInteractionEnabled = false;
            webView.BackgroundColor        = UIColor.White;

            var tokenSource = new CancellationTokenSource();
            var task        = Task.Run(async() =>
            {
                if (tokenSource.Token.IsCancellationRequested)
                {
                    return;
                }
                while (true)
                {
                    await Task.Delay(1);
                    if (tokenSource.Token.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }, tokenSource.Token);

            webDelegate.OnPageLoadFinished += (s, e) =>
            {
                tokenSource.Cancel();
            };

            webView.LoadHtmlString(html, null);

            await task;

            return(file);
        }