0

我正在尝试使用 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(.....)

4

0 回答 0