我试图使用iText生成一个html表到pdf:
HTML:
<script type="text/javascript">
function getPdf(){
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open('POST', 'Hello',false);
xmlHttpRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlHttpRequest.send("table=" + document.getElementsByName("123").innerHTML);
}
</script>
</head>
<body>
<table id="123">
<tr>
<td>data</td>
</tr>
<tr>
<td>data</td>
</tr>
<tr>
<td><input type='button' onclick="getPdf();" value='pdf'</td>
</tr>
</table>
</body>
doPost方法:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/pdf");
try {
Document document = new Document();
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
document.add(new Paragraph(request.getParameter("table")));
document.close();
}
catch (DocumentException de) {
throw new IOException(de.getMessage());
}
}
显然它不使用document.add(.....)