-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfController.java
More file actions
40 lines (36 loc) · 1.18 KB
/
pdfController.java
File metadata and controls
40 lines (36 loc) · 1.18 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
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.pdf.PdfWriter;
@RequestMapping(value = "toPdf", method = RequestMethod.POST)
public void toPdf(
@RequestBody(required=true) String body,
@RequestBody(required=true) String cssUrl,
HttpServletRequest request,
HttpServletResponse response) throws DocumentException, UnsupportedEncodingException{
StringBuffer html = new StringBuffer();
html.append("<html>");
html.append("<head>");
html.append(cssUrl);
html.append("</head>");
html.append("<body>");
body = URLDecoder.decode(body, "UTF8");
html.append(body);
html.append("</body>");
html.append("</html>");
try {
response.reset();
response.setContentType("application/pdf");
Document doc = new Document(PageSize.A4);
PdfWriter.getInstance(doc, response.getOutputStream());
doc.open();
HTMLWorker hw = new HTMLWorker(doc);
hw.parse(new StringReader(html.toString()));
doc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}