简体   繁体   English

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

[英]Why does TCPDF ignore my inline CSS?

I am trying to create a PDF file using TCPDF. 我正在尝试使用TCPDF创建PDF文件。 But When I tried to insert inline CSS into the HTML, it's displaying a blank page! 但是当我尝试在HTML中插入内联CSS时,它显示的是空白页面! Here are a few lines of my 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 . 我在TCPDF示例39中插入代码。 But the output is only the TCPDF logo and the line "Example of HTML Justification". 但输出只是TCPDF徽标和“HTML Justification示例”行。 What's the reason? 什么原因? Or it does not support inline CSS? 或者它不支持内联CSS?

TCPDF has a very limited CSS support. TCPDF的CSS支持非常有限。 It doesn't support all attributes. 它不支持所有属性。

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, this is 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 处理大多数CSS 2.1和一些CSS3属性,包括@ import,@ media和@page规则
  • supports external stylesheets, either local or through http/ftp (via fopen-wrappers) 支持外部样式表,本地或通过http / ftp(通过fopen-wrappers)
  • supports complex tables, including row & column spans, separate & collapsed border models, individual cell styling 支持复杂表格,包括行和列跨度,单独和折叠边框模型,单个单元格样式

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 MPDF

Examples (both PHP and generated PDF) can be found here and here . 可在此处此处找到示例(PHP和生成的PDF)。

mPDF Documentation mPDF文档

Hope it helps :) 希望能帮助到你 :)

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上的示例输出

in my case server added slashes to double quotes - tcpdf ignored attributes like style without warning. 在我的情况下,服务器为双引号添加了斜杠 - tcpdf忽略了没有警告的样式等属性。 so I removed slashes by stripslashes function 所以我用stripslashes函数删除了斜杠

well there is a work around that I found : 我发现有一个解决方法:

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

hope this helps someone. 希望这有助于某人。

There is no PHP library which supports fully CSS. 没有完全支持CSS的PHP库。 TCPDF does quite nice job with relatively good performance even for larger strings. TCPDF做得相当不错,性能相对较好,即使对于较大的字符串也是如此。 Check out examples eg 006 , 007 , 21 or 061 . 检查出的例子,例如00600721061

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM