我正在使用 jQuery 将我的表单提交到一个 servlet,其中使用 iText 生成 PDF 流。
private void service(HttpServletRequest request, HttpServletResponse response)
{
String oid = "something";
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 12, Font.BOLD, BaseColor.BLUE);
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter pdfWriter = PdfWriter.getInstance(document, baos);
document.open();
Paragraph paragraph = new Paragraph(oid, font);
paragraph.setAlignment(Paragraph.ALIGN_CENTER);
document.add(paragraph);
document.close();
pdfWriter.close();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline;filename=" + oid + ".pdf");
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.setHeader("Pragma", "No-cache");
response.setContentLength(baos.size());
ServletOutputStream sos = response.getOutputStream();
baos.writeTo(sos);
sos.flush();
}
我的问题是,在我的 FORM.submit 函数中,如何将“responseData”转换为 PDF 并在新选项卡/窗口而不是当前 JSP 页面中打开它?
$("#myform").submit(function() {
$.ajax ({
url: "MyServlet",
type: "POST",
data: values
}) .done (function(responseData) {
// alert(responseData); // alert response from servlet, it's raw data of that PDF
// HOW TO 'convert' RESPONSE TO PDF AND OPEN IT IN A NEW TAB/WINDOW ??
});
return false;
});
任何建议将不胜感激。谢谢!