FIXED PDF Generation in component

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
adam.hughes
Joomla! Intern
Joomla! Intern
Posts: 91
Joined: Thu Sep 15, 2005 7:30 am

FIXED PDF Generation in component

Post by adam.hughes » Wed Aug 04, 2010 7:32 am

I'm having (quite) a few issue's with pdf generation in my custom component. It's not that the pdf isn't generated , just that I can't modify the file beforehand.

I have tried setting the following in view.pdf.php.

Code: Select all

<?php
	defined('_JEXEC') or die('Restricted Access');
	jimport( 'joomla.application.component.view');

	class questportalViewdownloads extends JView
	{
		function display($tpl = null)
		{
			
			//$document =& JFactory::getDocument();
			$this->setName("Quest Leisure Price list");
			$this->setTitle("{$this->sapdetails['name']}; Price List");
			$this->setDescription("Price List for {}. Generated on {$this->day} at {$this->time}");
			$this->setMetaData('keywords', 'Some Keywords');
			$this->setGenerator('www.questleisure.com');
			
			$document =& JFactory::getDocument();
			$document->setName('Some Name');
			$document->setTitle('Some Title');
			$document->setDescription('Some Description');
			$document->setMetaData('keywords', 'Some Keywords');
			$document->setGenerator('Some Generator');
			
			parent::display($tpl);
			
		}
	}
?>
I don't mind creating the pdf file from scratch using the documentation from tcpdf, but I don't want to have to install another version of tcpdf (jooml already has one installed), and I don't know how to create a new tcpdf class without adding another version.

I can't find any documentation (aside from the very basic bits) on the pdf generation anywhere. Is there any available (or can anyone help) ?
Last edited by adam.hughes on Thu Aug 05, 2010 10:40 am, edited 1 time in total.

User avatar
nailson_imgn
Joomla! Guru
Joomla! Guru
Posts: 729
Joined: Wed Apr 15, 2009 5:33 pm
Location: Fortaleza, CE - Brasil

Re: PDF Generation in component

Post by nailson_imgn » Wed Aug 04, 2010 4:40 pm

adam.hughes wrote:I don't mind creating the pdf file from scratch using the documentation from tcpdf, but I don't want to have to install another version of tcpdf (jooml already has one installed), and I don't know how to create a new tcpdf class without adding another version.
To import:

Code: Select all

jimport( 'tcpdf.tcpdf' );
To use the class:

Code: Select all

$pdfClass = new TCPDF();
Or you could extend TCPDF:

Code: Select all

class YourPDFClassName extends TCPDF{}
.
Nailson Oliveira
Técnico de Tecnologia da Informação - Desenvolvedor Joomla!
-----------------------------------------------------------------------------------------

adam.hughes
Joomla! Intern
Joomla! Intern
Posts: 91
Joined: Thu Sep 15, 2005 7:30 am

Re: PDF Generation in component

Post by adam.hughes » Thu Aug 05, 2010 8:47 am

Thanks for the update.
I tried the jimport and the import itself does work okay.

The trouble is that none of the tcpdf config is updated this way, so everything start's to fail afterwards (such as loading fonts etc.).

I then found the following file /libraries/joomla/document/pdf/pdf.php

This seems to be the file I needed as this does teh TCPDF import, but also set's all the variables that are used by TCPDF.

The only thing is I don't know how to use pdf.php

adam.hughes
Joomla! Intern
Joomla! Intern
Posts: 91
Joined: Thu Sep 15, 2005 7:30 am

Re: PDF Generation in component

Post by adam.hughes » Thu Aug 05, 2010 9:25 am

I've also come across issue's with the fact that the version of tcpdf included in joomla is very old (2.5).
The latest version is 5.x.

So I think the best thing to do is upgrade the installed version of tcpdf and then use this direct.

So far I have :

1. Backed up /libraries/tcpdf to libraries/tcpdf.original
2. Downloaded the latest tcpdf from http://www.tecnick.com.
3. Updated the tcpdf with the downloaded one.
4. Created a test file on my webserver and set it up the same as the fiorst example on the tcpdf site (code below)

Code: Select all

<?php
require_once('libraries/tcpdf/config/lang/eng.php');
require_once('libraries/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set default font subsetting mode
$pdf->setFontSubsetting(true);

// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);

// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();

// Set some content to print
$html = <<<EOD
<h1>Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;color:black;"><span style="background-color:#CC0000;"> TC<span style="color:white;">PDF</span> </span></a>!</h1>
<i>This is the first example of TCPDF library.</i>
<p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
<p>Please check the source code documentation and other examples for further information.</p>
<p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
EOD;

// Print text using writeHTMLCell()
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);

// ---------------------------------------------------------

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('example_001.pdf', 'F');

//============================================================+
// END OF FILE
//============================================================+
?>
All the above is working fine. So I at least now tcpdf and my webserver is working okay.

