简体   繁体   English

itext 7 html至pdf并具有页面大小检测

[英]itext 7 html to pdf with page size detection

I am using itext 7 to render HTML templates (Velocity) to PDF, then to PNG (Apache PdfBox), for thermal printer printing. 我正在使用itext 7将HTML模板(Velocity)呈现为PDF,然后呈现为PNG(Apache PdfBox),以进行热敏打印机打印。

My template is rendered as : 我的模板呈现为:

<html>
<head>
    <style>...</style>
</head>
<body>
    <div id="container">...</div>
</body>
</html>

My container has a fixed width to 512px. 我的容器的宽度固定为512像素。 I would like to measure the rendered height of the html, and then render it as a single page PDF with page height = measured height. 我想测量html的呈现高度,然后将其呈现为页面高度=测量高度的单页PDF。

But the rendered size is not the expected size. 但是渲染的大小不是预期的大小。 I expect to have a rendered width of 512px, but the layoutResult returns a width of 385 (instead of the expected 512). 我期望呈现的宽度为512px,但是layoutResult返回的宽度为385(而不是预期的512)。

Which parameter have I missed ? 我错过了哪个参数? Thanks. 谢谢。

Please take a look at the FAQ entry How do the measurement systems in HTML relate to the measurement system in PDF? 请查看常见问题解答条目。HTML中的测量系统与PDF中的测量系统有什么关系? You'll discover that: 您会发现:

1 inch = 96 px 1英寸= 96像素

1 inch = 72 user units = 72 pt 1英寸= 72个用户单位= 72磅

Looking at the numbers you have, it looks as if you are confusing pixels and points. 查看您拥有的数字,好像您在混淆像素和点。

96 px = 72 pt
512 px = 512 / 96 * 72 pt
512 px = 384 pt

I guess you expect 512 px and get 384 pt, but that's not an error, because 512 px equals 384 pt. 我猜您期望512像素并获得384 pt,但这并不是一个错误,因为512 px等于384 pt。 Hence there is no problem. 因此没有问题。 The main problem with your question is that you forget to mention the measurement system. 问题的主要问题是您忘记提及测量系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM