Contents
JLR(Java LaTeX Report)是基于MiKTeX和Velocity模板引擎构建的可生成LaTeX的一个Java包,对Tutorial的示例进行试验和稍加改动。
JLR 概述
由nixo-soft开发的JLR(Java LaTex Report)的Java包,可应用于生成LaTeX。
从这段官方介绍可以看出,免费没开源,在Maven上也没有,基于MiKTeX和Velocity模板引擎构建。Version: 2.1 (Last modified: 20 Aug 2013 19:10:19)
Filesize: 1.25 MB (compressed)
Features:Requirements: Java Runtime Environment (JRE) 6 Update 20 / Installed or portable LaTeX Distribution(e.g. MiKTeX or MiKTeX Portable)
Simple conversion of Java to PDF (Java2PDF)
Use of the very stable, proven and free LaTeX software package for generating PDF files
Uses the Apache Velocity Template Engine
Lightweight, small and powerful
It is a free library (Freeware)
Hint: The Open Source PDF-Renderer is suitable for displaying PDFs within a Java application
Tutorial: http://www.nixo-soft.de/tutorials/jlr/JLRTutorial.html
JLR 范例
一个由Tutorial稍加改动的例子,文档结构图如下。要引入jlr.jar,准备好LaTeX模板文件invoiceTemplate.tex和invoice的CTAN包(下载)
在foreach处有改动的 invoiceTemplate.tex1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27documentclass{letter}
usepackage{invoice}
address{Josh Parker \
5650 Webster Ave \
West Palm Beach, Florida 33405 \
date{31-May-2010}
begin{document}
begin{letter}{$CustomerName \
$CustomerStreet \
$CustomerZip}
opening{Invoice no. $Number}
Dear customer,
...
begin{invoice}{Euro}{0}
ProjectTitle{Example Project}
#foreach( $s in $services )
Fee{$s.getName()} {$s.getUnit()} {$s.getCount()}
#end
end{invoice}
closing{Best regards}
end{letter}
end{document}
如果用的是MikTeX portable,得改用JLRGenerator,具体见Tutorial,这里的是对应JLRConverter的 App.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50public class{
public static void main(String[] args){
File workingDirectory = new File(System.getProperty("user.dir")
+ File.separator + "resources");
File template = new File(workingDirectory.getAbsolutePath()
+ File.separator + "invoiceTemplate.tex");// 模板路径
File outputDir = new File(workingDirectory.getAbsolutePath()
+ File.separator + "invoice");// 输出路径
if (!outputDir.isDirectory()) {
outputDir.mkdir();// 确保输出路径存在
}
File invoice1 = new File(outputDir.getAbsolutePath() + File.separator
+ "invoice1.tex");// 账单文件
try {
// 第一步
JLRConverter converter = new JLRConverter(workingDirectory);
// 第二步
converter.replace("Number", "1");
converter.replace("CustomerName", "Ivan Pfeiffer");
converter.replace("CustomerStreet", "Schwarzer Weg 4");
converter.replace("CustomerZip", "13505 Berlin");
// 替换掉Tutorial中的 ArrayList> services = new
// ArrayList>();………………
List services = new ArrayList<>();
services.add(new Service("Apple", 100.2, 12.33));
services.add(new Service("Orange", 20.44, 0.99));
services.add(new Service("Banana", 0.23, 1.1));
converter.replace("services", services);
// 第三步
if (!converter.parse(template, invoice1)) {
System.out.println(converter.getErrorMessage());
}
JLROpener.open(invoice1);// 打开账单文件
// JLROpener.print(invoice1);//然而并不管用
} catch (IOException e) {
e.printStackTrace();
}
}
}
增添的 Service.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class Service{
private String name;
private double unit;//单价
private double count;//数量
public Service(){
}
public Service(String name,double unit,double count){
this.name=name;
this.unit=unit;
this.count=count;
}
public String getName(){
return name;
}
public double getUnit(){
return unit;
}
public double getCount(){
return count;
}
}
程序运行后,得到替换后的LaTeX文件 invoice1.tex1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27documentclass{letter}
usepackage{invoice}
address{Josh Parker \
5650 Webster Ave \
West Palm Beach, Florida 33405 \
date{31-May-2010}
begin{document}
begin{letter}{Ivan Pfeiffer \
Schwarzer Weg 4 \
13505 Berlin}
opening{Invoice no. 1}
Dear customer,
...
begin{invoice}{Euro}{0}
ProjectTitle{Example Project}
Fee{Apple} {100.2} {12.33}
Fee{Orange} {20.44} {0.99}
Fee{Banana} {0.23} {1.1}
end{invoice}
closing{Best regards}
end{letter}
end{document}
相对应可生成的pdf文件
参考