我试图从具有CSS的HTML生成pdf .

以下是代码段

Document document = new Document();
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(outFileName));

        document.open();

        String htmlTxt = FileUtils.readFileToString(new File(dirPath + inpFileName), "UTF-8");
        ByteArrayInputStream fis = new ByteArrayInputStream(htmlTxt.getBytes("UTF-8"));
        XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
        // worker.parseXHtml(pdfWriter, document, fis);

        worker.parseXHtml(pdfWriter, document, new FileInputStream(dirPath + inpFileName),
                new FileInputStream(CSS));

        // close the document
        document.close();
        // close the writer
        pdfWriter.close();

我尝试生成pdf使用 worker.parseXHtml(pdfWriter, document, fis); PDF生成没有

哪个工作正常然后使用 worker.parseXHtml(pdfWriter, document, new FileInputStream(dirPath + inpFileName), new FileInputStream(CSS)); 在这种情况下,pdf生成但没有任何内容 .

我也试过如iText Parse HTML with CSS Example中建议的那样使用CSS解析器

重要说明 - HTML已从jTidy API生成 . 这是HTML和CSS文件

#pdfPreview { font-family: arial }

#pdfPreview .page_1 {position:relative; overflow: hidden;padding: 0px;border: none;width: 90%;margin:0 auto;}
#pdfPreview .page_1 .id_1 {border:none;margin: 0px 0px 0px 0px;padding: 0px;border:none;width: 100%;overflow: hidden;}
#pdfPreview .page_1 .id_2 {border:none;margin: 107px 0px 0px 8px;padding: 0px;border:none;width: 720px;overflow: hidden;}

#pdfPreview .page_1 #dimg1 {position:absolute;top:181px;left:0px;z-index:-1;width:736px;height:677px;}
#pdfPreview .page_1 #dimg1 #img1 {width:736px;height:677px;}

#pdfPreview .page_1 #inl_img1 {position:relative;width:7px;height:17px;}



#pdfPreview #page_2 {position:relative; overflow: hidden;margin: 41px 0px 37px 40px;padding: 0px;border: none;width: 776px;}
#pdfPreview #page_2 #pdfPreview .id_1 {border:none;margin: 0px 0px 0px 0px;padding: 0px;border:none;width: 776px;overflow: hidden;}
#pdfPreview #page_2 .id_2 {border:none;margin: 28px 0px 0px 8px;padding: 0px;border:none;width: 720px;overflow: hidden;}

#pdfPreview #page_2 #dimg1 {position:absolute;top:7px;left:0px;z-index:-1;width:741px;height:935px;}
#pdfPreview #page_2 #dimg1 #img1 {width:741px;height:935px;}

#pdfPreview .vehicleDescriptionTable{
    border: black 1px solid;
}

#pdfPreview .vehicleDescriptionTable table tr td:nth-child(1){
	width: 3%;
    padding-left: 1%;
}

#pdfPreview .vehicleDescriptionTable table tr td:nth-child(2){
	    width: 57%;
}

#pdfPreview .vehicleDescriptionTable table tr td:nth-child(3){
	    width: 3%;
    padding-left: 1%;
}

#pdfPreview .vehicleDescriptionTable table tr td:nth-child(4){
	    width: 37%;
}

#pdfPreview td{
	vertical-align: top;
}

#pdfPreview tr{
	/* line-height: 0em; */
}

#pdfPreview html {
    font:  13px 'Arial';
}

#pdfPreview p.sectionHead {
    text-align: center;
    font: 16px 'Arial' bold;
    background-color: #ccc;
    height: 2em;
    padding-top: .5em;
    margin-bottom: 0;
    border-top: black 1px solid;
    border-left: black 1px solid;
    border-right: black 1px solid;
    /* width: 774px; */
}

#pdfPreview p.subSectionHead {
    font-size: 14px;
    font-weight: bold;
    background-color: #eee;
    margin-top: 0;
    height: 1.5em;
    padding-left: 2%;
    padding-top: .2em;
    margin-bottom: 0;
    border-top: 1px solid black;
    border-bottom: 1px solid black;
    /* border-bottom: none; */
    /* border-right: black 1px solid; */
    /* width: 100%; */
}

#pdfPreview table {
    /* border: 1px solid black; */
    border-bottom: 4em;
    width: 100%;
}

#pdfPreview td:nth-child(3) {
    border-left: black 1px solid;
}

#pdfPreview tbody {
    border-bottom: black 1px solid;
    /* padding-bottom: 1em; */
}

#pdfPreview p {
    margin-top: 0;
    margin-bottom: .2em;
}

