A set of HtmlToPdf.Maui
NuGet Packages: HtmlToPdf.Maui Usage:
In MauiProgram.CreateMauiApp register the control: .UseHtmlToPdf()
public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp() .UseMauiCommunityToolkit() .UseHtmlToPdf() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); }); #if DEBUG builder.Logging.AddDebug(); #endif return builder.Build(); } }
Example:
create a MainPage.xaml
In XAML it is necessary to insert a WebView with Visibility false.
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Button
x:Name="ButtonGeneratePDF"
Text="Generate"
Clicked="GenerateClicked"
HorizontalOptions="Fill" />
<WebView x:Name="WebViewPDF" IsVisible="False"/>
</VerticalStackLayout>
In the code links put:
public partial class MainPage : ContentPage { private string html = "Hello"; public MainPage() { InitializeComponent(); } private async void GenerateClicked(object sender, EventArgs e) { WebViewPDF.Source = new HtmlWebViewSource() { Html = html }; if (await ToPdfService.ToPdfAsync(WebViewPDF, "sampleNamePdf") is ToFileResult result) { if (result.IsError) { await Toast.Make(result.Result).Show(); return; } await Launcher.OpenAsync(new OpenFileRequest { File = new ReadOnlyFile(result.Result) }); } } } }
Currently available for Android and IOS.