php 为什么 TCPDF 忽略我的内联 CSS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11395171/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:26:11  来源:igfitidea点击:

Why does TCPDF ignore my inline CSS?

phpcsstcpdf

提问by AssamGuy

I am trying to create a PDF file using TCPDF. But When I tried to insert inline CSS into the HTML, it's displaying a blank page! Here are a few lines of my HTML:

我正在尝试使用 TCPDF 创建 PDF 文件。但是当我尝试将内联 CSS 插入 HTML 时,它显示了一个空白页面!这是我的 HTML 的几行:

pdf->AddPage();

// set font
$pdf->SetFont('helvetica', 'B', 20);

$pdf->Write(0, 'Example of HTML Justification', '', 0, 'L', true, 0, false, false, 0);

// create some HTML content
$html = '
<div id="wrapper" style="width:900px;height:auto;margin:0 auto;">
<div id="header" style="width:900px;height:180px;border-bottom:2px solid #5a5a5a;">
<div id="img" style="width:174px;height:87px;position:relative;top:10px;left:350px;"><img src="Untitled Document_files/logoticket.png" /></div>
<div id="text" style="width:250px;position:relative;left:315px;top:20px;font-family:Arial, Helvetica, sans-serif;font-weight:bold;font-size:14px;">E-TICKET/RESERVATION VOUCHER</div>
<div id="text2" style="width:200px;position:relative;left:345px;top:40px;font-family:Arial, Helvetica, sans-serif;
font-weight:bold;font-size:12px;">Visit - www.bookings.7sisters.in</div>
</div>
<div id="mainContent">
<div id="icon" style="width:600px;
height:37px;
position:relative;
left:190px;
top:20px;">
<img src="Untitled D.....

etc.

等等。

I am inserting the code in TCPDF example 39. But the output is only the TCPDF logo and the line "Example of HTML Justification". What's the reason? Or it does not support inline CSS?

我在TCPDF 示例 39 中插入代码。但输出只是 TCPDF 徽标和“HTML 理由示例”行。什么原因?或者它不支持内联 CSS?

回答by abhshkdz

TCPDF has a very limited CSS support. It doesn't support all attributes.

TCPDF 对 CSS 的支持非常有限。它不支持所有属性。

Currently, only the following CSS attributes are supported:

目前,仅支持以下 CSS 属性:

  • font-family
  • font-size
  • font-weight
  • font-style
  • color
  • background-color
  • text-decoration
  • width
  • height
  • text-align
  • 字体系列
  • 字体大小
  • 字体粗细
  • 字体样式
  • 颜色
  • 背景颜色
  • 文字装饰
  • 宽度
  • 高度
  • 文本对齐

So try removing the other attributes, and tell if it works.

所以尝试删除其他属性,并判断它是否有效。

Also, thisis an active forum for TCPDF related discussion.

此外,是一个进行 TCPDF 相关讨论的活跃论坛。

TCPDF Documentation

TCPDF 文档

Alternatives to TCPDF that are definitely worth trying:

绝对值得尝试的 TCPDF 替代品:

domPDF

domPDF

Key Features

主要特点

  • handles most CSS 2.1 and a few CSS3 properties, including @import, @media & @page rules
  • supports external stylesheets, either local or through http/ftp (via fopen-wrappers)
  • supports complex tables, including row & column spans, separate & collapsed border models, individual cell styling
  • 处理大多数 CSS 2.1 和一些 CSS3 属性,包括 @import、@media 和 @page 规则
  • 支持外部样式表,本地或通过 http/ftp(通过 fopen-wrappers)
  • 支持复杂的表格,包括行和列跨度、分离和折叠边框模型、单个单元格样式

domPDF Documentation

domPDF 文档

Helpful tutorial: http://kyokasuigetsu25.wordpress.com/2011/07/14/how-to-use-dompdf-with-css/

有用的教程:http: //kyokasuigetsu25.wordpress.com/2011/07/14/how-to-use-dompdf-with-css/

mPDF

pdf

Examples (both PHP and generated PDF) can be found hereand here.

可以在此处此处找到示例(PHP 和生成的 PDF)。

mPDF Documentation

mPDF 文档

Hope it helps :)

希望能帮助到你 :)

回答by Michal - wereda-net

in my case server added slashes to double quotes - tcpdf ignored attributes like style without warning. so I removed slashes by stripslashes function

在我的情况下,服务器在双引号中添加了斜杠 - tcpdf 在没有警告的情况下忽略了样式等属性。所以我通过stripslashes函数删除了斜线

回答by Dray

well there is a work around that I found :

好吧,我发现了一个解决方法:

<h1>Heading<small>Text(is smaller than heading.)</small></h1>

hope this helps someone.

希望这有助于某人。

回答by lubosdz

There is no PHP library which supports fully CSS. TCPDF does quite nice job with relatively good performance even for larger strings. Check out examples e.g. 006, 007, 21or 061.

没有完全支持 CSS 的 PHP 库。即使对于较大的字符串,TCPDF 也能以相对较好的性能表现出色。检查出的例子,例如00600721061

回答by metalfight - user868766

I used the code you provided, and changed the color to red for 'Visit - www.bookings.7sisters.in' using inline CSS.

我使用了您提供的代码,并使用内联 CSS 将“访问 - www.bookings.7sisters.in”的颜色更改为红色。

See my sample output at https://www.box.com/s/520e9e1f4976802696ec

https://www.box.com/s/520e9e1f4976802696ec查看我的示例输出