#pdfPreview .floatright{
	float: right;
	text-align: right;
}

#pdfPreview .floatleft{
	float:left;
	text-align: left;
}

#pdfPreview .topmargin{
	margin-top:.4em;
}

/*Melissa Page2*/
#pdfPreview #page2, #vehicleListTable, #table3 {
  width: 90%;
  margin: 0 auto;
  border-spacing: 0px;
}
#pdfPreview #page2 thead {
  background-color: #eee;
}
#pdfPreview #page2 thead th:first-child {
  padding-bottom: 2rem;
  text-align: left;
}
#page2 thead th:nth-child(2) {
  padding-bottom: 2rem;
  text-align: right;
}
#pdfPreview #page2 tbody .simple td {
  font-weight: bold;
}
#pdfPreview #page2 tbody .simple td p {
  font-weight: normal;
}
#pdfPreview #page2 tbody .header {
  width: 15%;
  padding: .3rem;
  background-color: #eee;
  font-weight: bold;
}
#pdfPreview #page2 tbody .subheader {
  width: 10%;
  padding: .3rem;
}
#pdfPreview #page2 tbody .subtableheader {
  text-align: center;
}
#pdfPreview #page2 tbody .fillout {
  padding: .3rem;
  text-align: center;
  color: #E6E6E6;
}
#pdfPreview #page2 tbody .filledout {
  padding: .3rem;
  text-align: center;
  color: black;
}
#page2 tbody .bottomrow {
  padding-bottom: 6rem;
  text-align: left;
}

#pdfPreview .border-top {
  border: 1px solid black;
}
#pdfPreview .border-top-left{
	border-left: 1px solid black;
	border-top: 1px solid black;
	border-bottom: 1px solid black;
}
#pdfPreview .border-middle-left {
  border-left: 1px solid black;
  border-right: 1px solid black;
  border-bottom: 1px solid black;
}
#pdfPreview .border-middle-fix{
	border-bottom: 1px solid black;
}
#pdfPreview .border-middle {
  border-right: 1px solid black;
  border-bottom: 1px solid black;
}
#pdfPreview .bottom-left {
  border-left: 1px solid black;
  border-bottom: 1px solid black;
}
#pdfPreview .bottom-right {
  border-right: 1px solid black;
  border-left: 1px solid black;
  border-bottom: 1px solid black;
}

#pdfPreview .footer1{
	margin-bottom:4em;
}

#pdfPreview td.topmargin{
	width:50%;
}

#pdfPreview .footerClass{
	width:90%;
	margin:0 auto;
	padding-bottom:4em;
}

#pdfPreview .mainBodyHeadArea{
	border-left:1px solid black;
	border-right:1px solid black;
	border-top:1px solid black;
}

#pdfPreview .submissionCellLeft{
	width:50%;
	padding:1em;
}

#pdfPreview .submissionCellRight{
	width: 50%;
	padding:1em;
	border-left: 1px solid black;
}

#pdfPreview td.addressCity {
    width: 40%;
}

#pdfPreview td.addressZip {
    width: 20%;
}

#pdfPreview td.addressState {
    width: 20%;
}

#pdfPreview td.addressCountry {
    width: 20%;
    border-left:none;
}

#pdfPreview table{
	border-spacing: 0;
}

#pdfPreview .addressRow{
	padding: 0;
}

#pdfPreview #vehicleListTable th, #pdfPreview #vehicleListTable td {
	padding:.5em;
	font-weight: bold;
}

#pdfPreview .filloutHeight{
	height:1em;
}

#pdfPreview .borderLeft{
	border-left:1px solid black;
}

#pdfPreview .fillAddressCity,.fillAddressState,.fillAddressCountry,.fillAddressZip{
	margin-left: 3em;
}

#pdfPreview .center{
	text-align: center;
}

#pdfPreview #vehicleListTable, #pdfPreview #table3 {
	width:90%;
	margin:0 auto;
}

#pdfPreview  td {
	padding:.5em;
}




#pdfPreview {
    width: 800px;
}

#pdfPreview p, td{
  font-size:12px;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?xml-stylesheet href="foo.css" media="all" type="text/css"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator"
