从JQuery到Java Servlet的数据,输出将为PDF

问题描述 投票:0回答:1

在这里,我将网络的输出制成PDF时遇到了一些问题。我有一张桌子,里面有一些链接。该链接将通过JQuery $ .post将信息共享到Java Servlet,并且输出将是表中的PDF。这是表格代码。

<table id="lsconf" class="tableku table-bordered table-sm nowrap" style="color: #000;">
                    <thead>
                      <tr>
                        <th class="th-sm" style="background: #1ab188;">Remove</th>
                        <th class="th-sm" style="background: #1ab188;">Export to PDF</th>
                        <th class="th-sm" style="background: #1ab188;">Student ID</th>
                        <th class="th-sm" style="background: #1ab188;">Year</th>
                        <th class="th-sm" style="background: #1ab188;">Semester</th>
                        <th class="th-sm" style="background: #1ab188;">First Name</th>      
                        <th class="th-sm" style="background: #1ab188;">Last Name</th>
                        <th class="th-sm" style="background: #1ab188;">Birthdate</th>
                        <th class="th-sm" style="background: #1ab188;">Sex</th>
                        <th class="th-sm" style="background: #1ab188;">Email</th>
                        <th class="th-sm" style="background: #1ab188;">Department</th>
                        <th class="th-sm" style="background: #1ab188;">Degree</th>                      
                        <th class="th-sm" style="background: #1ab188;">Phone Number</th>
                        <th class="th-sm" style="background: #1ab188;">Location</th>
                        <th class="th-sm" style="background: #1ab188;">Conference Name</th>
                        <th class="th-sm" style="background: #1ab188;">Topic</th>               
                      </tr>
                    </thead>
                    <tbody>
                      <c:forEach items="${locstudList}" var="locstud" >
                      <tr>
                        <td style="text-align: center;"><a class="remove" style="cursor:pointer;">&#10060</a></td>
                        <td style="text-align: center;"><a class="down" style="cursor:pointer;"><i class="fa fa-download" style="color: #fff;" aria-hidden="true"></i></a></td>
                        <td class="id"          style="color: #fff; vertical-align: middle;">${locstud.studentId}</td>
                        <td class="year"        style="color: #fff; vertical-align: middle;">${locstud.year}</td>
                        <td class="semester"    style="color: #fff; vertical-align: middle;">${locstud.semester}</td>
                        <td class="fname"       style="color: #fff; vertical-align: middle;">${locstud.fname}</td>
                        <td class="lname"       style="color: #fff; vertical-align: middle;">${locstud.lname}</td>  
                        <td class="birth"       style="color: #fff; vertical-align: middle;">${locstud.birth}</td>
                        <td class="sex"         style="color: #fff; vertical-align: middle;">${locstud.sex}</td>
                        <td class="email"       style="color: #fff; vertical-align: middle;">${locstud.email}</td>
                        <td class="department"  style="color: #fff; vertical-align: middle;">${locstud.department}</td>
                        <td class="degree"      style="color: #fff; vertical-align: middle;">${locstud.degree}</td>                     
                        <td class="phonenum"    style="color: #fff; vertical-align: middle;">${locstud.phonenum}</td>
                        <td class="location"    style="color: #fff; vertical-align: middle;">${locstud.location}</td>
                        <td class="conference"  style="color: #fff; vertical-align: middle;">${locstud.conference}</td>
                        <td class="topic"       style="color: #fff; vertical-align: middle;">${locstud.topic}</td>
                      </tr>
                    </c:forEach> 
                    </tbody>
                </table>

这里是我的JQuery代码。

$('.down').click(function () {

          var data2 = $(this).parents('tr');
          var col1 = data2.find('td:eq(1)').text();
          markrow = markrow + ';' + col1;         

          alert(markrow);

          $.post(
              "GeneratePDF", 
              {data : markrow},
              function(result) {
              alert(result);              
          });
        }); 

最后这是我的Java Servle代码

Document document = new Document();
        try
        {
            String data = request.getParameter("data"); 
            String filename = "Test123.pdf";   
            response.setContentType("application/pdf");         
            PdfWriter.getInstance(document, response.getOutputStream());
            response.setHeader("Content-Disposition","attachment; filename=" + filename);
            document.open();
            document.add(new Paragraph(data));  
            document.close();
            System.out.println("Done");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   

但是我只得到这个输出

%PDF-1.4 %���� 2 0 obj <>stream x�+�r �26S�00SI�2P�5�1���BҸ4>>>/MediaBox[0 0 595 842]/Parent 3 0 R/Contents 2 0 R/Type/Page>> endobj 1 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj xref 0 7 0000000000 65535 f 0000000331 00000 n 0000000015 00000 n 0000000419 00000 n 0000000174 00000 n 0000000470 00000 n 0000000515 00000 n trailer <]/Info 6 0 R>> %iText-5.4.0 startxref 668 %%EOF
java jquery java-ee-6 eclipse-jee
1个回答
0
投票

问题可能是由于响应方法中的操作顺序。

在调用pdf编写器之前设置响应头],如下所示,可以解决此问题。

Document document = new Document();
try {

    // Set the response header before sending the output stream to pdf writer
    String filename = "Test123.pdf";   
    response.setContentType("application/pdf");         
    response.setHeader("Content-Disposition","attachment; filename=" + filename);

    // Send output stream to pdf writer
    PdfWriter.getInstance(document, response.getOutputStream());
    document.open();
    String data = request.getParameter("data"); 
    document.add(new Paragraph(data));
    document.close();
    System.out.println("Done");
} catch(Exception e) {
    e.printStackTrace();
}
© www.soinside.com 2019 - 2024. All rights reserved.