LibreOfficeRenderer.java
2.73 KB
1
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.brainfood.ofbiz;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import org.ofbiz.base.util.UtilIO;
import org.ofbiz.base.component.ComponentConfig;
import org.ofbiz.base.component.ComponentException;
import groovy.text.SimpleTemplateEngine;
import groovy.text.Template;
import groovy.lang.Writable;
public class LibreOfficeRenderer {
public static void service(HttpServletRequest request, HttpServletResponse response, Map<String, Object> context)
throws InterruptedException, IOException, ClassNotFoundException, ComponentException {
Object templateFile = context.get("_template");
if (templateFile != null) {
String fullPath = ComponentConfig.getRootLocation("driverup") + "/pdf-templates/" + templateFile.toString();
String template = UtilIO.readString(new FileInputStream(fullPath));
// FIXME: This is a hack. The Libreoffice file contains a $ declaration and this fixes it.
context.put("Linux_X86_64", "$Linux_X86_64");
context.put("Build", "$Build");
StringBuilder output = new StringBuilder();
int idx = 0;
while (idx < template.length()) {
int remaining = template.length() - idx;
int chunkLength = remaining < 64000 ? remaining : 64000;
String tmplChunk = template.substring(idx, idx + chunkLength);
SimpleTemplateEngine engine = new SimpleTemplateEngine();
Writable stTemplate = engine.createTemplate(tmplChunk).make(context);
output.append(stTemplate.toString());
idx += chunkLength;
}
File outputFile = File.createTempFile("bfdcs-", "-tmpl");
UtilIO.writeString(outputFile, output.toString());
ProcessBuilder pb = new ProcessBuilder("libreoffice", "--headless", "--convert-to", "pdf", outputFile.getPath(), "--outdir", outputFile.getParentFile().getPath());
Map<String, String> env = pb.environment();
env.put("HOME", "/home/ofbiz");
env.put("USERNAME", "ofbiz");
Process process = pb.start();
InputStream is = process.getInputStream();
process.waitFor();
response.setContentType("application/pdf");
UtilIO.copy(new FileInputStream(outputFile.getPath() + ".pdf"), true, response.getOutputStream(), true);
outputFile.delete();
new File(outputFile.getPath() + ".pdf").delete();
}
}
}