1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > html2pdf页面大小 带有页面大小检测的itext 7 html到pdf(itext 7 html to pdf with page size detection)...

html2pdf页面大小 带有页面大小检测的itext 7 html到pdf(itext 7 html to pdf with page size detection)...

时间:2019-03-31 14:32:17

相关推荐

html2pdf页面大小 带有页面大小检测的itext 7 html到pdf(itext 7 html to pdf with page size detection)...

带有页面大小检测的itext 7 html到pdf(itext 7 html to pdf with page size detection)

我使用itext 7将HTML模板(Velocity)呈现为PDF,然后呈现为PNG(Apache PdfBox),用于热敏打印机打印。

我的模板呈现为:

...

我的容器的固定宽度为512px。 我想测量HTML的渲染高度,然后将其渲染为页面高度为测量高度的单页PDF。

但渲染的大小不是预期的大小。 我期望有一个512px的渲染宽度,但layoutResult返回宽度385(而不是预期的512)。

我错过了哪个参数? 谢谢。

I am using itext 7 to render HTML templates (Velocity) to PDF, then to PNG (Apache PdfBox), for thermal printer printing.

My template is rendered as :

...

My container has a fixed width to 512px. 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.

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).

Which parameter have I missed ? Thanks.

原文:/questions/50616145

更新时间:-03-04 14:03

最满意答案

1英寸= 96像素

1英寸= 72个用户单位= 72磅

看看你拥有的数字,看起来好像你在混淆像素和点。

96 px = 72 pt

512 px = 512 / 96 * 72 pt

512 px = 384 pt

我想你会期望512像素,并获得384点,但这不是一个错误,因为512像素等于384点。 因此没有问题。 你的问题的主要问题是你忘了提到测量系统。

1 inch = 96 px

1 inch = 72 user units = 72 pt

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. Hence there is no problem. The main problem with your question is that you forget to mention the measurement system.

相关问答

可以使用 /itext/com/itextpdf/text/pdf/PdfReader.html#getPageSize(int ) /itext/com/itextpdf/text/pdf/PdfReader.html#getPageSizeWithRotation(int ) 比较你从中得到的矩形的宽度和高度,你知道你使用的纸张大小。 Either use

...

您可以尝试使用-fs-pdf-font-embed和-fs-pdf-font-encoding css规则。 从用户指南 : -fs-pdf-font-embed:与字体规则中嵌入的值一起使用,以使Flying Saucer在PDF文档中嵌入字体文件,避免需要调用FontResolver类的addFont()方法 -fs-pdf-font-encoding:在一个font-face规则中使用指定你嵌入到PDF中的自定义字体的引用; 将编码的名称作为值。 例如在您的打印CSS: @font-face

...

pdfHTML v1.0.1中添加了对page-break-after CSS属性的支持 没有/ alt标签是v1.0.0中的一个错误,并在v1.0.1中修复 (为了可见性,将其作为答案发布,因为此问题之前已经出现过)。 Support for the page-break-after CSS property has been added in pdfHTML v1.0.1 The absence of the /alt tags was a bug in v1.0.0 and is fixed

...

您正在创建PdfDocument实例而不是Document实例! 如文档所述, PdfDocument类仅供内部使用。 You're creating a PdfDocument instance instead of a Document instance! As documented, the PdfDocument class is for internal use only.

如果您知道如何在此问题中解释的方面添加额外空间: 如何扩展PDF的页面大小以添加水印? 那么你应该知道如何在底部添加额外的空间。 你的问题是重复的。 PDF文档的页面大小是使用/MediaBox定义的。 它可以使用/CropBox进行裁剪。 在我给出的答案中,我们像这样更改/MediaBox : PdfArray mediabox = pageDict.getAsArray(PdfName.MEDIABOX);

llx = mediabox.getAsNumber(0).floatValue();

...

请查看常见问题解答条目HTML中的测量系统如何与PDF中的测量系统相关联? 你会发现: 1英寸= 96像素 1英寸= 72个用户单位= 72磅 看看你拥有的数字,看起来好像你在混淆像素和点。 96 px = 72 pt

512 px = 512 / 96 * 72 pt

512 px = 384 pt

我想你会期望512像素,并获得384点,但这不是一个错误,因为512像素等于384点。 因此没有问题。 你的问题的主要问题是你忘了提到测量系统。 Please take a look at the

...

为什么你的代码不起作用 正如HTML to PDF教程的介绍中所解释的那样, HTMLWorker在很多年前就被弃用了。 它无意转换完整的HTML页面。 它不知道HTML页面有

和部分; 它只是解析所有内容。 它旨在解析小的HTML片段,您可以使用StyleSheet类定义样式; 真正的CSS不受支持。 然后是XML Worker。 XML Worker是一种解析XML的通用框架。 作为概念证明,我们决定将一些XHTML编写为PDF功能,但我们不支持所有HTML标记。 例如:

...

首先,你需要获得iText 7(核心库)和pdfHtml附加组件(将解析HTML + CSS并将其转换为iText对象的部分)。 转到github以了解如何下载这些内容。 假设你有这个HTML : 有了这个相应的CSS : 然后你可以使用这段代码: ConverterProperties converterProperties =

new ConverterProperties().setBaseUri(resoureLocation);

HtmlConverter.convertToPd

...

以下是此问题的解决方案: $html2pdf = new HTML2PDF('P', array($width_in_mm,$height_in_mm), 'en', true, 'UTF-8', array(0, 0, 0, 0));

宽度和高度应为MM。 如果您使用英寸将其转换为MM。 式: $width_in_mm = $width_in_inches * 25.4;

$height_in_mm = $height_in_inches * 25.4;

不要四舍五入。 即使它有一个小数点

...

对于任何想要知道的人来说没关系.....我拒绝在我的配置文件中设置的所有用户导致问题。 调整以允许管理员解决此问题。 Ok for anyone that would like to know..... I had deny all users set in my config file which was causing the problem. Adjusting to allow admins resolved this.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。