tcpdf Cheat Sheet

http://www.tcpdf.org

Examples: http://www.tcpdf.org/examples.php

Documentation: http://www.tcpdf.org/doc/code/functions_func.html

https://www.rubydoc.info/gems/rfpdf/1.17.1/TCPDF#Image-instance_method

Nice lib, but horrible documentation

 

Initialisation

Page Setup

$pdf = new TCPDF('P', 'mm', 'A4');  // P = portrait, L = landscape

http://www.tcpdf.org/doc/code/classTCPDF.html#a134232ae3ad1ec186ed45046f94b7755

Available Page sizes: http://sourceforge.net/p/tcpdf/code/ci/master/tree/include/tcpdf_static.php Line 114

Meta-info

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Foo');
$pdf->SetTitle('Bar');
$pdf->SetSubject('Bar');
$pdf->SetKeywords('www.example.com');

Simple, clean white sheet

Margins: http://www.tcpdf.org/doc/code/classTCPDF.html#ab3bbdb7c85ea08d175fd559be6132ba0

Font: http://www.tcpdf.org/doc/code/classTCPDF.html#afd56e360c43553830d543323e81bc045

$pdf->AddPage();
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->SetMargins(0, 0, 0, true);
$pdf->SetAutoPageBreak(TRUE, 0);
$pdf->SetFont('dejavusans', '', 14, '', true);

Output

http://www.tcpdf.org/doc/code/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1

// Directly to browser
$pdf->Output('example_001.pdf', 'I')

// To File
$pdf->Output('/path/to/example_001.pdf', 'F')

 

(HTML)-Text

setFont: http://www.tcpdf.org/doc/code/classTCPDF.html#afd56e360c43553830d543323e81bc045
writeHTMLCell: http://www.tcpdf.org/doc/code/classTCPDF.html#a8458280d15b73d3baffb28eebe2d9246

$pdf->SetFont('dejavusans', '', 11, '', true);
$html = 'Foo<br />Bar';
$pdf->writeHTMLCell(0, 0, 50, 100, $html);

Image

http://www.tcpdf.org/doc/code/classTCPDF.html#a714c2bee7d6b39d4d6d304540c761352

$pdf->Image('/images/layout/image.jpg', 0, 0, 150);

Center horizontally

$pdf->Image('image.png', null, 30, null, $pageHeight - 30 - 30, null, null, null, null, null, 'C'); // center image horizontally

Drawing

Line Styles: http://www.tcpdf.org/doc/code/classTCPDF__STATIC.html#a0ca1ec8163a54a7264d8bad924f2e25a

S = Stroke the path
F = Fill the path

Rectangle

http://www.tcpdf.org/doc/code/classTCPDF.html#a8fc7b86c255aeb0f14d281bdaeb2015c

$pdf->Rect(20, 20, 60, 20, 'SF', array(), array(255, 255, 255));

Barcode 

// define barcode style
$style = array(
'position' => '',
'align' => 'C',
'stretch' => false,
'fitwidth' => true,
'cellfitalign' => '',
'border' => false,
'hpadding' => 'auto',
'vpadding' => 'auto',
'fgcolor' => array(0,0,0),
'bgcolor' => false, //array(255,255,255),
'text' => true,
'font' => 'helvetica',
'fontsize' => 6,
'stretchtext' => 0
);

$pdf->write1DBarcode('47110815', 'C39', 25, 25, 50, 10, 0.4, $style, 'N');

Symfony Framework Setup

  • Save lib to lib/vendor/tcpdf
  • In a symfony action load the lib via
    •     require_once(sfConfig::get('sf_lib_dir') .
            '/vendor/tcpdf/tcpdf.php');