Skip to content

Commit

Permalink
Templater v6.0.0
Browse files Browse the repository at this point in the history
API changes so CancellationToken can be passed into processing.
Java API change to support try-with-resource pattern (close() instead of flush() method).
Sync .NET and Java API (argument order changes in .NET - legacy API currently available with extension methods)

Show how to use CancellationToken in the TemplaterServer by using timeout argument.
Minimal Java version is now 8.
  • Loading branch information
zapov committed Aug 3, 2021
1 parent 6709e6c commit 9202b20
Show file tree
Hide file tree
Showing 207 changed files with 399 additions and 370 deletions.
2 changes: 1 addition & 1 deletion Advanced/CsvStreaming/CsvStreaming.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<Private>True</Private>
</Reference>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.5.2.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.0.0\lib\Net40\NGS.Templater.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Advanced/CsvStreaming/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNetZip" version="1.13.0" targetFramework="net40-Client" />
<package id="Templater" version="5.2.0" targetFramework="net40-Client" />
<package id="Templater" version="6.0.0" targetFramework="net40-Client" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/CsvStreaming/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>hr.ngs.templater.example</groupId>
<artifactId>csv-streaming-example</artifactId>
<packaging>jar</packaging>
<version>5.2.0</version>
<version>6.0.0</version>
<name>CSV streaming</name>
<url>https://github.com/ngs-doo/TemplaterExamples</url>

Expand Down
2 changes: 1 addition & 1 deletion Advanced/CsvStreaming/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static void Main(string[] args)
using (var zip = new ZipOutputStream("output.zip"))
{
zip.PutNextEntry("output.csv");
using (var doc = config.Build().Open(File.OpenRead("template/input.csv"), zip, "csv"))
using (var doc = config.Build().Open(File.OpenRead("template/input.csv"), "csv", zip))
{
//streaming processing assumes we have only a single collection, which means we first need to process all other tags
doc.Process(new { filter = new { date = "All", user = "All" } });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static void main(final String[] args) throws Exception {
}
//remove remaining rows
doc.templater().resize(doc.templater().tags(), 0);
doc.flush();
doc.close();
conn.close();
zos.closeEntry();
zos.close();
Expand Down
2 changes: 1 addition & 1 deletion Advanced/DepartmentReport/DepartmentReport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.5.2.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.0.0\lib\Net40\NGS.Templater.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion Advanced/DepartmentReport/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Templater" version="5.2.0" targetFramework="net40-Client" />
<package id="Templater" version="6.0.0" targetFramework="net40-Client" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/DepartmentReport/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>hr.ngs.templater.example</groupId>
<artifactId>department-report-example</artifactId>
<packaging>jar</packaging>
<version>5.2.0</version>
<version>6.0.0</version>
<name>DepartmentReportExample</name>
<url>https://github.com/ngs-doo/TemplaterExamples</url>

Expand Down
2 changes: 1 addition & 1 deletion Advanced/DepartmentReport/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static void Main(string[] args)
.NavigateSeparator(':')
.Include(SortExpression)
.Build()
.Open(fis, fos, "xlsx"))
.Open(fis, "xlsx", fos))
doc.Process(GetCompany());

Process.Start(new ProcessStartInfo("departments.xlsx") { UseShellExecute = true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static void main(final String[] args) throws Exception {
.build()
.open(templateStream, "xlsx", fos);
tpl.process(getCompany());
tpl.flush();
tpl.close();
fos.close();
Desktop.getDesktop().open(tmp);
}
Expand Down
2 changes: 1 addition & 1 deletion Advanced/DoubleProcessing/DoubleProcessing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.5.2.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.0.0\lib\Net40\NGS.Templater.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Advanced/DoubleProcessing/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Templater" version="5.2.0" targetFramework="net40-Client" />
<package id="Templater" version="6.0.0" targetFramework="net40-Client" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/DoubleProcessing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>hr.ngs.templater.example</groupId>
<artifactId>double-processing-example</artifactId>
<packaging>jar</packaging>
<version>5.2.0</version>
<version>6.0.0</version>
<name>DoubleProcessingExample</name>
<url>https://github.com/ngs-doo/TemplaterExamples</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static void main(final String[] args) throws Exception {
doc1.templater().resize(new String[] { t }, 0);//hide column from output
}
}
doc1.flush();
doc1.close();

