-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,30 @@ | ||
using Altinn.App.Core.Models; | ||
using Altinn.Platform.Storage.Interface.Models; | ||
|
||
namespace Altinn.App.Core.Features | ||
namespace Altinn.App.Core.Features; | ||
|
||
/// <summary> | ||
/// Interface to customize PDF formatting. | ||
/// </summary> | ||
/// <remarks> | ||
/// This interface has been changed and both methods now has default implementation for backwards compatibility. | ||
/// All users will call the method with the Instance parameter, and a user only needs to implement one | ||
/// </remarks> | ||
public interface IPdfFormatter | ||
{ | ||
/// <summary> | ||
/// Interface to customize PDF formatting. | ||
/// Old method to format the PDF dynamically (without Instance) | ||
/// </summary> | ||
Task<LayoutSettings> FormatPdf(LayoutSettings layoutSettings, object data) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// Method to format the PDF dynamically (new version with the instance) | ||
/// </summary> | ||
public interface IPdfFormatter | ||
async Task<LayoutSettings> FormatPdf(LayoutSettings layoutSettings, object data, Instance instance, LayoutSet? layoutSet) | ||
{ | ||
/// <summary> | ||
/// Method to format the PDF dynamically | ||
/// </summary> | ||
Task<LayoutSettings> FormatPdf(LayoutSettings layoutSettings, object data); | ||
return await FormatPdf(layoutSettings, data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using Altinn.App.Core.Internal.Expressions; | ||
using Altinn.App.Core.Models; | ||
using Altinn.Platform.Storage.Interface.Models; | ||
|
||
namespace Altinn.App.Core.Features.Pdf; | ||
|
||
/// <summary> | ||
/// Custom formatter that reads the `hidden` properties of components and pages | ||
/// to determine if they should be hidden in PDF as well. | ||
/// </summary> | ||
/// <remarks> | ||
/// Add this to dependency injection in order to run dynamics for hiding pages and comonents. | ||
/// <code> | ||
/// services.AddTrancient<IPdfFormatter, DynamicsPdfFormatter>(); | ||
/// </code> | ||
/// </remarks> | ||
public class DynamicsPdfFormatter : IPdfFormatter | ||
{ | ||
private readonly LayoutEvaluatorStateInitializer _layoutStateInit; | ||
|
||
/// <summary> | ||
/// Constructor for <see cref="DynamicsPdfFormatter" /> | ||
/// </summary> | ||
public DynamicsPdfFormatter(LayoutEvaluatorStateInitializer layoutStateInit) | ||
{ | ||
_layoutStateInit = layoutStateInit; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<LayoutSettings> FormatPdf(LayoutSettings layoutSettings, object data, Instance instance, LayoutSet? layoutSet) | ||
{ | ||
layoutSettings.Pages ??= new(); | ||
layoutSettings.Pages.ExcludeFromPdf ??= new(); | ||
layoutSettings.Components ??= new(); | ||
layoutSettings.Components.ExcludeFromPdf ??= new(); | ||
|
||
var state = await _layoutStateInit.Init(instance, data, layoutSetId: layoutSet?.Id); | ||
foreach (var pageContext in state.GetComponentContexts()) | ||
{ | ||
var pageHidden = ExpressionEvaluator.EvaluateBooleanExpression(state, pageContext, "hidden", false); | ||
if (pageHidden) | ||
{ | ||
layoutSettings.Pages.ExcludeFromPdf.Add(pageContext.Component.Id); | ||
} | ||
else | ||
{ | ||
//TODO: figure out how pdf reacts to groups one level down. | ||
foreach (var componentContext in pageContext.ChildContexts) | ||
{ | ||
var componentHidden = ExpressionEvaluator.EvaluateBooleanExpression(state, componentContext, "hidden", false); | ||
if (componentHidden) | ||
{ | ||
layoutSettings.Components.ExcludeFromPdf.Add(componentContext.Component.Id); | ||
} | ||
} | ||
} | ||
} | ||
return layoutSettings; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters