Skip to content

Commit

Permalink
Migrated renderer to Scriban
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanstapel committed Feb 17, 2025
1 parent d7d722c commit 70f07f0
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 226 deletions.
2 changes: 1 addition & 1 deletion ZUGFeRD.Render.Demo/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class Application
internal async Task RunAsync()
{
InvoiceDescriptor desc = InvoiceDescriptor.Load("../../../../demodata/zugferd22/zugferd_2p2_EXTENDED_Fremdwaehrung-factur-x.xml");
string html = InvoiceDescriptorHtmlRenderer.Render(desc);
string html = await InvoiceDescriptorHtmlRenderer.RenderAsync(desc);
System.IO.File.WriteAllText("output.html", html);
} // !RunAsync()
}
Expand Down
2 changes: 1 addition & 1 deletion ZUGFeRD.Render.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace s2industries.ZUGFeRD.Render.Demo
{
internal class Program
{
static async Task Main(string[] args)
internal static async Task Main(string[] args)
{
Application app = new Application();
await app.RunAsync();
Expand Down
35 changes: 0 additions & 35 deletions ZUGFeRD.Render/HtmlHelper.cs

This file was deleted.

63 changes: 24 additions & 39 deletions ZUGFeRD.Render/InvoiceDescriptorHtmlRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand All @@ -16,62 +16,47 @@
* specific language governing permissions and limitations
* under the License.
*/
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.Language;
using RazorLight;
using s2industries.ZUGFeRD;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Scriban;

namespace s2industries.ZUGFeRD.Render
{
public class InvoiceDescriptorHtmlRenderer
{
public static string Render(InvoiceDescriptor desc)
public static async Task<string> RenderAsync(InvoiceDescriptor invoice)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = typeof(InvoiceDescriptorHtmlRenderer).Namespace + ".test.cshtml";

string templateText = "";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
string templateContent = _LoadEmbeddedResource("Simple.scriban");
var template = Template.Parse(templateContent);
var model = new
{
templateText = reader.ReadToEnd();
}

var engine = new RazorLightEngineBuilder()
.UseOptions(new RazorLightOptions()
{
EnableDebugMode = true
})
.SetOperatingAssembly(typeof(InvoiceDescriptorHtmlRenderer).GetTypeInfo().Assembly)
.Build();

var viewModel = new { Model = desc, Html = new HtmlHelper() };
Invoice = invoice
};

Task<string> resultTask = engine.CompileRenderStringAsync("test", templateText, viewModel);
resultTask.Wait();
string result = await template.RenderAsync(model, memberRenamer: member => member.Name);
return result;
} // !RenderAsync()


return resultTask.Result;
} // !Render()
private static string _LoadEmbeddedResource(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
var resourcePath = assembly.GetManifestResourceNames()
.FirstOrDefault(r => r.EndsWith(resourceName, StringComparison.OrdinalIgnoreCase));

if (resourcePath == null)
throw new FileNotFoundException($"Die eingebettete Ressource '{resourceName}' wurde nicht gefunden.");

public static void Render(InvoiceDescriptor desc, string filename)
{
string output = Render(desc);
StreamWriter writer = File.CreateText(filename);
writer.WriteLine(output);
writer.Close();
} // !Render()
using (var stream = assembly.GetManifestResourceStream(resourcePath))
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
} // !_LoadEmbeddedResource()
}
}
41 changes: 0 additions & 41 deletions ZUGFeRD.Render/MyViewProvider.cs

This file was deleted.

148 changes: 148 additions & 0 deletions ZUGFeRD.Render/Simple.scriban
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Invoice {{ Invoice.InvoiceNo }}</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-12">
<h1>Invoice {{ InvoiceNo }}</h1>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="card">
<div class="card-body">
<h5 class="card-title">Buyer</h5>
{{ Invoice.Buyer?.Name }}<br />
{{ Invoice.Buyer?.Street }}<br />
{{ Invoice.Buyer?.Postcode }} {{ Invoice.Buyer?.City }}<br />
</div>
</div>
</div>
<div class="col-6">
<div class="card">
<div class="card-body">
<h5 class="card-title">Seller</h5>
{{ Invoice.Seller?.Name }}<br />
{{ Invoice.Seller?.Street }}<br />
{{ Invoice.Seller?.Postcode }} {{ Invoice.Seller?.City }}<br />
</div>
</div>
</div>
</div>

