java中的HTML到PDF转换

时间:2017-03-03 05:37:52

标签: html parsing itext

我正在尝试使用HTML创建PDF文件。

以下是我的代码。

try {
     String k = "<html> <head>   <title></title>   <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/> </head> <body text='#000000' link='#000000' alink='#000000' vlink='#000000'> <table width='100%' cellpadding='0' cellspacing='0' border='0'> <tr><td width='50%'>&nbsp;</td><td align='center'><table style='border: 1px solid transparent; width: 1014px' cellpadding='0' cellspacing='0' border='1' bgcolor='white'> <tr>   <td style='width: 50px; height: 1px;'></td>   <td style='width: 2px; height: 1px;'></td>   <td style='width: 1px; height: 1px;'></td>   <td style='width: 1px; height: 1px;'></td>   <td style='width: 32px; height: 1px;'></td>   <td style='width: 46px; height: 1px;'></td>   <td style='width: 139px; height: 1px;'></td>   <td style='width: 6px; height: 1px;'></td>   <td style='width: 144px; height: 1px;'></td>   <td style='width: 127px; height: 1px;'></td>   <td style='width: 11px; height: 1px;'></td>   <td style='width: 60px; height: 1px;'></td>   <td style='width: 16px; height: 1px;'></td>   <td style='width: 17px; height: 1px;'></td>   <td style='width: 5px; height: 1px;'></td>   <td style='width: 280px; height: 1px;'></td>   <td style='width: 2px; height: 1px;'></td>   <td style='width: 75px; height: 1px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 42px;'></td> </tr> <tr valign='top'>   <td style='width: 50px; height: 20px;'></td>   <td nowrap colspan='8' style='width: 371px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px;'>International Trade Department</span></td>   <td colspan='9' style='width: 593px; height: 20px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 10px;'></td> </tr> <tr valign='top'>   <td colspan='7' style='width: 271px; height: 20px;'></td>   <td nowrap colspan='8' style='text-align: center;width: 386px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px; font-weight: bold; text-decoration: underline;'>ACCOUNTING VOUCHER DIFFERENT TEMPLATE</span></td>   <td colspan='3' style='width: 357px; height: 20px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 9px;'></td> </tr> <tr valign='top'>   <td colspan='11' style='width: 559px; height: 20px;'></td>   <td nowrap style='width: 60px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px; font-weight: bold;'>Dated:</span></td>   <td nowrap colspan='5' style='width: 320px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px;'>11/01/2017</span></td>   <td style='width: 75px; height: 20px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 2px;'></td> </tr> <tr valign='top'>   <td colspan='2' style='width: 52px; height: 1px;'></td>   <td nowrap colspan='3' rowspan='2' style='width: 34px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px; font-weight: bold;'>To</span></td>   <td colspan='13' style='width: 928px; height: 1px;'></td> </tr> <tr valign='top'>   <td colspan='2' style='width: 52px; height: 19px;'></td>   <td nowrap colspan='5' rowspan='2' style='width: 462px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px;'>Intellect</span></td>   <td colspan='8' style='width: 466px; height: 19px;'></td> </tr> <tr valign='top'>   <td colspan='5' style='width: 86px; height: 1px;'></td>   <td colspan='8' style='width: 466px; height: 1px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 1px;'></td> </tr> <tr valign='top'>   <td colspan='5' style='width: 86px; height: 20px;'></td>   <td nowrap colspan='5' style='width: 462px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px;'>jay coach Goregoan Mumbai Maharashtra &nbsp;INDIA</span></td>   <td colspan='8' style='width: 466px; height: 20px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 5px;'></td> </tr> <tr valign='top'>   <td colspan='3' style='width: 53px; height: 20px;'></td>   <td nowrap colspan='3' style='width: 79px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px; font-weight: bold;'>Ref No. :</span></td>   <td nowrap colspan='4' style='width: 416px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px;'>0202091116FL1739 &nbsp; </span></td>   <td style='width: 11px; height: 20px;'></td>   <td nowrap colspan='2' style='width: 76px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px; font-weight: bold;'>Product:</span></td>   <td nowrap colspan='4' style='width: 304px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px;'>Import LC - Sight_AT</span></td>   <td style='width: 75px; height: 20px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 1px;'></td> </tr> <tr valign='top'>   <td colspan='11' style='width: 559px; height: 20px;'></td>   <td nowrap colspan='3' style='width: 93px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px; font-weight: bold;'>Operation:</span></td>   <td nowrap colspan='3' style='width: 287px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px;'>Issuance</span></td>   <td style='width: 75px; height: 20px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 21px;'></td> </tr> <tr valign='top'>   <td colspan='4' style='width: 54px; height: 20px;'></td>   <td nowrap colspan='13' style='width: 885px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px; font-weight: bold;'>Charges :</span></td>   <td style='width: 75px; height: 20px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 9px;'></td> </tr> <tr valign='top'>   <td colspan='4' style='width: 54px; height: 80px;'></td>   <td nowrap colspan='12'> <table width='100%' cellspacing='0' cellpadding='0' style='border: #000000 1px solid'> <tr> <td nowrap valign='top' width='294' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> Description</span></td> <td nowrap valign='top' width='294' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> Debit Amount</span></td> <td nowrap valign='top' width='295' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> Credit Amount</span></td> </tr> <tr> <td nowrap valign='top' width='294' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> Bank Commission</span></td> <td nowrap valign='top' width='294' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> USD 300.00 (1) A/C No: [890890123126]</span></td> <td nowrap valign='top' width='295' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> USD 300.00 (1) A/C No: [3456734567]</span></td> </tr> </table> </td>   <td colspan='2' style='width: 77px; height: 80px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 26px;'></td> </tr> <tr valign='top'>   <td colspan='4' style='width: 54px; height: 20px;'></td>   <td nowrap colspan='13' style='width: 885px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 18.0px; font-weight: bold;'>Accounting :</span></td>   <td style='width: 75px; height: 20px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 11px;'></td> </tr> <tr valign='top'>   <td colspan='4' style='width: 54px; height: 120px;'></td>   <td nowrap colspan='13'> <table width='100%' cellspacing='0' cellpadding='0' style='border: #000000 1px solid'> <tr> <td nowrap valign='top' width='294' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> Account Set</span></td> <td nowrap valign='top' width='294' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> Debit Role</span></td> <td nowrap valign='top' width='297' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> Credit Role</span></td> </tr> <tr> <td nowrap valign='top' width='294' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> BOOK LC CONTINGENT LIABILITY</span></td> <td nowrap valign='top' width='294' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> USD 1136668.00  A/C No: [3456734567]</span></td> <td nowrap valign='top' width='297' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> USD 1136668.00 A/C No: [3456734567]</span></td> </tr> <tr> <td nowrap valign='top' width='294' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> BOOK CASH MARGIN</span></td> <td nowrap valign='top' width='294' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> USD 397833.80  A/C No: [890890123126]</span></td> <td nowrap valign='top' width='297' height='40' style='word-wrap:break-word; text-align:left; border-top-style: solid; border-top-width: 1px; border-top-color: #000000; border-left-style: solid; border-left-width: 1px; border-left-color: #000000; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #000000; border-right-style: solid; border-right-width: 1px; border-right-color: #000000;'> <span style='font-family: Arial; font-size: 18; font-weight: ; font-style: ; text-decoration: '> USD 397833.80 A/C No: [3456734567]</span></td> </tr> </table> </td>   <td style='width: 75px; height: 120px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 45px;'></td> </tr> <tr valign='top'>   <td colspan='2' style='width: 52px; height: 16px;'></td>   <td nowrap colspan='6' style='width: 225px; word-wrap: break-word; '><span style='font-family: Arial; font-size: 14.0px;'>TRADE SERVICES DEPT.</span></td>   <td colspan='10' style='width: 737px; height: 16px;'></td> </tr> <tr valign='top'>   <td colspan='18' style='width: 1014px; height: 621px;'></td> </tr> </table></td><td width='50%'>&nbsp;</td></tr> </table> </body> </html>";

        OutputStream file = new FileOutputStream(new File("D:\\NEW_PDF.pdf"));
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, file);
        document.open();
        InputStream is = new ByteArrayInputStream( k.getBytes() );
        //document.close();
        //XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);

        HTMLWorker htmlWorker = new HTMLWorker(document);
        htmlWorker.parse(new StringReader(k));


        document.close();
        file.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

但PDF未正确显示。 有人可以帮帮我吗?

非常感谢你。

0 个答案:

没有答案