Yii 1.1: tcpdf

PDF generation with TCPDF
40 followers

ETcPdf is a wrapper for the TCPDF class, which is not bundled, since it's huge (not so much the class itself, but the associated files, making a 9 Mb tarball or 13 Mb unpacked). You can download the class here. Notice that TCPDF is updated almost daily, so I recommend you to update the class file frequently, since new cool features are added on every release. I used TCPDF 4.2.007 while developing this wrapper.

Resources

Documentation

Requirements

  • Yii 1.0 or above
  • TCPDF 4.2.007 or above

Installation

  • Extract the release file under protected/extensions

TCPDF installation:

  • Download the tarball.
  • Unpack it on the extension directory.
  • If necessary, rename the resulting subdirectory to tcpdf
  • You just need to keep these subdirectories inside tcpdf: config, fonts and images, since the cache is stored on the runtime directory, and no docs are needed

Usage

See the following code example:

<?php
$pdf = Yii::createComponent('application.extensions.tcpdf.ETcPdf', 
                            'P', 'cm', 'A4', true, 'UTF-8');
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Nicola Asuni");
$pdf->SetTitle("TCPDF Example 002");
$pdf->SetSubject("TCPDF Tutorial");
$pdf->SetKeywords("TCPDF, PDF, example, test, guide");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont("times", "BI", 20);
$pdf->Cell(0,10,"Example 002",1,1,'C');
$pdf->Output("example_002.pdf", "I");

Total 20 comments

#13698 report it
Ravi Bhalodiya at 2013/06/18 08:34am
PDF should be as my Page...

I have already implemented this extension.. How can I get exported file as my page look like.. what can I do?? Thank in advance..

#13535 report it
rackycz at 2013/06/04 10:38am
Custom Header / Footer

To create custom header / footer you have to extend the basic class TCPDF and define methods Footer() / Header(). It is more complicated if you are using ETCPDF because you have to edit constructor in ETcPdf.php. I created one more parameter and based on it's value constructor desides which object will be used to fill variable myTCPDF.

// etcpdf.php
require_once(dirname(__FILE__).'/myTCPDF.php');
public function __construct($orientation, $unit, $format, $unicode, $encoding, $docType = null)
{
  switch ($docType)
  {
    case null:
      $this->myTCPDF = new TCPDF($orientation, $unit, $format, $unicode, $encoding);
      return;
      break;
 
    case 'myPdf':
      $this->myTCPDF = new myTCPDF($orientation, $unit, $format, $unicode, $encoding);
      return;
      break;
  }
}
 
// myTCPDF.php
 
require_once(dirname(__FILE__).'/tcpdf/tcpdf.php');
class myTCPDF extends TCPDF
{
  public function Header() 
  {
    // ...
  }
  public function Footer() 
  {
    // ..
  }
}

You also have to change methods in etcpdf.php:

public function __call($method, $params)
{
  return call_user_func_array(array($this->myTCPDF, $method), $params);
}
 
public function __set($name, $value)
{
   $this->myTCPDF->$name = $value;
}
 
public function __get($name)
{
   return $this->myTCPDF->$name;
}

Or should I do it in another way? What If I want to generate 10 different PDFs with different headers and footers? Do I really have to extend TCPDF 10x? Or can ETCPDF solve this situation?

#13129 report it
lgastmans at 2013/05/07 02:35am
header and footer

The answer lies in this link: Your text to link here...

#13124 report it
Wiwit Iwan SEP at 2013/05/06 11:07pm
issue with header and footer

I have issue with header and footer.. how to set header and footer and how to erase the line at top and bottom of the pages...? can anyone help with this issue?

#12908 report it
lgastmans at 2013/04/19 09:21pm
line at top of page

I have started using tcpdf and have it working, but for a mysterious line that appears at the top of the page. I have just the following as code, yet the line keeps appearing:

$pdf = Yii::createComponent('application.extensions.tcpdf.ETcPdf', 'P', 'cm', 'A4', true, 'UTF-8');
$pdf->SetFont('dejavusans', 'B', 16, '', true);
$pdf->AddPage();
$pdf->Cell(0, 0, $pdf->header_title, 0, 1, 'L', 0, '', 0);
$pdf->Output('invoice.pdf', 'I');

Anyone else have this issue?

#12894 report it
CedSha at 2013/04/18 08:49am
AliasNbPages error

I confirm same error report for me. I delete the line

$pdf->AliasNbPages();

and then it works again .I do not know precisely the consequences... but seems this methods no more exist in tcpdf.

#12885 report it
lgastmans at 2013/04/18 12:19am
tcpdf version 6

