Generate PDF's using TCPDF - Initial Set Up

Ridgejp
Ridgejp used Ask the Experts™
on
Hi,

Just trying to get going with the initial setup so I can learn the basic's but struggling to get to grips which the location of/and which are the basic include files?

Any help would be greatly appreciated ... the new github location states that full installation instructions are available at tcpdg.org but I'm struggling to locate them?

J
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
The examples on the site seem fairly self-explanatory
https://tcpdf.org/examples/example_001/

Try that and if you don't come right post back and I will try a green fields install.

Author

Commented:
It's the include file in the php that I'm struggling with ... what's the location of 'tcpdf_include.php'?

Author

Commented:
Sorry didn't quite explain myself there ...

I understand I need to download the 'tcpdf_include.php' referenced in the example you cited and upload it to my server - I just can't find it on the site ... when I go to GitHub the file can be located but it then references further files such as tcpdf.php, tcpdf_config.php after which I got a little lost.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
The main file is this one

https://github.com/tecnickcom/TCPDF/blob/master/tcpdf.php

If you look at that you see it includes these
// TCPDF configuration
require_once(dirname(__FILE__).'/tcpdf_autoconfig.php');
// TCPDF static font methods and data
require_once(dirname(__FILE__).'/include/tcpdf_font_data.php');
// TCPDF static font methods and data
require_once(dirname(__FILE__).'/include/tcpdf_fonts.php');
// TCPDF static color methods and data
require_once(dirname(__FILE__).'/include/tcpdf_colors.php');
// TCPDF static image methods and data
require_once(dirname(__FILE__).'/include/tcpdf_images.php');
// TCPDF static methods and data
require_once(dirname(__FILE__).'/include/tcpdf_static.php');

Open in new window


You can check each of the above for further includes but it looks like if you have the files in the root and the includes folder you should be set.

You can then include tcpdf.php from anywhere - it loads the other files relative to where IT is.

Author

Commented:
Ok I'll work through those and come back to you.
Thanks
Most Valuable Expert 2011
Top Expert 2016
Commented:
I downloaded the current TCPDF in 2016 (my previous download was a few years old).

This one is apparently not ready for prime time yet:
https://github.com/tecnickcom/tc-lib-pdf

This one, captioned "obsolete," is actually the right one to use (6.2.13):
https://github.com/tecnickcom/TCPDF/

Here's what my TCPDF directory looks like:
TCPDF Directory
Here's a simple demo script:
https://iconoun.com/demo/tcpdf_example.php
<?php // demo/tcpdf_example.php
/**
 * Demonstrate how to put an image into a TCPDF document
 * and write some text over part of the image
 *
 * TCPDF 6.2.13
 *
 * See http://www.tcpdf.org/doc/code/annotated.html ?
 */
error_reporting(E_ALL);

// A DATE-TIMEZONE SETTING MAY BE REQUIRED - DEPENDS ON PHP SETTINGS
date_default_timezone_set('America/Chicago');

// LOAD THE TCPDF CLASS AND CONFIGURATION
require_once('tcpdf/config/tcpdf_config.php');
require_once('tcpdf/tcpdf.php');

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= 'storage'
. DIRECTORY_SEPARATOR
. 'tcpdf_example'
. '.pdf'
;
$pdf_file_name
= getcwd()
. DIRECTORY_SEPARATOR
. $pdf_file_link
;

// SOME VARIABLES FOR OUR TESTS (COULD COME FROM DATA BASE, ETC)
$img = 'images/ray_padded.png';
$txt = 'This is Ray';


// EXTEND THE TCPDF OBJECT SO WE CAN SUBSTITUTE OUR OWN METHODS
class PDF extends TCPDF
{
    // NULLIFY AUTOMATIC HEADER AND FOOTER
    public function Header() {}
    public function Footer() {}
}

