我正在使用ajax,它首先使用itext库生成基于jsp数据的pdf。我的pdf正在服务器上生成但无法下载该pdf。 我想通过jquery ajax b'coz来做它我不希望jsp页面得到刷新。
我在点击下载按钮
时调用此功能function download()
{
alert("down");
var e = document.getElementById("companyName");
var company = e.options[e.selectedIndex].value;
var billNo=$("#billNo").val();
var billDate=$("#billDate").val();
alert(billDate);
var totalAmount=$("#totalAmount").val();
var amount=$("#amount").val();
var discount=$("#discount").val();
var tax=$("#tax").val();
var subTotal=$("#subTotal").val();
var dataObj = {
"companyName":company,
"billNo":billNo,
"billDate":billDate,
"totalAmount":totalAmount,
"amount":amount,
"discount":discount,
"tax":tax,
"subTotal":subTotal,
data : []
};
var table = document.getElementById("tableId");
var rowCount = table.rows.length;
for(var i=1; i<rowCount; i++)
{
var row = table.rows[i];
var value=row.cells[2].innerHTML;
var jsonData = {};
var columnName = "itemName";
var columnName1="itemPrice";
var column2="quantity";
jsonData[columnName] =row.cells[1].innerHTML ;
jsonData[columnName1] = row.cells[3].innerHTML;
jsonData[column2] = row.cells[2].innerHTML;
dataObj.data.push(jsonData);
}
var data1=JSON.stringify(dataObj);
console.log(dataObj);
console.log(data1);
$.ajax({
url: "downloadbill",
contentType: 'application/json',
data:data1,
type: 'POST',
datatype:'json',
async: true,
success: function (res) {
alert("downloaded Successfully");
}
});
}
struts.xml中的操作
<action name="downloadbill" class="client.user.bill.action.DownloadIssueBillAction">
<interceptor-ref name="json">
<param name="enableSMD">true</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">1024</param>
</result>
</action>
DownloadIssueBillAction
public class DownloadIssueBillAction implements ModelDriven<Bill>, ServletRequestAware
{
Bill bill=new Bill();
private String billNo;
private String companyName;
private String billDate;
private String totalAmount;
private String amount;
private String discount;
private ArrayList<Bill> data;
private String subTotal;
private String tax;
private HttpServletRequest servletRequest;
private HttpServletResponse servletResponse;
private InputStream inputStream;
private String fileName;
private long contentLength;
public String execute() throws FileNotFoundException
{
try {
String filePath = servletRequest.getRealPath("/");
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, "Neeraj"+getBillNo()+".pdf");
OutputStream file = new FileOutputStream(fileToCreate);
Document document = new Document();
PdfWriter.getInstance(document, file);
PdfPTable table = new PdfPTable(3);
PdfPCell c1 = new PdfPCell(new Phrase("Item Name"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
c1.setBorder(Rectangle.NO_BORDER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Item Quantity"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
c1.setBorder(Rectangle.NO_BORDER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Item Price"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
c1.setBorder(Rectangle.NO_BORDER);
table.addCell(c1);
table.setHeaderRows(1);
for(int x=0; x<data.size(); x++){
c1 = new PdfPCell(new Phrase(data.get(x).getItemName(),FontFactory.getFont(FontFactory.TIMES_ROMAN, 8)));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
c1.setBorder(Rectangle.NO_BORDER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase(data.get(x).getQuantity(),FontFactory.getFont(FontFactory.TIMES_ROMAN, 8)));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
c1.setBorder(Rectangle.NO_BORDER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase(data.get(x).getItemPrice(),FontFactory.getFont(FontFactory.TIMES_ROMAN, 8)));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
c1.setBorder(Rectangle.NO_BORDER);
table.addCell(c1);
}
table.setSpacingBefore(30.0f); // Space Before table starts, like margin-top in CSS
table.setSpacingAfter(30.0f);
PdfPTable table2 = new PdfPTable(1);
c1 = new PdfPCell(new Phrase("SUB TOTAL:"+getSubTotal(),FontFactory.getFont(FontFactory.TIMES_ROMAN, 10)));
c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
c1.setBorder(Rectangle.NO_BORDER);
table2.addCell(c1);
c1 = new PdfPCell(new Phrase(" TAX:"+getTax(),FontFactory.getFont(FontFactory.TIMES_ROMAN, 10)));
c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
c1.setBorder(Rectangle.NO_BORDER);
table2.addCell(c1);
c1 = new PdfPCell(new Phrase(" AMOUNT:"+getAmount(),FontFactory.getFont(FontFactory.TIMES_ROMAN, 10)));
c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
c1.setBorder(Rectangle.NO_BORDER);
table2.addCell(c1);
c1 = new PdfPCell(new Phrase(" DISCOUNT:"+getDiscount(),FontFactory.getFont(FontFactory.TIMES_ROMAN, 10)));
c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
c1.setBorder(Rectangle.NO_BORDER);
table2.addCell(c1);
c1 = new PdfPCell(new Phrase(" TOTAL:"+getTotalAmount(),FontFactory.getFont(FontFactory.TIMES_ROMAN, 10)));
c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
c1.setBorder(Rectangle.NO_BORDER);
table2.addCell(c1);
//Now Insert Every Thing Into PDF Document
document.open();//PDF document opened........
document.add(Chunk.NEWLINE); //Something like in HTML :-)
Paragraph preface = new Paragraph("Neeraj Industries Pvt Ltd");
preface.setAlignment(Element.ALIGN_CENTER);
document.add(preface);
Paragraph preface1 = new Paragraph("Bill No"+getBillNo(),new Font(FontFamily.TIMES_ROMAN, 16));
preface1.setAlignment(Element.ALIGN_CENTER);
document.add(preface1);
PdfPTable table1 = new PdfPTable(2);
PdfPCell cellOne = new PdfPCell(new Phrase("Client Name:"+getCompanyName(),FontFactory.getFont(FontFactory.TIMES_ROMAN, 12)));
PdfPCell cellTwo = new PdfPCell(new Phrase("Date:"+getBillDate(),FontFactory.getFont(FontFactory.TIMES_ROMAN, 12)));
cellOne.setBorder(Rectangle.NO_BORDER);
cellTwo.setBorder(Rectangle.NO_BORDER);
cellOne.setHorizontalAlignment (Element.ALIGN_CENTER);
cellOne.setPadding (10.0f);
cellTwo.setHorizontalAlignment (Element.ALIGN_CENTER);
cellTwo.setPadding (10.0f);
table1.setSpacingBefore(30.0f); // Space Before table starts, like margin-top in CSS
table1.addCell(cellOne);
table1.addCell(cellTwo);
document.add(table1);
document.add(table);
document.add(table2);
document.close();
file.close();
System.out.println("Pdf created successfully..");
setInputStream(new FileInputStream(fileToCreate));
setFileName(fileToCreate.getName());
setContentLength(fileToCreate.length());
System.out.println("File to create :"+fileToCreate.getName());
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
@Override
public Bill getModel() {
// TODO Auto-generated method stub
return bill;
}
public String getBillNo() {
return billNo;
}
public void setBillNo(String billNo) {
this.billNo = billNo;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getBillDate() {
return billDate;
}
public void setBillDate(String billDate) {
this.billDate = billDate;
}
public String getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(String totalAmount) {
this.totalAmount = totalAmount;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getDiscount() {
return discount;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public ArrayList<Bill> getData() {
return data;
}
public void setData(ArrayList<Bill> data) {
this.data = data;
}
public String getSubTotal() {
return subTotal;
}
public void setSubTotal(String subTotal) {
this.subTotal = subTotal;
}
public String getTax() {
return tax;
}
public void setTax(String tax) {
this.tax = tax;
}
//add the call to the table
@Override
public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public long getContentLength() {
return contentLength;
}
public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
}
答案 0 :(得分:0)
两种选择: 只需对PDF进行正常页面加载,浏览器可能无法卸载当前页面。取决于浏览器。
或者,在javascrpt中创建一个iframe并将其添加到页面中。将src设置为要下载的PDF的URL。