content="HTML Tidy for Java (vers. 2009-12-01), see jtidy.sourceforge.net" />
<meta charset="UTF-8" />
<title>PDF HTML Title</title>
<link rel="Stylesheet" type="text/css" href="previewStylesPDF.css" />
</head>
<body>
<div id="pdfPreview">
<table id="page2">
<thead>
<tr>
<th colspan="2" class="border-top-left">Table Title</th>
<th colspan="6" class="border-top">Author Name</th>
</tr>
</thead>
<tbody>
<tr class="simple">
<td colspan="8" class="border-middle-left">Single or Multiple
Vehicle Registration for a Single Customer</td>
</tr>
<tr class="simple">
<td colspan="8" class="border-middle-left">
<p>Instructions for members only: Scan and upload this
completed and <b><ins style="font-weight: bold">signed</ins> form
to the mail-id <a
href="#">myName@Company.com</a></b></p>
</td>
</tr>
<tr>
<td class="header border-middle-left">Dealer</td>
<td class="subheader border-middle-fix">Dealer Name</td>
<td colspan="4" class="border-middle fillDealerName filledout">
Mercedez</td>
<td class="subheader border-middle">Phone</td>
<td colspan="2" class="border-middle fillDealerPhone filledout">
(###)###-####</td>
</tr>
<tr>
<td class="subheader border-middle-left">Name Dealer Code</td>
<td class="border-middle-fix fillDealerCode filledout">BorderLand</td>
<td class="subheader border-middle">Dealer Contact Name</td>
<td colspan="3" class="border-middle fillDealerContact filledout">
</td>
<td class="subheader border-middle">Email</td>
<td class="border-middle fillDealerEmail filledout"></td>
</tr>
</tbody>
</table>
<table id="vehicleListTable">
<thead>
<tr>
<th class="header border-middle-left">Serial Number</th>
<th colspan="4" class="subtableheader border-middle-fix">COL TITLE #1</th>
<th colspan="2" class="subtableheader border-middle borderLeft">COL TITLE #2</th>
<th colspan="2" class="subtableheader border-middle">COL TITLE #3</th>
</tr>
</thead>
<tbody>

<tr class="filloutHeight">
<td class="fillout border-middle-left filloutHeight"></td>
<td class="fillout border-middle-fix filloutHeight" colspan="4">
</td>
<td class="fillout border-middle filloutHeight" colspan="2"></td>
<td class="fillout border-middle filloutHeight" colspan="2"></td>
</tr>

<tr class="filloutHeight">
<td class="fillout border-middle-left filloutHeight"></td>
<td class="fillout border-middle-fix filloutHeight" colspan="4">
</td>
<td class="fillout border-middle filloutHeight" colspan="2"></td>
<td class="fillout border-middle filloutHeight" colspan="2"></td>
</tr>
</tbody>
</table>
<table id="table3">
<tbody>
<tr>
<td class="header border-middle-left">Customer</td>
<td class="subheader border-middle-fix">Name</td>
<td colspan="3" class="border-middle fillCustomerName filledout">
Atlantis City</td>
<td class="subheader border-middle">Phone</td>
<td colspan="2" class="fillout border-middle">Phone</td>
</tr>
<tr>
<td class="subheader border-middle-left" colspan="2">Authorized
Representative Name</td>
<td class="fillout border-middle" colspan="6">Name</td>
</tr>
<tr>
<td class="subheader border-middle-left">Email</td>
<td class="fillout border-middle" colspan="7">Email</td>
</tr>
<tr>
<td class="subheader border-middle-left">Address</td>
<td class="border-middle filladdress1 filledout" colspan="7">Street Address</td>
</tr>
<tr>
<td colspan="8" class="addressRow">
<table>
<tbody>
<tr>
<td class="subheader border-middle-left addressCity">City <span
class="fillAddressCity filledout">ATLANTIS</span></td>
<td class="subheader border-middle addressState">State/Province
<span class="fillAddressState filledout">EE</span></td>
<td class="subheader border-middle addressCountry">Country</td>
<td class="subheader border-middle addressZip">Zip/Postal Code
<span class="fillAddressZip filledout">99999</span></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td colspan="4" class="subheader border-middle-left">The above
vehicles will be used for the following vocation / applications
only:</td>
<td class="fillout border-middle" colspan="4">Vocation</td>
</tr>
<tr class="simple">
<td colspan="8" class="border-middle-left">
<p><b>Customer Certification:</b> All information to go here.</p>
</td>
</tr>
<tr>
<td colspan="4" class="subheader bottomrow bottom-left">Customer
Authorized customer representative signature (REQUIRED)</td>
<td colspan="4" class="subheader bottomrow bottom-right">Date</td>
</tr>
</tbody>
</table>
<div class="footerClass">
<div class="floatleft topmargin">
<p class="">PDF (Rev: 4/2/2014)</p>
</div>
<div class="floatright topmargin">
<p class="">Page 2 of 2</p>
</div>
</div>
</div>
</body>
</html>