我在struts2动作中使用以下代码,使用飞碟将HTML转换为PDF:它只是按原样打印样式代码而不应用它。
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
public String execute() throws Exception
{
String result="success";
try {
String url = "C:\\Users\\Anoop\\Desktop\\files\\form1.html";
System.setProperty("file.encoding", "UTF-8");
String content = PDFGeneratorFromHTML.readFile(url,Charset.forName("UTF-8") );
String k = "<html><body> This is my Project </body></html>";
OutputStream file = new FileOutputStream(new File("D:\\Test2.pdf"));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(content);
renderer.layout();
renderer.createPDF(file);
file.close();
inputStream = new FileInputStream(new File("D:\\Test2.pdf"));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
我的带有css文件的HTML是:
/*
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<style type="text/css">
.text-right{text-align:right;}
.text-center{text-align:center;}
table{
margin: 10px 0px;
clear: both;
border-top: none;
border-bottom: none;
text-align: left;
}
.border-left{border-left: solid 1px #e6e4df;}
/*table tr:nth-child(2n+1){background-color:#09C; color:#fff;}*/
table tr th{border:solid 1px #bfbfba; border-left:none; border-right:none; padding:15px 0 !important; background-color:#f5f5f2; color:#333;}
table tr td, table tr th{border-bottom:solid 1px #e6e4df; padding:5px; border-right:solid 1px #e6e4df;}
table tr td.total{background-color:#CCC;}
.header-uni-form{width:960px; height:auto; float:left; padding:20px 0;}
.header-uni-form .uni-form-logo{
width:307px; height:auto; float:left; padding:20px 0;
}
.border-none{border:none !important;}
.w20{width:20%;}
.wi60{width:60%;}
.wi10{width:10%;}
.customer-info{width:940px; height:auto; float:left; margin:10px 0;}
.customer-info .customer-left{position: relative; width:620px; height:170px; float:left; border-radius:10px; -moz-border-radius:10px; -ms-border-radius:10px; -o-border-radius:10px; -webkit-border-radius:10px; border:1px solid #333;}
.customer-info .customer-left .bottom-border, .customer-info .customer-right .bottom-border{
border-bottom: solid 1px #333 !important;
text-align: left;
}
.customer-info .customer-left table, .customer-info .customer-right table, .customer-info .customer-right table tr td, .customer-info .customer-left table tr td, .customer-info .customer-right table tr td th, .customer-info .customer-left table tr td th{border:1px solid #fff; margin:10px;}
.customer-info .customer-left .name, .customer-info .customer-right .name{position:absolute; top:-17px; left:20px; color:#000; padding:5px 10px; background:#fff;}
.customer-info .customer-right{width:300px; height:170px; float:left; margin-left:15px; border-radius:10px; -moz-border-radius:10px; -ms-border-radius:10px; -o-border-radius:10px; -webkit-border-radius:10px; position: relative; border:1px solid #333;}
.header-uni-form .uni-form-heading{width:307px; height:auto; float:left; margin-left:15px;}
.header-uni-form .invoice-form-uni{width:307px; height:auto; float:left; margin-left:15px; padding:20px 0;}
</style>
<body>
<div style="width:960px; height:auto; margin:0 auto; clear:both;">
<div class="header-uni-form">
<div class="uni-form-logo text-center">Logo</div>
<div class="uni-form-heading"><strong>UNITELLER FINANCIAL SERVICES</strong>
218 Route 17 North<br />`enter code here`
Rochelle Park, N.J. 07662
</div>
<div class="invoice-form-uni text-center">Invoice No.: IE 07 2014</div>
</div>
<div class="customer-info">
<div class="customer-left">
<span class="name">Customer</span>
<table width="98%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="17%">Name</td>
<td colspan="5" class="bottom-border"> </td>
</tr>
<tr>
<td>Address</td>
<td colspan="5" class="bottom-border"> </td>
</tr>
<tr>
<td>City</td>
<td width="30%" class="bottom-border"> </td>
<td width="14%">State </td>
<td width="13%" class="bottom-border"> </td>
<td width="10%">Zip</td>
<td width="16%" class="bottom-border"> </td>
</tr>
<tr>
<td colspan="6" class="bottom-border"> </td>
</tr>
</table>
</div>
<div class="customer-right">
<table width="95%" border="1">
<tr>
<td width="27%">Date</td>
<td width="73%" class="bottom-border">07/31/14</td>
</tr>
<tr>
<td> </td>
<td width="73%" class="bottom-border"> </td>
</tr>
<tr>
<td>Rep</td>
<td width="73%" class="bottom-border"> </td>
</tr>
<tr>
<td> </td>
<td width="73%" class="bottom-border"> </td>
</tr>
</table>
</div>
</div>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<th width="7%" class="border-left">Qty</th>
<th width="74%" class="text-center">Description</th>
<th width="9%" class="text-center">Unit Price</th>
<th width="10%" class="text-center">TOTAL</th>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Window Pick-up</td>
<td class="text-right">$1.75</td>
<td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Deposit To Account</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Caja Popular Mexicana</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Window Pick-up</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Caja Popular Mexicana Payment order over $6,000 MXP, Caja Popular Mexicana Payment order over $6,000 MXP</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Window Pick-up</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Window Pick-up</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Deposit To Account</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Window Pick-up</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Window Pick-up</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Deposit To Account</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Window Pick-up</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left">4588</td>
<td>Window Pick-up</td>
<td class="text-right">$1.75</td><td class="text-right total">$48,56.00</td>
</tr>
<tr>
<td class="border-left"><span class="border-none"></span></td>
<td> </td>
<td class="text-right"> </td>
<td class="text-right total"> </td>
</tr>
<tr>
<td colspan="2" rowspan="5" class="border-none"> </td>
<td class="border-left">Sub Total</td>
<td class="text-right total">$55.00</td>
</tr>
<tr>
<td class="border-left"> </td>
<td class="text-right total">$0.00</td>
</tr>
<tr>
<td class="border-left"> </td>
<td class="text-right total">$0.00</td>
</tr>
<tr>
<td class="border-left"> </td>
<td class="text-right total"> </td>
</tr>
<tr>
<td class="border-left" style="text-align: right; font-weight: bold;">TOTAL</td>
<td class="text-right total">$88,442.00</td>
</tr>
</table>
</div>
</body>
</html> */
&#13;
css正在打印,但没有在pdf中打印。