adam.hughes
Joomla! Intern
Joomla! Intern
Posts: 91
Joined: Thu Sep 15, 2005 7:30 am

Re: PDF Generation in component

Post by adam.hughes » Thu Aug 05, 2010 9:35 am

I then added the following into my component

Code: Select all


global $l;
$l = Array();
$l['a_meta_charset'] = 'UTF-8';
$l['a_meta_dir'] = 'ltr';
$l['a_meta_language'] = 'en';
$l['w_page'] = 'page';

jimport( 'tcpdf.tcpdf' );
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->setFontSubsetting(true);
$pdf->SetFont('dejavusans', '', 14, '', true);
$pdf->AddPage();
$html = <<<EOD
<h1>Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;color:black;"><span style="background-color:#CC0000;"> TC<span style="color:white;">PDF</span> </span></a>!</h1>
<i>This is the first example of TCPDF library.</i>
<p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
<p>Please check the source code documentation and other examples for further information.</p>
<p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
EOD;
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
$pdf->Output('example_001.pdf', 'F');

This does generate the pdf, but issues the following error : TCPDF ERROR: Some data has already been output, can't send PDF file

adam.hughes
Joomla! Intern
Joomla! Intern
Posts: 91
Joined: Thu Sep 15, 2005 7:30 am

Re: PDF Generation in component

Post by adam.hughes » Thu Aug 05, 2010 9:37 am

So the outcome was :

1. Upgrade tcpdf to the latest version
2. Add the following lines into the controller in my component :

Code: Select all

global $l;
$l = Array();
$l['a_meta_charset'] = 'UTF-8';
$l['a_meta_dir'] = 'ltr';
$l['a_meta_language'] = 'en';
$l['w_page'] = 'page';
jimport( 'tcpdf.tcpdf' );
The above then allows me to generate pdf's

adam.hughes
Joomla! Intern
Joomla! Intern
Posts: 91
Joined: Thu Sep 15, 2005 7:30 am

Re: PDF Generation in component

Post by adam.hughes » Thu Aug 05, 2010 10:40 am

Even though the tcpdf is working fine, I have found that it's much (approx 3 times) slower then just using fpdf direct (fpdf is much older and doesn't support utf8, but suit's my needs).

I have the following two different solutions (in case anyone else wants to use pdf generation in their own component)

FPDF
  • 1. download fpdf from http://fpdf.org/
    2. Create a fpdf directory in my component (I created in site/libraries)
    3. Copy the fpdf.php, fpdf.css and fonts directory from the fpdf download into the above directory
    4. Add the following lines in my controller function

    Code: Select all

    define('FPDF_FONTPATH',JPATH_COMPONENT.DS.'libraries'.DS.'fpdf'.DS.'font'.DS);
    require_once (JPATH_COMPONENT.DS.'libraries'.DS.'fpdf'.DS.'fpdf.php');
    				
    $pdf=new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $dir = getcwd();
    $file = JPATH_COMPONENT.DS.'test.pdf';
    $pdf->Output($file);
    
    5. Then finish by adding a link to download the pdf ($file above) in the view used by the controller.
TCPDF
To use TCPDF I :
  • 1. Upgrade TCPDF in joomla (download tcpdf from tecknick and overwrite the one in libraries/tcpdf)
    2. Add the following to my code

    Code: Select all

    jimport( 'tcpdf.tcpdf' );
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('TCPDF Example 001');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    $pdf->setLanguageArray($l);
    $pdf->setFontSubsetting(true);
    $pdf->SetFont('helvetica', '', 14, '', true);
    $pdf->AddPage();
    $txt = <<<EOD
    Hello World
    EOD;
    $pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);
    $pdf->Output('test2.pdf', 'F');
    
Both work fine, but as I say the fpdf one seems approx 3 times quicker on this very simple exmaple. I did text with a couple of different font's in tcpdf and this made a massive difference. The above using helvicta was one of the quickest, some of the other ones two twice as long (suh as dejavusna.

hjortsberg
Joomla! Explorer
Joomla! Explorer
Posts: 359
Joined: Sun Jun 22, 2008 4:25 pm

Re: FIXED PDF Generation in component

Post by hjortsberg » Fri Dec 03, 2010 11:32 pm

I have put this in my view.pdf.php but all I get is a blank pdf except one small square at the beginning

Code: Select all

function display($tpl = null)
	{
		
		global $option, $mainframe;
		
		$document =& JFactory::getDocument();
				
		jimport( 'tcpdf.tcpdf' );
		require_once('libraries/tcpdf/config/lang/swe.php');
		require_once('libraries/tcpdf/tcpdf.php');

		$pdfClass = new TCPDF();
		$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
		$pdf->SetCreator('TCPDF');
		$pdf->SetAuthor('Nicola Asuni');
		$pdf->SetTitle('TCPDF Example 001');
		$pdf->SetSubject('TCPDF Tutorial');
		$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
		//$pdf->setPrintHeader(false);
		//$pdf->setPrintFooter(false);
		//$pdf->SetDefaultMonospacedFont('courier');
		$pdf->SetMargins('15', '27', '15');
		$pdf->SetHeaderMargin('5');
		$pdf->SetFooterMargin('10');
		$pdf->SetAutoPageBreak(TRUE, '25');
		$pdf->setImageScale(1.25);
		$pdf->setLanguageArray($l);
		//$pdf->setFontSubsetting(true);
		$pdf->SetFont('vera', '', 14, '', true);
		$pdf->AddPage();
		$txt = '
<h1>Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;color:black;"><span style="background-color:#CC0000;"> TC<span style="color:white;">PDF</span> </span></a>!</h1>
<i>This is the first example of TCPDF library.</i>
<p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
<p>Please check the source code documentation and other examples for further information.</p>
<p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
';


		$pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);
		//$pdf->Output('test2.pdf', 'F');
		
		parent::display($tpl);
	}
Any idea??

Help pleeezzzee:(

isilothar
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Jan 05, 2012 10:24 am

Re: FIXED PDF Generation in component

Post by isilothar » Thu Jan 05, 2012 10:35 am

hjortsberg wrote:I have put this in my view.pdf.php but all I get is a blank pdf except one small square at the beginning

Code: Select all

function display($tpl = null)
	{
		
		global $option, $mainframe;
		
		$document =& JFactory::getDocument();
				
		jimport( 'tcpdf.tcpdf' );
		require_once('libraries/tcpdf/config/lang/swe.php');
		require_once('libraries/tcpdf/tcpdf.php');

		$pdfClass = new TCPDF();
		$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
		$pdf->SetCreator('TCPDF');
		$pdf->SetAuthor('Nicola Asuni');
		$pdf->SetTitle('TCPDF Example 001');
		$pdf->SetSubject('TCPDF Tutorial');
		$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
		//$pdf->setPrintHeader(false);
		//$pdf->setPrintFooter(false);
		//$pdf->SetDefaultMonospacedFont('courier');
		$pdf->SetMargins('15', '27', '15');
		$pdf->SetHeaderMargin('5');
		$pdf->SetFooterMargin('10');
		$pdf->SetAutoPageBreak(TRUE, '25');
		$pdf->setImageScale(1.25);
		$pdf->setLanguageArray($l);
		//$pdf->setFontSubsetting(true);
		$pdf->SetFont('vera', '', 14, '', true);
		$pdf->AddPage();
		$txt = '
<h1>Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;color:black;"><span style="background-color:#CC0000;"> TC<span style="color:white;">PDF</span> </span></a>!</h1>
<i>This is the first example of TCPDF library.</i>
<p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
<p>Please check the source code documentation and other examples for further information.</p>
<p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
';


		$pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);
		//$pdf->Output('test2.pdf', 'F');
		
		parent::display($tpl);
	}
Any idea??

Help pleeezzzee:(

Hi hjortsberg

One problem can be that $pdf->Output('test2.pdf', 'F'); will save file to local server with the name given by name.



how Output work in tcpdf. You probably whant to use I insted of F.

CPDF::Output ($name = 'doc.pdf',$ dest = 'I' )
Send the document to a given destination: string, local file or browser. In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.
The method first calls Close() if necessary to terminate the document.

Parameters:
$name (string) The name of the file when saved. Note that special characters are removed and blanks characters are replaced with the underscore character.
$dest (string) Destination where to send the document. It can take one of the following values:
I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
D: send to the browser and force a file download with the name given by name.
F: save to a local server file with the name given by name.
S: return the document as a string (name is ignored).
FI: equivalent to F + I option
FD: equivalent to F + D option
E: return the document as base64 mime multi-part email attachment (RFC 2045)

arrowjoe
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Wed Dec 03, 2008 12:35 pm

Re: FIXED PDF Generation in component

Post by arrowjoe » Wed Feb 01, 2012 4:26 pm

This caused me screens full of gobbledegook and endless frustration.
Use the RAW format to generate output and generate your PDF directly in

Code: Select all

view.raw.php
or in an attached template.
One rather untidy fix was to simply halt PHP execution after outputting the PDF ...

Code: Select all

$pdf->Output('test2.pdf', 'I');
exit();
Much better is ...

Code: Select all

ob_end_clean();
$pdf->Output('test2.pdf', 'I');
Joomla buffers output prior to rendering. This function discards the contents of the topmost output buffer and turns off buffering. The net result is that the PDF file generated by your code is not 'wrapped' in HTML headers at render time. Credit for this to 'Luffy' at StackOverflow.


Locked

Return to “Joomla! 1.5 Coding”