KItis
KItis

Reputation: 5646

PHP: converting html file to pdf

I have an html file named welcomemailtemplate.html and I need to convert that file to a PDF.

I first read this file using the following method provided by Yii framework:

$filename = Yii::app()->basePath.'\views\email\welcomemailtemplate.html';
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
$name = $model->name;
fclose($handle);
$message = str_replace ( "[username]", $name, $contents );

Then, to generate the PDF file, the following parameters are set:

 Yii::import('application.vendors.*');
       require_once('tcpdf/tcpdf.php');
       require_once('tcpdf/config/lang/eng.php');
       $pdf = new TCPDF();
       $pdf->SetCreator("Torget");
       $pdf->SetAuthor('test name');
       $pdf->SetTitle('Savani Test');
       $pdf->SetSubject(' Torget Order Confirmation');
       $pdf->SetKeywords(' Torget, Order, Confirmation');
       //$pdf->SetHeaderData('', 0, PDF_HEADER_TITLE, '');
       $pdf->SetHeaderData('', 0, "Torget Order", '');
       $pdf->setHeaderFont(Array('helvetica', '', 8));
       $pdf->setFooterFont(Array('helvetica', '', 6));
       $pdf->SetMargins(15, 18, 15);
       $pdf->SetHeaderMargin(5);
       $pdf->SetFooterMargin(10);
       $pdf->SetAutoPageBreak(TRUE, 0);
       $pdf->SetFont('dejavusans', '', 7);
       $pdf->AddPage();

If I pass the content as follows, it creates the PDF:

 $pdf->writeHTML("<span>Hello World!</span>", true, false, true, false, '');

But if I pass the read html file content for pdf creating using following method it gives following error:

  $pdf->writeHTML($message, true, false, true, false, '');
       $pdf->LastPage();

Error message:

Undefined index: thead

Upvotes: 2

Views: 4276

Answers (2)

Kris
Kris

Reputation: 8873

CutyCapt seems to be a very good option for you. Its very easy to integrate also.

Upvotes: 1

Vlad Jula-Nedelcu
Vlad Jula-Nedelcu

Reputation: 1696

Try to validate the file using the w3c validator http://validator.w3.org/.
I've worked with tcpdf before but i gave it up because it didn't seem reliable. You can also try wkhtmltopdf binary (only if your hosting allows you to use proc_open/proc_close). Seems a little more stable to me. It also has a PHP class to help you use it.

Upvotes: 3

Related Questions