38519f21 by Ean Schuessler

LibreOffice render helper #3913

1 parent ef4b37ae
......@@ -141,9 +141,7 @@ public class DirectControlServlet extends HttpServlet {
}
} else {
// Check if the request is a backbone style "emulateJSON" request
if (contentType != null &&
contentType.indexOf("x-www-form-urlencoded") != -1 &&
request.getParameter("model") != null) {
if (contentType != null && contentType.indexOf("x-www-form-urlencoded") != -1 && request.getParameter("model") != null) {
Debug.logInfo("MODEL: " + request.getParameter("model"), module);
JSON json = new JSON(new StringReader(request.getParameter("model")));
Map<String,Object> items = json.JSONObject();
......@@ -186,6 +184,14 @@ public class DirectControlServlet extends HttpServlet {
}
}
// Check if there is an output handler
String outputHandler = "JSON";
if (serviceName.indexOf("|") != -1) {
String[] parts = serviceName.split("\\|");
serviceName = parts[0];
outputHandler = parts[1];
}
Debug.logInfo("Service name" +serviceName + " mapped for " + pathInfo + "#" + method, module);
// If the sessionId parameter is set, attempt to look up the corresponding
......@@ -243,6 +249,7 @@ public class DirectControlServlet extends HttpServlet {
response.setStatus(400);
}
if ("JSON".equals(outputHandler)) {
response.setContentType("application/x-json");
PrintWriter writer = response.getWriter();
......@@ -254,9 +261,13 @@ public class DirectControlServlet extends HttpServlet {
String jsonStr = json.toString();
response.setContentLength(jsonStr.getBytes("UTF8").length);
writer.write(jsonStr);
writer.flush();
writer.close();
}
if ("PDF".equals(outputHandler)) {
LibreOfficeRenderer.service(request, response, result);
}
} catch (Throwable t) {
response.setStatus(500);
PrintWriter writer = response.getWriter();
......
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 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 {
Object templateFile = context.get("_template");
if (templateFile != null) {
String template = UtilIO.readString(new FileInputStream(templateFile.toString()));
// 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");
SimpleTemplateEngine engine = new SimpleTemplateEngine();
Writable stTemplate = engine.createTemplate(template).make(context);
String output = stTemplate.toString();
File outputFile = File.createTempFile("bfdcs-", "-tmpl");
UtilIO.writeString(outputFile, output);
List<String> args = Arrays.asList(new String[]{"libreoffice", "--headless", "--convert-to", "pdf", outputFile.getPath(), "--outdir", "/tmp"});
Process process = new ProcessBuilder(args).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();
}
}
}