Passionate Coder
Passionate Coder

Reputation: 7294

HTML2pdf Css not supporting

I have used a html2pdf file for generating pdf.But it does not support css at all. Please let me know what is the issue.

1) Downloaded file from https://sourceforge.net/projects/html2fpdf/?source=typ_redirect

2) placed in a folder and used below code to generate pdf (working fine)

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

but when i used css in this then generated pdf is not opened

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

My php version is PHP Version 5.3.8.

I am bowed to use HTML2PDF. So please answer if you know(I have used same with TCPDF and thats working fine but i need to implement same in HTML2PDF)

Upvotes: 2

Views: 15014

Answers (2)

Guillaume
Guillaume

Reputation: 586

I did two tests and CSS is working well except for the size of the div.

PHP Version : 5.6.30

Test 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 result Test 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');

 ?>

Result

result2

Upvotes: 3

LuFFy
LuFFy

Reputation: 9297

Got Your Issue : Just Remove <div></div> from your code

Instead of using :

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

Use :

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

Upvotes: 0

Related Questions