Generate PDF's using TCPDF - Initial Set Up

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
RidgejpManaging DirectorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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.
0
RidgejpManaging DirectorAuthor Commented:
It's the include file in the php that I'm struggling with ... what's the location of 'tcpdf_include.php'?
0
RidgejpManaging DirectorAuthor 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.
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Julian HansenCommented:
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.
0
RidgejpManaging DirectorAuthor Commented:
Ok I'll work through those and come back to you.
Thanks
0
Ray PaseurCommented:
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

0
Julian HansenCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RidgejpManaging DirectorAuthor 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
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.