所以我想要实现的是将HTML转换为带图像的PDF格式。
我正在使用iText
并且有我的代码:
public void downloadTemplateAsPdf() {
try {
InputStream content = null;
// Object Id for Html stream
String objID = this.actData.getMainActs().get(0).getId();
ContentStream cmisStream = folderCatalogue.getDocumentContentStream(objID);
content = cmisStream.getStream();
File fileTmp = File.createTempFile("tempFile", "pdf");
OutputStream fileStream = new FileOutputStream(fileTmp);
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, fileStream);
document.open();
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
worker.parseXHtml(writer, document, content, Charset.forName("UTF-8"));
document.close();
fileStream.close();
//encode title for UTF-8 letters
String title = URLEncoder.encode(templateTitle, "UTF-8");
StringBuilder fileName = new StringBuilder(title);
if (title.contains("+")) {
for (int i = 0; i < title.length(); i++) {
if (title.charAt(i) == '+') {
fileName.setCharAt(i, ' ');
}
}
}
// JSF primefaces <p:fileDowbload> to download file
streamedContent = new DefaultStreamedContent(new FileInputStream(fileTmp), cmisStream.getMimeType(),
fileName + ".pdf");
我已查看http://itextpdf.com/examples/xml-worker/html-images
和iText HTML to PDF for non-english content images are very small in PDF但我检查的所有内容都有所不同。
任何帮助将不胜感激。 如果我应该提供更多信息,请告诉我们。
编辑:我的HTML示例。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><style>body{ font-family:Arial,Verdana,sans-serif; }</style><head><title></title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><p><img src="image/9efc807d-352a-410d-9bf9-17ada2772bac.png"/></p>
</body></html>
如果我将src url粘贴到浏览器,它将打开我的图像文件