// INSTANTIATE THE OBJECT
$pdf = new PDF('P', 'mm', 'LETTER', true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(0,0,0,TRUE);
$pdf->SetAutoPageBreak(FALSE);
// $pdf->setLanguageArray($l); // FAILS AT CURRENT 6.2.13
$pdf->SetFont('times', '', 13);
$pdf->setCellPaddings(0,0,0,0);
$pdf->setCellMargins(0,0,0,0);

// DO NOT DO THIS
// $pdf->setFillColor(255,255,224);

// APPLY CELL BORDERS, IF NEEDED
$bdr = '1TRBL';

// BUT IF NO CELL BORDER IS NEEDED
$bdr = 0;

// ADD A PAGE
$pdf->AddPage('P', 'LETTER', TRUE);

// I HAVE NO IDEA WHAT THIS IS DOING TO IMAGE SCALE - EXPERIMENT WITH IT
$pdf->setImageScale(1.53);

// GET THE IMAGE SETTINGS
$pdf->Image
( $img
, 0             // $x
, 0             // $y
, 0             // WIDTH
, 0             // HEIGHT
, 'PNG'         // TYPE
, '#'           // LINK URL
, 'T'           // SET POINTER TOP LEFT
, FALSE         // NO RESIZING
, 300           // DPI
, 'L'           // PALIGN
, FALSE         // ISMASK
, FALSE         // IMGMASK
, 0             // BORDER
, FALSE         // FIT TO BOX
, FALSE         // HIDDEN
, FALSE         // FIT ON PAGE
)
;

// ADD SOME TEXT ON TOP OF THE IMAGE
$pdf->Text
( 24            // $x,
, 24            // $y,
, $txt
, FALSE         // $fstroke           = false,
, FALSE         // $fclip             = false,
, TRUE          // $ffill             = true,
, $bdr          // $border            = whatever,
, 2             // $ln                = 0, 2=PUT CURSOR BELOW
, ''            // $align             = '', DEFAULT LEFT
, FALSE         // $fill              = false, CELL BACKGROUND
, ''            // $link              = '', NOT A LINK
, 0             // $stretch           = 0, NO TEXT STRETCH
, FALSE         // $ignore_min_height = false,
, 'A'           // $calign            = 'T', A=FONT TOP INSIDE CELL
, 'T'           // $valign            = 'M', T=VERTICAL ALIGN INSIDE CELL
, TRUE          // $rtloff            = false TRUE = USE PAGE TOP LEFT CORNER TO ALIGN
)
;

// WRITE THE PDF FILE TO THE SERVER
$pdf->Output($pdf_file_name, 'F');

// PRESENT A CLICKABLE LINK SO WE CAN D/L AND PRINT THE PDF
echo '<a target="my_PDF" href="' . $pdf_file_link . '"><strong>Print the PDF</strong></a>';

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
The examples folder of that download has everything you need to know - pick anyone and you will see the following include
require_once('tcpdf_include.php');

Open in new window


That refers to a the tcpdf_include.php file which is in the same folder as the examples and looks like this
// THIS IS A CUSTOMISED COPY OF THE VERSION OFF THE ROOT FOLDER
require_once('config/tcpdf_config_alt.php');

// Include the main TCPDF library (search the library on the following directories).
$tcpdf_include_dirs = array(
	realpath('../tcpdf.php'),
	'/usr/share/php/tcpdf/tcpdf.php',
	'/usr/share/tcpdf/tcpdf.php',
	'/usr/share/php-tcpdf/tcpdf.php',
	'/var/www/tcpdf/tcpdf.php',
	'/var/www/html/tcpdf/tcpdf.php',
	'/usr/local/apache2/htdocs/tcpdf/tcpdf.php'
);
foreach ($tcpdf_include_dirs as $tcpdf_include_path) {
	if (@file_exists($tcpdf_include_path)) {
		require_once($tcpdf_include_path);
		break;
	}
}

Open in new window

The above is just an overly complex way of saying "find tcpdf.php and this is where it could potentially be". You can just tell the script directly where the file is and skip all the CPU abuse.

From the above we can also see that the only files included are the tcpdf_config.php file (which you should make a copy of and customise to suite your requirements) and the tcpdf.php file - which will find the rest of the files it needs based on its location.

Assume you want to setup the following folder structure

webroot/
     index.php
     config
         <customised tcpdf_config.php>
     TCPDF
           <extracted TCPDF archive> 

Open in new window

In index.php you would have the following
require("config/tcpdf_config.php");
require("TCPDF/tcpdf.php");

// You are good to go here

Open in new window


All that remains is to edit your copy of config/tcpdf_config.php to match your environment.

Author

Commented:
Thank you both ... spent some time looking at both examples and finally think I've got a basis now for starting to do some testing.

Really appreciate your input as alway ... just don't leave the country I've got loads more questions to come!

J
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial