Xamarin free HTML or DOC to PDF Conversion

前端 未结 4 1844
既然无缘
既然无缘 2021-02-06 15:49

I\'m currently searching for a library or a way to convert HTML OR DOCX files into PDF on the phone/tab, primarily I\'am searching for a way on Android or iOS idk if its a PCL o

4条回答
  •  生来不讨喜
    2021-02-06 16:08

    Frustrated with the existing solutions, I've built some extension methods (OpenSource, MIT Licensed) that convert HTML or the content of a Xamarin.Forms.WebView to a PDF file. Sample usage for WebView to PDF:

            async void ShareButton_Clicked(object sender, EventArgs e)
            {
                if (Forms9Patch.ToPdfService.IsAvailable)
                {
                    if (await webView.ToPdfAsync("output.pdf") is ToFileResult pdfResult)
                    {
                        if (pdfResult.IsError)
                            using (Toast.Create("PDF Failure", pdfResult.Result)) { }
                        else
                        {
                            var collection = new Forms9Patch.MimeItemCollection();
                            collection.AddBytesFromFile("application/pdf", pdfResult.Result);
                            Forms9Patch.Sharing.Share(collection, shareButton);
                        }
                    }
                }
                else
                    using (Toast.Create(null, "PDF Export is not available on this device")) { }
            }
        }
    

    For a more complete explanation of how to use it, here's a short article: https://medium.com/@ben_12456/share-xamarin-forms-webview-as-a-pdf-a877542e824a?

提交回复
热议问题