我正在使用iText html2PDF
2.0.0 版本将 HTML 转换为 PDF。
在我的 HTML 文件中,有太多单词无法包含在表格列中。
这可以在图书馆完成iText 5
。这是我的 HTML 和 java 文件。
<html>
<head>
<title>TM_Report</title>
<style type="text/css">
.div-half-width {
display: inline;
width: 50%;
color: red;
}
</style>
</head>
<body>
<table width="70%" style="">
<tbody>
<tr>
<td width="50%"><p style="width: 80%; background-color: fuchsia;">3aaaa22aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaa1</p></td>
<td width="50%"><p style="width: 80%; background-color: cyan;">4aaaaaa33aaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbaaaaaaaaaaaaaaaaa2</p></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
<p style="word-break: break-all; width: 80%; background-color: fuchsia;">aaaa22aaaaaa-aaaaaaaaaabb-bbbbbbbbbbbbb-bbbbbbbbaaaa-aaaaaaaaaaaaa-aaaaa1aaaa22-aaaaaaaaaaaa-aaaabbbbbbbb-bbbbbbbb_bbbbbbbaa_aaaaaaaaaaa_aaaaaaaaa1aaaa22_aaaaaaaaaaa_aaaaabbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaa_aaaaaaaaaaaaa1</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
创建AccessiblePDF.java
public class Test {
public static final String sourceFolder = "res/pdfHTML/AccessiblePDF/";
public static final String destinationFolder = "target/output/pdfHTML/AccessiblePDF/";
public static final String[] files = { "Accessibility" };
public static void main(String[] args) throws IOException, InterruptedException {
for (String name : files) {
String htmlSource = sourceFolder + name + ".html";
String resourceFolder = sourceFolder;
String pdfDest = destinationFolder + name + ".pdf";
File file = new File(pdfDest);
file.getParentFile().mkdirs();
new CreateAccessiblePDF().createPdf(htmlSource, pdfDest, resourceFolder);
}
}
public void createPdf(String src, String dest, String resources) throws IOException {
try {
FileOutputStream outputStream = new FileOutputStream(dest);
WriterProperties writerProperties = new WriterProperties();
writerProperties.addXmpMetadata();
PdfWriter pdfWriter = new PdfWriter(outputStream, writerProperties);
PdfDocument pdfDoc = new PdfDocument(pdfWriter);
ConverterProperties props = new ConverterProperties();
HtmlConverter.convertToPdf(new FileInputStream(src), pdfDoc, props);
pdfDoc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我什至尝试过样式表“table-layout:fixed;” 在表中和“断字:全部打断;” 在列中,但尽管它在 Itext 7“html2pdf.HtmlConverter”类中不起作用。
有什么方法可以将不包含空格(仅字符)的单词换行吗?
有。
你需要看一下界面
public interface ISplitCharacters {
/**
* Returns <CODE>true</CODE> if the character can split a line. The splitting implementation
* is free to look ahead or look behind characters to make a decision.
* @param glyphPos the position of {@link Glyph} in the {@link GlyphLine}
* @param text an array of unicode char codes which represent current text
*/
boolean isSplitCharacter(GlyphLine text, int glyphPos);
}
Run Code Online (Sandbox Code Playgroud)
默认实现(由 Document 使用)被DefaultSplitCharacters
设置为在各种空白变体处进行分割。
最好的解决方案是编写一个继承DefaultSplitCharacters
并添加自己的额外分割字符(如字母数字字符)的类。
为了设置它,您可以使用setProperty
RootElement(从中Document
继承)。属性名称是Property.SPLIT_CHARACTERS
由于您从未在原始代码中创建布局Document
,因此需要进行一些修改。
List<IElement>
您可以使用以下代码进行渲染:
List<IElement> elements = HtmlConverter.convertToElements(stream, converterProperties);
Run Code Online (Sandbox Code Playgroud)
然后,您可以将元素添加到 a 中Document
,该元素是通过正确实现预先设置的ISplitCharacters
归档时间: |
|
查看次数: |
2879 次 |
最近记录: |