<div class="row mt-2">
<div class="col-6">
<div class="card">
<div class="card-body">
<h5 class="card-title">Ship To</h5>
{{ Invoice.ShipTo?.Name }}<br />
{{ Invoice.ShipTo?.Street }}<br />
{{ Invoice.ShipTo?.Postcode }} {{ Invoice.ShipTo?.City }}<br />
</div>
</div>
</div>
<div class="col-6">
<div class="card">
<div class="card-body">
<h5 class="card-title">Ship From</h5>
{{ Invoice.ShipFrom?.Name }}<br />
{{ Invoice.ShipFrom?.Street }}<br />
{{ Invoice.ShipFrom?.Postcode }} {{ Invoice.ShipFrom?.City }}<br />
</div>
</div>
</div>
</div>

<div class="row mt-4">
<div class="col-12">
<table class="table">
<thead>
<tr>
<th>Product</th>
<th>No</th>
<th>Quantity</th>
<th>Gross Unit Price</th>
<th>Net Unit Price</th>
<th>Line Total</th>
</tr>
</thead>
<tbody>
{{ for item in Invoice.TradeLineItems }}
<tr>
<td>
{{ item.Name }}
{{ if item.Description }}
<br />
<small>{{ item.Description }}</small>
{{ end }}
</td>
<td>
{{ item.SellerAssignedID }}
<br />
{{ if item.BuyerAssignedID }}
<small>Buy assigned id: {{ item.BuyerAssignedID }}</small>
{{ end }}
<br />
{{ if item.GlobalID }}
{{ if item.GlobalID.SchemeID != "Unknown" }}
<small>{{ item.GlobalID.SchemeID }}: {{ item.GlobalID.Id }}</small>
{{ end }}
{{ end }}
</td>
<td>
{{ item.BilledQuantity }}
{{ if item.UnitQuantity }}
<br />
<small>Unit quantity: {{ item.UnitQuantity }}</small>
{{ end }}
</td>
<td>
{{ if item.GrossUnitPrice }}
{{ item.GrossUnitPrice | math.format("N2") }}
{{ end }}
</td>
<td>
{{ if item.NetUnitPrice }}
{{ item.NetUnitPrice | math.format("N2") }}
{{ end }}
</td>
<td>{{ item.LineTotalAmount | math.format("N2") }}</td>
</tr>
{{ end }}
</tbody>
</table>
</div>
</div>

<div class="row">
<div class="offset-8 col-4">
<dl>
<dt>Line Total</dt>
<dd>{{ Invoice.LineTotalAmount | math.format("N2") }}</dd>
<dt>Tax Total</dt>
<dd>{{ Invoice.TaxTotalAmount | math.format("N2") }}</dd>
<dt>Grand Total</dt>
<dd>{{ Invoice.GrandTotalAmount | math.format("N2") }}</dd>

{{ if Invoice.TotalPrepaidAmount }}
<dt>Prepaid Amount</dt>
<dd>{{ Invoice.TotalPrepaidAmount | math.format("N2") }}</dd>
{{ end }}

{{ if Invoice.DuePayableAmount }}
<dt>Due Payable</dt>
<dd>{{ Invoice.DuePayableAmount | math.format("N2") }}</dd>
{{ end }}
</dl>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions ZUGFeRD.Render/ZUGFeRD.Render.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="test.cshtml" />
<EmbeddedResource Include="Simple.scriban" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="RazorLight" Version="2.3.1" />
<PackageReference Include="Scriban" Version="5.12.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 70f07f0

Please sign in to comment.