//now let's prepare our complex object for standard processing
Map<String, Object> complex = buildComplexObject(totalPeople);
Expand All @@ -64,7 +64,7 @@ public static void main(final String[] args) throws Exception {
ITemplateDocument doc2 = factory.open(is, "xlsx", fos);
doc2.process(complex);
doc2.process(new HashMap<String, Object>() {{ put("starcraft", units); }});
doc2.flush();
doc2.close();
fos.close();

Desktop.getDesktop().open(tmp);
Expand Down
2 changes: 1 addition & 1 deletion Advanced/PowerQuery/PowerQuery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.5.2.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.0.0\lib\Net40\NGS.Templater.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Advanced/PowerQuery/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Templater" version="5.2.0" targetFramework="net40-Client" />
<package id="Templater" version="6.0.0" targetFramework="net40-Client" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/PowerQuery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>hr.ngs.templater.example</groupId>
<artifactId>power-query-example</artifactId>
<packaging>jar</packaging>
<version>5.2.0</version>
<version>6.0.0</version>
<name>PowerQueryExample</name>
<url>https://github.com/ngs-doo/TemplaterExamples</url>

Expand Down
8 changes: 4 additions & 4 deletions Advanced/PowerQuery/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class Program

private static object ToIsoFormat(object value, string tag, string[] metadata)
{
//only apply iso format on CSV part of the processing
if (!tag.StartsWith("csv.")) return value;
//only apply iso format on CSV part of the processing (StartsWith using Ordinal is significantly faster)
if (!tag.StartsWith("csv.", StringComparison.Ordinal)) return value;
if (value is DateTime)
{
var dt = (DateTime)value;
Expand Down Expand Up @@ -101,11 +101,11 @@ public static void Main(string[] args)
{
var data = new InputData();
data.csv = GenerateData(100000);
data.sheet = GenerateData(25000);
data.sheet = GenerateData(50000);

using (var fis = File.OpenRead("template/PowerQuery.xlsx"))
using (var fos = File.OpenWrite("PowerQuery.xlsx"))
using (var doc = Factory.Open(fis, fos, "xlsx"))
using (var doc = Factory.Open(fis, "xlsx", fos))
doc.Process(data);

Process.Start(new ProcessStartInfo("PowerQuery.xlsx") { UseShellExecute = true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,9 @@
public class PowerQueryExample {

private static IDocumentFactory factory = Configuration.builder()
.include(new LocalDateToDate())
//Excel will complain about corrupted file unless Templater is initialized with a valid license
.build("Customer email", "Customer license");

static class LocalDateToDate implements IDocumentFactoryBuilder.ILowLevelReplacer {

@Override
public Object replace(Object value, String tag, String[] metadata) {
//Templater does not understand java8 types so we can convert local date into legacy date
if (value instanceof LocalDate) {
return java.sql.Date.valueOf((LocalDate)value);
}
return value;
}
}

//class should be public Templater to reduce the overhead of reflection access
public static class CsvData {
public LocalDate date;
Expand Down Expand Up @@ -113,14 +100,13 @@ private static File fixStreamingZip(File file) throws IOException {
public static void main(final String[] args) throws Exception {
File tmp = File.createTempFile("power", ".xlsx");
InputData data = new InputData();
data.csv = generateData(100000);
data.sheet = generateData(25000);
data.csv = generateData(100_000);
data.sheet = generateData(50_000);

try(InputStream is = PowerQueryExample.class.getResourceAsStream("/PowerQuery.xlsx");
OutputStream os = Files.newOutputStream(tmp.toPath())) {
ITemplateDocument tpl = factory.open(is, "xlsx", os);
OutputStream os = Files.newOutputStream(tmp.toPath());
ITemplateDocument tpl = factory.open(is, "xlsx", os)) {
tpl.process(data);
tpl.flush();
}

File result = fixStreamingZip(tmp);
Expand Down
2 changes: 1 addition & 1 deletion Advanced/SalesOrderMVP (.NET)/SalesOrderMVP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<Private>True</Private>
</Reference>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.5.2.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.0.0\lib\Net40\NGS.Templater.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Advanced/SalesOrderMVP (.NET)/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Fluent.Ribbon" version="3.6.1.236" targetFramework="net40-Client" />
<package id="Templater" version="5.2.0" targetFramework="net40-Client" />
<package id="Templater" version="6.0.0" targetFramework="net40-Client" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/SheetReport/SheetReport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<Private>True</Private>
</Reference>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.5.2.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.0.0\lib\Net40\NGS.Templater.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Advanced/SheetReport/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SharpZipLib" version="0.86.0" targetFramework="net40-Client" />
<package id="Templater" version="5.2.0" targetFramework="net40-Client" />
<package id="Templater" version="6.0.0" targetFramework="net40-Client" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/SheetReport/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>hr.ngs.templater.example</groupId>
<artifactId>sheet-report-example</artifactId>
<packaging>jar</packaging>
<version>5.2.0</version>
<version>6.0.0</version>
<name>SheetReportExample</name>
<url>https://github.com/ngs-doo/TemplaterExamples</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static void main(final String[] args) throws Exception {

tpl.process(data);

tpl.flush();
tpl.close();
fos.close();
Desktop.getDesktop().open(tmp);
}
Expand Down
4 changes: 2 additions & 2 deletions Advanced/TemplaterServer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM ubuntu:18.10
FROM ubuntu:20.04

RUN apt update && apt install openjdk-11-jre-headless libreoffice-common libreoffice-java-common libreoffice-writer libreoffice-calc wget -yq

RUN wget -q https://github.com/ngs-doo/TemplaterExamples/releases/download/v4.0.0/templater-server.jar
RUN wget -q https://github.com/ngs-doo/TemplaterExamples/releases/download/v6.0.0/templater-server.jar

COPY templater.lic .

Expand Down
7 changes: 4 additions & 3 deletions Advanced/TemplaterServer/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Once the docker has started, REST API can be consumed by calling:

with body for the actual document. PDF document will be returned as the response.

Alternatively [portable version of LibreOffice](https://www.libreoffice.org/download/portable-versions/) can be used instead of Docker.
Alternatively, on Windows [portable version of LibreOffice](https://www.libreoffice.org/download/portable-versions/) can be used instead of Docker.

### REST server for other languages

Expand All @@ -46,6 +46,8 @@ The workflow for using the server is:
* PUT /document?template=file.ext - using JSON for request body will process the previously saved template with provided JSON. To create PDF `Accept: application/pdf` can be used
* DELETE /document?template=file.ext - will remove previously saved template document

Templater supports cancellation pattern via token interface. This example uses timeout based implementation to limit the execution duration of the request.

### PDF conversion libraries for docx

Several libraries/applications are available for PDF output:
Expand Down Expand Up @@ -117,7 +119,7 @@ While :collapse will remove tags, processor will still continue trying to replac
#### Dynamic resize

Dynamic resize works on Object[][] or List<List<Object>> types as long as dimensions are the same.
In .NET special two dimensional type: Array[,] is also supported.
In .NET special two-dimensional type: Array[,] is also supported.

When combined with **merge-nulls** and **span-nulls** multiple cells can be combined into one.

Expand Down Expand Up @@ -163,7 +165,6 @@ Tags can be combined with static/dynamic pictures in each row/page.
Row context can span multiple rows/columns.
Since context can be nested, this allows for very complex document layouts.
Text based watermark is supported.
Currently it requires usage of :all metadata, since watermark is repeated multiple times in different document parts.

#### Scorecard

Expand Down
2 changes: 1 addition & 1 deletion Advanced/TemplaterServer/TemplaterServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Templater" Version="5.2.0" />
<PackageReference Include="Templater" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions Advanced/TemplaterServer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>hr.ngs.templater.example</groupId>
<artifactId>templater-server</artifactId>
<packaging>jar</packaging>
<version>5.2.0</version>
<version>6.0.0</version>
<name>TemplaterServer</name>
<url>https://github.com/ngs-doo/TemplaterExamples</url>

Expand All @@ -20,7 +20,7 @@
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo</url>
<url>https://artifact.aspose.com/repo/</url>
</repository>
</repositories>
<dependencies>
Expand All @@ -37,13 +37,13 @@
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>2.7.3</version>
<version>3.9.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>19.12</version>
<version>21.7</version>
<classifier>jdk17</classifier>
<scope>compile</scope>
</dependency>
Expand Down
3 changes: 3 additions & 0 deletions Advanced/TemplaterServer/src/SharedResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SharedResource
internal readonly string[] TemplateFiles;
internal readonly string TemplateHtml;
internal readonly string DefaultHtml;
internal readonly int Timeout = 30;
internal readonly Dictionary<string, string> Jsons;
internal readonly Dictionary<string, PdfConverter> PdfConverters = new Dictionary<string, PdfConverter>();

Expand All @@ -25,6 +26,8 @@ public SharedResource(string[] args)
{
if (a.StartsWith("pdf="))
pdf = a.Substring("pdf=".Length).Split(',');
else if (a.StartsWith("timeout="))
Timeout = int.Parse(a.Substring("timeout=".Length));
}

foreach (var t in types)
Expand Down
Loading

0 comments on commit 9202b20

Please sign in to comment.