php 按比例缩放 HTML 以完全适合 PDF A4 大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12856880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scale HTML proportionally to fit exactly to PDF A4 size
提问by PHPDev
I am using PHP, Mysql, jQuery. I have a webpage that is to be converted into high-res A4 size PDF: http://optisolbusiness.com/funeral_site/sample/index/id/255.
我正在使用 PHP、Mysql、jQuery。我有一个要转换为高分辨率 A4 尺寸 PDF 的网页:http: //optisolbusiness.com/funeral_site/sample/index/id/255。
I have converted the HTML to PDF using wkhtmltopdf, which works great.
我已使用 将 HTML 转换为 PDF wkhtmltopdf,效果很好。
Here is the generated PDF http://optisolbusiness.com/Guru/Gurupdf/optisol.pdf. But the HTML is not fitting exactly to PDF size; There are spaces around the HTML in PDF. How to scale HTML to fit A4 PDF size 100%? Importantly the content inside the html (ie) text size, images width and height, background images also to be scaled proportionally.
这是生成的 PDF http://optisolbusiness.com/Guru/Gurupdf/optisol.pdf。但是 HTML 并不完全适合 PDF 大小;PDF 中的 HTML 周围有空格。如何缩放 HTML 以适应 A4 PDF 大小 100%?重要的是html里面的内容(即)文字大小、图片宽高、背景图片也要按比例缩放。
采纳答案by dualed
Your background image is not exactly high-res, this won't look great in print.
您的背景图像不完全是高分辨率,这在打印时看起来不太好。
I don't know wkhtmltopdf itself, but your body already has absolute dimensions set (in inches). This is probably the problem. Your body has a max size, the content has an absolute size too (given due to the background image pixel dimensions).
我不知道 wkhtmltopdf 本身,但是您的身体已经设置了绝对尺寸(以英寸为单位)。这大概就是问题所在。你的身体有一个最大尺寸,内容也有一个绝对尺寸(由于背景图像像素尺寸)。
This is not a good starting point for html-to-print transformaions, and PDF is essentially print.
这不是 html 到打印转换的良好起点,PDF 本质上是打印。
what to do (intermediate)
做什么(中级)
- remove any size restrictions from body
- wkhtml... has a switch called
zoom,1.5should be an appropriate value to fill the page - use page-size a4
- 从正文中删除任何尺寸限制
- wkhtml... 有一个名为 的开关
zoom,1.5应该是一个合适的值来填充页面 - 使用页面大小 a4
what to do (the "right" way)
做什么(“正确”的方式)
- remove size restrictions from body
- build the background borders (the black ones) with html elements and css styling
- refrain from defining "width" rules for those. You will only have to define a "width" once, all other widths should be set to "auto".
- heights will prove troublesome, because divs are only as high as their content requires. But setting height: 100% does not respect border and margin sizes.
- that yellow cross could be designed in css too, or a much higher resolution png/jpeg
- Use only "real" dimensions. That means do notuse pixels, use points, inches, or mm. You can use % values, but make sure those are % values of real dimensions (that means that at some point a parent element has a real dimension)
- 从正文中删除尺寸限制
- 使用 html 元素和 css 样式构建背景边框(黑色边框)
- 不要为那些定义“宽度”规则。您只需定义一次“宽度”,所有其他宽度应设置为“自动”。
- heights 会很麻烦,因为 div 的高度取决于它们的内容要求。但是设置 height: 100% 不考虑边框和边距大小。
- 那个黄色十字也可以用 css 设计,或者更高分辨率的 png/jpeg
- 仅使用“真实”尺寸。这意味着你不能使用的像素,使用点,英寸或毫米。您可以使用 % 值,但要确保它们是实际尺寸的 % 值(这意味着在某些时候父元素具有实际尺寸)
回答by Rob Forrest
I'd say that you're always going to struggle to get this to be perfect. In my opinion you're better off writing the PDF directly rather than relying on a third party tool.
我想说的是,您总是会努力使其完美。在我看来,您最好直接编写 PDF 而不是依赖第三方工具。
Consider looking into FPDF, an open-source PHP PDF writing library. Be warned, the website looks out of date, but the functionality works beautifully.
考虑研究FPDF,一个开源的 PHP PDF 编写库。请注意,该网站看起来已经过时,但功能运行良好。
回答by Gung Foo
You can set the size of the body to the size of the page.. in case of A4 that is 210x297mm.
您可以将正文的大小设置为页面的大小。如果 A4 为 210x297 毫米。
Btw: you should only be using width in percentage, otherwise wkhtmltopdf will have to try and convert it.
顺便说一句:你应该只使用百分比宽度,否则 wkhtmltopdf 将不得不尝试转换它。
So make sure you use width: 100%;if you want it to fill all the room. ;)
因此,width: 100%;如果您希望它填满整个房间,请确保使用。;)
BTW: If you want real high quality PDFs you will need to create them conforming to at least the PDF/X-3 standard. I don't think wkhtmltopdfdoes that tho.
顺便说一句:如果您想要真正的高质量 PDF,您将需要创建它们至少符合PDF/X-3 标准。我不认为wkhtmltopdf这样做。

