【问题标题】:Itext 7 HtmlConverter can not wrap words that do not contain white space charactersItext 7 HtmlConverter 不能换行不包含空格字符的单词
【发布时间】:2018-07-09 09:55:57
【问题描述】:

我正在使用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" 类中不起作用。

有没有什么办法可以把不包含空格(只有字符)的单词换行?

【问题讨论】:

    标签: itext7


    【解决方案1】:

    有。

    你需要看看界面

    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 的类,并添加自己的额外拆分字符(如字母数字字符)。

    为了设置它,您可以在 RootElement 上使用setPropertyDocument 继承自它)。属性名称为Property.SPLIT_CHARACTERS

    由于您从未在原始代码中创建布局 Document,因此需要进行一些修改。

    您可以使用以下代码渲染到List&lt;IElement&gt;

    List<IElement> elements = HtmlConverter.convertToElements(stream, converterProperties);
    

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

    【讨论】:

    • 文档 document = new Document(pdfDoc); document.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters() { @Override public boolean isSplitCharacter(GlyphLine text, int glyphPos) { return true; } });这个有效
    猜你喜欢
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多