使用 TCPDF(PHP) 渲染 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8990216/
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
HTML Rendering with TCPDF(PHP)
提问by navjotk
I am using TCPDF's writeHtml function for a page that renders properly in the browser. In the output PDF, the fonts are too small. I've tried with setFont, but it doesn't seem to have an effect. Does anyone have experience with this?
我正在为在浏览器中正确呈现的页面使用 TCPDF 的 writeHtml 函数。在输出 PDF 中,字体太小。我试过 setFont,但它似乎没有效果。有任何人对此有经验吗?
I'd like to add here that the HTML is not always in my control, so I would prefer to do this with TCPDF options(and not by modifying the source html)
我想在这里补充一点,HTML 并不总是在我的控制之下,所以我更喜欢用 TCPDF 选项来做到这一点(而不是通过修改源 html)
UPDATE: I am able to change the font size by setting it on the body. The only remaining problem is that, to render correctly in the browser, it needs to be 12px. To render correctly in the PDF, it needs be something like 30px. Do I set the media on the css? What is the media type for TCPDF?
更新:我可以通过在正文上设置字体大小来更改字体大小。唯一剩下的问题是,要在浏览器中正确呈现,它需要是 12px。要在 PDF 中正确呈现,它需要类似于 30px。我是否在 css 上设置媒体?TCPDF 的媒体类型是什么?
回答by rdlowrey
Are you using tags? tcpdf's HTML engine gives the tag precedence over any CSS, or other size-adjusting tags. If you remove any extraneous tags from the HTML and use straight CSS, things should render as expected. Or, if you aren't using CSS, you should. Just because a browser displays it correctly doesn't mean it will look the same on other formats. The browser has likely performed some magic of its own to fill in the gaps in your CSS specifications.
你在使用标签吗?tcpdf 的 HTML 引擎赋予标签优先于任何 CSS或其他大小调整标签。如果您从 HTML 中删除任何无关的标签并使用直接的 CSS,则应该按预期呈现。或者,如果您不使用 CSS,则应该使用。仅仅因为浏览器正确显示它并不意味着它在其他格式上看起来相同。浏览器可能已经发挥了自己的一些魔力来填补 CSS 规范中的空白。
UPDATE
更新
Here's an example of specifying CSS declarations with your HTML when using tcpdf. Note how all the styling is applied using the CSS declarations inside the <style>
tag outside the actualy HTML body.
这是使用 tcpdf 时使用 HTML 指定 CSS 声明的示例。请注意如何使用实际<style>
HTML 正文之外的标记内的 CSS 声明来应用所有样式。
<?php
$html = <<<EOF
<!-- EXAMPLE OF CSS STYLE -->
<style>
h1 {
color: navy;
font-family: times;
font-size: 24pt;
text-decoration: underline;
}
p {
color: red;
font-family: helvetica;
font-size: 12pt;
}
</style>
<body>
<h1>Example of <i>HTML + CSS</i></h1>
<p>Example of 12pt styled paragraph.</p>
</body>
EOF;
$pdf->writeHTML($html, true, false, true, false, '');
?>
回答by Andrius V.
The best solution that worked for me was to replace 'px' to 'pt' in html code:
对我有用的最佳解决方案是将 html 代码中的“px”替换为“pt”:
$tidy = str_replace ('px', 'pt', $tidy);
Before on the left side and after replacing on the right:
左侧之前和右侧更换后:
回答by Jeremy Harris
TCPDF recognizes basic CSS such as font-size, font-color, and font-family.
TCPDF 识别基本的 CSS,例如 font-size、font-color 和 font-family。
For a little more information, check out TCPDF not render all CSS properties
有关更多信息,请查看TCPDF 不呈现所有 CSS 属性