gpt4 book ai didi

itext7 - Itext 7 HtmlConverter 不能换行不包含空格字符的单词

转载 作者:行者123 更新时间:2023-12-03 23:17:11 27 4
gpt4 key购买 nike

我正在使用 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>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</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>


CreateAccessiblePDF.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();
}
}
}

我什至尝试过样式表“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);

}

默认实现(由 Document 使用)是 DefaultSplitCharacters它被设置为在空白的各种变体处拆分。

最好的办法是写一个继承自 DefaultSplitCharacters的类。并添加自己的额外拆分字符(如字母数字字符)。

为了设置它,您可以使用 setProperty在 RootElement (从 Document 继承)。属性名称是 Property.SPLIT_CHARACTERS
因为您从不创建布局 Document在您的原始代码中,这将需要进行一些修补。

你可以渲染到 List<IElement>使用以下代码:
List<IElement> elements = HtmlConverter.convertToElements(stream, converterProperties);

然后您可以将元素添加到 Document ,这是预先设置的 ISplitCharacters 的正确实现

关于itext7 - Itext 7 HtmlConverter 不能换行不包含空格字符的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48522696/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com