Commit 70ae76de authored by RobJBarr's avatar RobJBarr
Browse files

Added pdf downloading

parent 9a16fc29
Pipeline #172742 canceled with stages
in 45 seconds
......@@ -3,6 +3,7 @@ package ic.doc;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import ic.doc.web.MarkdownResultPage;
import ic.doc.web.PDFResultPage;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
......@@ -28,10 +29,12 @@ public class WebServer {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String query = req.getParameter("q");
String isMarkdown = req.getParameter("markdown");
String downloadType = req.getParameter("markdown");
if (query == null) {
new IndexPage().writeTo(resp);
} else if("md".equals(isMarkdown)) {
} else if ("pdf".equals(downloadType)){
new PDFResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
} else if("md".equals(downloadType)) {
new MarkdownResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
} else {
new HTMLResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
......
......@@ -8,7 +8,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.HttpOutput;
public class MarkdownResultPage implements Page {
......
package ic.doc.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
public class PDFResultPage implements Page {
private final String query;
private final String answer;
public PDFResultPage(String query, String answer) {
this.query = query;
this.answer = answer;
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
File markdown = new File("result.md");
// Header
FileWriter writer = new FileWriter("result.md");
writer.write(query + "\n");
writer.write("======\n");
// Content
if (answer == null || answer.isEmpty()) {
writer.write("Sorry, we did not understand your query.\n");
} else {
writer.write(answer);
}
writer.close();
ProcessBuilder pb = new ProcessBuilder();
pb.command("pandoc -s -o result.pdf result.md");
pb.start();
File pdf = new File("result.pdf");
// Byte streams
InputStream inputStream = new FileInputStream(pdf);
OutputStream outputStream = new FileOutputStream(pdf);
inputStream.transferTo(outputStream);
// Remove temporary file
markdown.delete();
pdf.delete();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment