英文:
iText 7 converting HTML to PDF - How to see entire wide table?
问题
InputStream is = this.getClass().getResourceAsStream("/very-wide-table_01.html");
ByteArrayOutputStream os = new ByteArrayOutputStream();
HtmlConverter.convertToPdf(is, os);
当输入的HTML中有一个宽表格时,会看到以下消息(表格被截断,数据丢失):
> 警告 TableWidths 由于单元格的最小宽度,表格宽度超出预期。
我如何告诉iText要么缩放内容以适应PDF页面,要么缩放页面以适应内容?
我确信可以使用PDF完成这个操作。Aspose.PDF库默认情况下会这样做。但我需要一种使用iText来实现的方法。
(编辑)它适用于PDF页面宽度过宽的任何表格...
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/XHTML">
<head><title>非常宽的表格</title><meta charset="UTF-8"/>
<style>
table {border-collapse: collapse;}
table, td, th {border: 1px solid red;}
th {text-align: center; text-weight: bold;}
</style></head>
<body>
<table>
<tr><td colspan="30">非常宽的表格</td></tr>
<tr>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td>
</tr>
</table>
</body>
</html>
英文:
InputStream is = this.getClass().getResourceAsStream("/very-wide-table_01.html");
ByteArrayOutputStream os = new ByteArrayOutputStream();
HtmlConverter.convertToPdf(is, os);
When input HTML has a wide table, this message is seen (and the table is truncated, data is lost):
> WARN TableWidths Table width is more than expected due to min width of cell(s).
How can I tell iText to either scale the content to fit the PDF page, OR scale the page to fit the content?
I'm sure this can be done with PDF. The Aspose.PDF library does it by default. But I need a way to do it with iText.
(edit) It's any table too wide for the PDF page...
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/XHTML">
<head><title>very wide table</title><meta charset="UTF-8"/>
<style>
table {border-collapse: collapse;}
table, td, th {border: 1px solid red;}
th {text-align: center; text-weight: bold;}
</style></head>
<body>
<table>
<tr><td colspan="30">Very wide table</td></tr>
<tr>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td><td>1234567890</td><td>1234567890</td>
<td>1234567890</td><td>1234567890</td>
</tr>
</table>
</body>
</html>
答案1
得分: 1
这个回答了如何改变页面大小以适应非常大的表格的基本问题。这个回答不提供确定所需宽度的任何手段,但至少这段代码确实改变了默认的页面大小。
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfDocument doc = new PdfDocument(new PdfWriter(baos));
doc.setDefaultPageSize(new PageSize(2000, 200));
ConverterProperties props = new ConverterProperties();
HtmlConverter.convertToPdf(this.getClass().getResourceAsStream("/very-wide-table_01.html"), doc, props);
英文:
This answers the basic question of how to change page size to account for very large tables. This answer does not provide any means of determining the required width, but at least this code does change the default page size.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfDocument doc = new PdfDocument(new PdfWriter(baos));
doc.setDefaultPageSize(new PageSize(2000, 200));
ConverterProperties props = new ConverterProperties();
HtmlConverter.convertToPdf(this.getClass().getResourceAsStream("/very-wide-table_01.html"), doc, props);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论