just installed the extension and

  1. solved the "K_PATH_CACHE already defined" error thanks to @Pablovp

  2. but, now i get the following error:

call_user_func_array() expects parameter 1 to be a valid callback, class 'TCPDF' does not have a method 'AliasNbPages'

any cues?

#11819 report it
CedSha at 2013/02/05 10:47pm
Answer to @ranairfan

Unfortunately this extension is not a converter from HTLM to PDF. It 'just' give you access to the tcpdf function you must draw your report by yourself...

#11397 report it
ranairfan at 2013/01/11 01:45am
how to load view

how i tell tcpdf to generate pdf of particular view ?

#11395 report it
ranairfan at 2013/01/11 01:43am
TCPDF ERROR: Some data has already been output, can't send PDF file

i have a view and want its pdf format. how can i get ?

i have this code

$this->renderPartial('application.views.reportcard.report',array( 'student_info'=>$student_info, ));

$pdf = Yii::createComponent('application.extensions.tcpdf.ETcPdf', 'P', 'cm', 'A4', true, 'UTF-8'); $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor("Nicola Asuni"); $pdf->SetTitle("TCPDF Example 002"); $pdf->SetSubject("TCPDF Tutorial"); $pdf->SetKeywords("TCPDF, PDF, example, test, guide"); $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); $pdf->AliasNbPages(); $pdf->AddPage(); $pdf->SetFont("times", "BI", 20); $pdf->Cell(0,10,"Example 002",1,1,'C'); $pdf->Output("example_002.pdf", "I");

#8044 report it
willowdan at 2012/05/05 05:52am
error %PDF-

The solution is to use blank layout, which only contains

<?php echo $content; ?>

This prevents any appending html tags to the generated pdf.

Cheers!

#6365 report it
Pablovp at 2012/01/03 11:20am
Solution for Constant K_PATH_CACHE already defined

Change line 85 from:

define ("K_PATH_CACHE", Yii::app()->getRuntimePath());

To:

if (!defined("K_PATH_CACHE")) 
{
    define ("K_PATH_CACHE", Yii::app()->getRuntimePath());
}
#6045 report it
MountainCastle at 2011/12/07 04:37pm
husnoo - tcpdf

@husnoo

Make sure you have a tcpdf subdirectory. Look in you folder structure you may have the files in extensions\tcpdf. If so move all the files in extensions\tcpdf to extensions\tcpdf\tcpdf.

#6033 report it
husnooo at 2011/12/07 02:48am
tcpdf

Hi,

i got the following error require_once(D:\Dev\Apache\htdocs\first\protected\extensions\tcpdf/tcpdf/tcpdf.php): failed to open stream: No such file or directory.

Please suggest the solution. thnks to all...

#5943 report it
Keith Sorbo at 2011/11/27 07:32pm
How to use extend header / footer

I love the extension and am using it successfully.

One thing I haven't been able to figure out is how to replace the header footer functions of tcpdf. In order to be able to customize the footer, you must redefine the Footer function. Since this extension does not technically extend the tcpdf class, redefining the footer function does not work.

#5908 report it
MountainCastle at 2011/11/23 08:33pm
tcpdf

naga --

make sure you have a subdirectory called tcpdf with the tcpdf files(not the wrapper) in there. The wrapper files go in extensions\tcpdf in there you have a sub directory tcpdf that contains the tcpdf files

#4297 report it
naga at 2011/06/23 01:48am
tcpdf

Hi I got the following error require_once(D:\Dev\Apache\htdocs\first\protected\extensions\tcpdf/tcpdf/tcpdf.php): failed to open stream: No such file or director,

#3159 report it
Trejder at 2011/03/22 06:45pm
Problems downloading main class

You can get source class of TCPDF from site mentioned by extension author (www.tecnick.com) or from www.tcpdf.org. But both sites gets often overloaded and results in timeouts instead of real content. In this situation, you may try to get the class from SourceForge, at: http://sourceforge.net/projects/tcpdf/.

#2634 report it
Mahdi at 2011/01/24 07:57am
Table in the Header

Hi, I want to use a table in the Header. It dosen't work. Any Idea??

#391 report it
lorenzomar at 2010/06/17 07:05am
error %PDF-

hi guys... I setting up this extension right now, but... i have one problem!! browser send me an alert.. it says:"file does not begin with '%PDF-'". I try the tcpdf library in a stand alone script (withous using YII) and it work fine!! i need help! thank you

PS i'm sorry for my bad english, i'm italian!! :D

Leave a comment

Please to leave your comment.

Create extension