使用TCPDF(PHP)进行HTML渲染 [英] HTML Rendering with TCPDF(PHP)

查看:1930
本文介绍了使用TCPDF(PHP)进行HTML渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将TCPDF的writeHtml函数用于在浏览器中正确呈现的页面. 在输出的PDF中,字体太小.我已经尝试过使用setFont,但似乎没有效果.有人有经验吗?

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?

我想在这里添加HTML并不总是在我的控件中,所以我宁愿使用TCPDF选项(而不是通过修改源html来做到这一点)

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)

更新:我可以通过将其设置在正文上来更改字体大小.唯一剩下的问题是,要在浏览器中正确呈现,它必须为12px.要在PDF中正确呈现,它的大小必须为30px.我是否将媒体放在CSS上? TCPDF的媒体类型是什么?

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?

推荐答案

您是否正在使用标签? tcpdf的HTML引擎提供了标签优先于任何CSS 或其他尺寸调整标签的优先级.如果从HTML删除任何多余的标签并使用纯CSS,则应按预期呈现内容.或者,如果您不使用CSS,则应该使用.仅仅因为浏览器正确显示它并不意味着它将在其他格式上看起来相同.浏览器可能已经发挥了自己的魔力来填补CSS规范中的空白.

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主体外部的<style>标记内的CSS声明应用所有样式.

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.

<?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, '');

?>

这篇关于使用TCPDF(PHP)进行HTML渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