2
我已经搜索了这些问题,但无法找到解决具体问题的解决方案。我需要做的是将包含图像和CSS样式的HTML文件转换为PDF。我使用的是iText 5,并且能够将样式包含到生成的PDF中。但是,我仍然在努力包括图像。我在下面列出了我的代码。具有绝对路径的图像包含在生成的PDF中,具有相对路径的图像不是。我知道我需要实现AbstractImageProvider,但我不知道该怎么做。任何帮助是极大的赞赏。使用iText将HTML和图像转换为PDF
Java文件:
public class Converter {
static String in = "C:/Users/APPS/Desktop/Test_Html/index.htm";
static String out = "C:/Users/APPS/Desktop/index.pdf";
static String css = "C:/Users/APPS/Desktop/Test_Html/style.css";
public static void main(String[] args) {
try {
convertHtmlToPdf();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void convertHtmlToPdf() throws DocumentException, IOException {
Document document = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(out));
document.open();
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, new FileInputStream(in), new FileInputStream(css));
document.close();
System.out.println("PDF Created!");
}
/**
* Not sure how to implement this
* @author APPS
*
*/
public class myImageProvider extends AbstractImageProvider {
@Override
public String getImageRootPath() {
// TODO Auto-generated method stub
return null;
}
}
}
HTML文件:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML to PDF</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>HTML to PDF</h1>
<p>
<span class="itext">itext</span> 5.4.2
<span class="description"> converting HTML to PDF</span>
</p>
<table>
<tr>
<th class="label">Title</th>
<td>iText - Java HTML to PDF</td>
</tr>
<tr>
<th>URL</th>
<td>http://wwww.someurl.com</td>
</tr>
</table>
<div class="center">
<h2>Here is an image</h2>
<div>
<img src="images/Vader_TFU.jpg" />
</div>
<div>
<img src="https://www.w3schools.com/images/picture.jpg" alt="Mountain" />
</div>
</div>
</body>
</html>
CSS文件:
h1 {
color: #ccc;
}
table tr td {
text-align: center;
border: 1px solid gray;
padding: 4px;
}
table tr th {
background-color: #84C7FD;
color: #fff;
width: 100px;
}
.itext {
color: #84C7FD;
font-weight: bold;
}
.description {
color: gray;
}
.center {
text-align: center;
}
这工作完美!非常感谢。 – jdubicki
@jdubicki很高兴我能够提供帮助。不要忘记,如果一个答案可以帮助你,你可以upvote它,然后接受它。谢谢 – MaVRoSCy
我遇到了另一个需要帮助的问题。为了能够读取和呈现嵌套的无序列表,我不得不做一些修改。我注意到我的标题标签没有在PDF中解析,任何人都可以帮我解决这个问题。下面是我需要做的chages。 – jdubicki