Commit ad51afcd authored by tianyi's avatar tianyi
Browse files

Added pdf

parent 0f4de16d
Pipeline #63429 canceled with stages
in 50 minutes and 24 seconds
package ic.doc;
import static ic.doc.MarkDownCreator.toMarkDown;
import ic.doc.web.Page;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import javax.servlet.http.HttpServletResponse;
public class PdfCreator implements Page {
......@@ -21,14 +16,20 @@ public class PdfCreator implements Page {
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
public void writeTo(HttpServletResponse resp) throws IOException, InterruptedException {
resp.setContentType("application/pdf");
OutputStream writer = resp.getOutputStream();
File markdown = toMarkDown(answer, query);
// turn markdown to a pdf using pandoc
InputStream stream = new FileInputStream(markdown);
writer.write(stream.readAllBytes());
ProcessBuilder pb = new ProcessBuilder("pandoc",markdown.getName() , "-o","res.pdf");
File dir = new File("/tmp");
pb.directory(dir);
Process process = pb.start();
process.waitFor();
File pdf = new File("/tmp/res.pdf");
writer.write(new FileInputStream(pdf).readAllBytes());
writer.close();
markdown.delete();
pdf.delete();
}
}
......@@ -43,7 +43,11 @@ public class WebServer {
new MarkDownCreator(query, result).writeTo(resp);
}else{
System.out.println("Is a pdf");
new PdfCreator(query, result).writeTo(resp);
try{
new PdfCreator(query, result).writeTo(resp);
}catch (InterruptedException e){
}
}
}
}
......
......@@ -4,5 +4,5 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public interface Page {
void writeTo(HttpServletResponse resp) throws IOException;
void writeTo(HttpServletResponse resp) throws IOException, InterruptedException;
}
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