Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup Condition="'$(VisualStudioVersion)' == '17.0'">
<Version>9.11.5-beta06</Version>
<Version>9.11.5-beta07</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(VisualStudioVersion)' == '18.0'">
<Version>10.0.0-rc.2.1.5</Version>
<Version>10.0.0-rc.2.1.6</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
17 changes: 17 additions & 0 deletions src/BootstrapBlazor/Options/PdfOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

namespace BootstrapBlazor.Components;

/// <summary>
/// PdfOptions 实例用于设置导出 Pdf 相关选项
/// </summary>
public class PdfOptions
{
/// <summary>
/// 获得/设置 是否横向打印 默认 false
/// </summary>
public bool Landscape { get; set; }
}
26 changes: 4 additions & 22 deletions src/BootstrapBlazor/Services/DefaultHtml2PdfService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,11 @@ class DefaultHtml2PdfService : IHtml2Pdf
{
private const string ErrorMessage = "请增加依赖包 BootstrapBlazor.Html2Pdf 通过 AddBootstrapBlazorHtml2PdfService 进行服务注入; Please add BootstrapBlazor.Html2Pdf package and use AddBootstrapBlazorHtml2PdfService inject service";

/// <summary>
/// <inheritdoc/>
/// </summary>
public Task<byte[]> PdfDataAsync(string url) => throw new NotImplementedException(ErrorMessage);
public Task<byte[]> PdfDataAsync(string url, PdfOptions? options = null) => throw new NotImplementedException(ErrorMessage);

/// <summary>
/// <inheritdoc/>
/// </summary>
public Task<Stream> PdfStreamAsync(string url) => throw new NotImplementedException(ErrorMessage);
public Task<Stream> PdfStreamAsync(string url, PdfOptions? options = null) => throw new NotImplementedException(ErrorMessage);

/// <summary>
/// Export method
/// </summary>
/// <param name="html">html raw string</param>
/// <param name="links"></param>
/// <param name="scripts"></param>
public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) => throw new NotImplementedException(ErrorMessage);
public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null) => throw new NotImplementedException(ErrorMessage);

/// <summary>
/// Export method
/// </summary>
/// <param name="html">html raw string</param>
/// <param name="links"></param>
/// <param name="scripts"></param>
public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) => throw new NotImplementedException(ErrorMessage);
public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null) => throw new NotImplementedException(ErrorMessage);
}
12 changes: 8 additions & 4 deletions src/BootstrapBlazor/Services/IHtml2Pdf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,31 @@ public interface IHtml2Pdf
/// Export method
/// </summary>
/// <param name="url">url</param>
Task<byte[]> PdfDataAsync(string url);
/// <param name="options"></param>
Task<byte[]> PdfDataAsync(string url, PdfOptions? options = null);

/// <summary>
/// Export method
/// </summary>
/// <param name="url">url</param>
Task<Stream> PdfStreamAsync(string url);
/// <param name="options"></param>
Task<Stream> PdfStreamAsync(string url, PdfOptions? options = null);

/// <summary>
/// Export method
/// </summary>
/// <param name="html">html raw string</param>
/// <param name="links"></param>
/// <param name="scripts"></param>
Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null);
/// <param name="options"></param>
Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null);

/// <summary>
/// Export method
/// </summary>
/// <param name="html">html raw string</param>
/// <param name="links"></param>
/// <param name="scripts"></param>
Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null);
/// <param name="options"></param>
Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null);
}
8 changes: 4 additions & 4 deletions test/UnitTest/Components/ExportPdfButtonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,21 @@ public void ExportPdfButtonSettings_Ok()

class MockHtml2PdfService : IHtml2Pdf
{
public Task<byte[]> PdfDataAsync(string url)
public Task<byte[]> PdfDataAsync(string url, PdfOptions? options = null)
{
throw new NotImplementedException();
}

public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null)
public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null)
{
throw new NotImplementedException();
}

public Task<Stream> PdfStreamAsync(string url)
public Task<Stream> PdfStreamAsync(string url, PdfOptions? options = null)
{
throw new NotImplementedException();
}

public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) => Task.FromResult<Stream>(new MemoryStream(Encoding.UTF8.GetBytes("Hello World")));
public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null, PdfOptions? options = null) => Task.FromResult<Stream>(new MemoryStream(Encoding.UTF8.GetBytes("Hello World")));
}
}
10 changes: 10 additions & 0 deletions test/UnitTest/Services/Html2PdfServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ public async Task ExportPdf_Error()
await Assert.ThrowsAsync<NotImplementedException>(() => pdfService.PdfDataFromHtmlAsync("<h2>Test</h2>"));
await Assert.ThrowsAsync<NotImplementedException>(() => pdfService.PdfStreamFromHtmlAsync("<h2>Test</h2>"));
}

[Fact]
public void PdfOptions_Ok()
{
var options = new PdfOptions
{
Landscape = true
};
Assert.True(options.Landscape);
}
}
Loading