HTML2pdf Css不支持

时间:2017-07-07 04:03:23

标签: php css pdf

我使用html2pdf文件生成pdf。但它根本不支持css。请告诉我这是什么问题。

1)从中下载文件 https://sourceforge.net/projects/html2fpdf/?source=typ_redirect

2)放置在一个文件夹中并在代码下方使用以生成pdf(正常工作)

require("html2fpdf.php");  
$buffer = "<div>
<h1>Hello Friends</h1><br>
<p>This is a sample code</p>
</div>";
$pdf = new HTML2FPDF('P', 'mm', 'Legal'); 
$pdf->AddPage(); 
$pdf->WriteHTML($buffer); 
$pdf->Output('my.pdf', 'D');

但是当我在这里使用css时,生成的pdf没有打开

require("html2fpdf.php");  
$buffer = "<div style='width:950px;'>
<h1>Hello Friends</h1><br>
<p style='font-size:10'>This is a sample code</p>
</div>";
$pdf = new HTML2FPDF('P', 'mm', 'Legal'); 
$pdf->AddPage(); 
$pdf->WriteHTML($buffer); 
$pdf->Output('my.pdf', 'D');

我的php版本是PHP Version 5.3.8

我很鞠躬使用HTML2PDF。所以如果你知道的话请回答(我已经和TCPDF一样使用了,但是我需要在HTML2PDF中实现它)

2 个答案:

答案 0 :(得分:3)

我做了两次测试,除了div的大小之外,CSS运行良好。

PHP版本:5.6.30

测试1

<?php
require("html2fpdf.php");
$buffer = "
<h1>Hello Friends</h1><br>
<span style='font-weight: bold; font-size: 18pt; color: #FF0000; font-family: Times'>Hello there! I am red!<br></span>
<br>
<p style='font-size:10'>This is a sample code</p>
<p style='font-size:10px; color:blue;'>This is a sample code</p>
<span style='font-size:10px; color:blue;'>This is a sample code</p>
<span style='font-weight: italic; font-size: 10pt; color: #00FF00;'>I am a pdf ! Please accept my answer!</span>
";
$pdf = new HTML2FPDF('P', 'mm', 'Legal');
$pdf->AddPage();
$pdf->WriteHTML($buffer);
$pdf->Output('my.pdf', 'D');

 ?>

<强>结果 result 测试2

<?php
require("html2fpdf.php");
$buffer = "<div style='width:950px;'>
<h1>Hello Friends</h1><br>
<span style='font-weight: bold; font-size: 18pt; color: #FF0000; font-family: Times'>Hello there! I am red!<br></span>
<br>
<p style='font-size:10'>This is a sample code</p>
<p style='font-size:10px; color:blue;'>This is a sample code</p>
<span style='font-size:10px; color:blue;'>This is a sample code</p>
<span style='font-weight: italic; font-size: 10pt; color: #00FF00;'>I am a pdf ! Please accept my answer!</span></div>
";
$pdf = new HTML2FPDF('P', 'mm', 'Legal');
$pdf->AddPage();
$pdf->WriteHTML($buffer);
$pdf->Output('my.pdf', 'D');

 ?>

结果

result2

答案 1 :(得分:0)

得到您的问题:只需从代码中删除<div></div>

而不是使用:

$buffer = "<div>
<h1>Hello Friends</h1><br>
<p>This is a sample code</p>
</div>";

使用:

$buffer = "<h1>Hello Friends</h1><br>
<p>This is a sample code</p>";