Preferred PDF generation

4 views
Skip to first unread message

Jack Timmons

unread,
Aug 26, 2010, 4:07:18 PM8/26/10
to Professional PHP Developers
This isn't a question of how or what to use. Codeacula needs your
thoughts on the preferred method to generate PDFs.

Bonus points if you relate it to the MVC structure.

Even more bonus points if you relate it to CakePHP.

I'm not going to copy answers, I'm not finish for free work here. I'm
trying to compare what "industry experts" use compared to the garbage
I see going on in here, so I can make a case about something.

I have a bag full of upvotes and love waiting for you.

--
Jack Timmons
@_Codeacula

Robert Gonzalez

unread,
Aug 26, 2010, 5:30:39 PM8/26/10
to professi...@googlegroups.com
There are a bunch of PDF generating packages that are floating around. We've rolled our own PDF generator where I work as a pluggable hook into the fpdf library. We've also used tcPDF, but have gone pretty much full blown fpdf for our PDF generating needs.

Sorry, without experience in CakePHP I wouldn't know how this would relate to it. As for MVC, we generally build the PDF templates as "views" and generate them with the same model data we would if we pushed regular HTML views.

--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.

For information or project assistance please visit :
http://www.360psg.com

You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP

sc_mach

unread,
Aug 26, 2010, 5:32:52 PM8/26/10
to Professional PHP Developers
Not sure if my answer falls into your requested "thoughts on the
preferred method" criteria, but here's my shot at fame...

I used to use FPDF (http://www.fpdf.org/) but it is no longer
supported, so I migrated over to TCPDF (http://www.tcpdf.org/). It
works well, has decent documentation, examples, and you can get pretty
quick support from the developer(s).

I have an abstract class that implements FPDF and it's methods. Then
there are "pdf" classes that when called extend the abstract and
return a full pdf based on parameters passed and/or set in it
concretely.

I will attempt to break it down... I am missing TONS of p[arts here,
but I think for the most part I covered the general structure.

FPDF.php
Pdf.php // abstract implementor
Pdf/
PdfThatIMade.php // These are the core of each unique pdf. They
are what is called. They choose the layout, extend the pdf abstract
and provide the "body content".
AnotherPdfThatIMade.php
Common/
Header.php // these are chunks of code that are used among
many layouts, or chosen by the 'core' (described above) to inject in
to its "body". (In a non-Barry Bonds way...)
Footer.php
Signature.php
Images/
logo1.jpg // just an image directory to keep things tidy, plus
the image path set in the abstract can be used in all layouts to
access these.
logo2.jpg
Layout.php // abstract that is extended by the layout files. It
gives access to the base Pdf class.
Layout/
Blank.php // these are the "shell", they set things like
margins, page count, and position the common elements around the body
Letterhead.php
....

So what you end up with is something like:

class Pdf extends TCPDF {
public $output_type;
public function setLayout();
public function setBody();
public function render();
}

class Pdf_Layout_Letterhead extends Pdf_Layout {
$this->pdf->setMargins(10,15,10,15);
$this->pdf->AddPage();

public function setLayout() {
$this->getCommon('Header');
$this->buildBody();
$this->getCommon('Footer');
}
}

class PdfThatIMade extends Pdf {
public $layout = 'Letterhead';
public $output_type = 'inline';

public __construct($params) {
if (isset($params['output_type'])) {
$this->output_type = $params['output_type'];
}
}

setBody() {
$this->pdf->Cell('Here is the body of my PDF!');
$this->getCommon('signature');
}
}

Then you can call:

$params = array('output_type' => 'file');
$pdf = new PdfThatIMade($params);

So now it is really easy to create a pdf by selecting a layout that
has the components you want and injecting a body. Or create a new
layout, new components, edit components, and have it affect all files
that include that component....so on and so on in to the limitless
possibilities :)

Jack Timmons

unread,
Aug 26, 2010, 5:35:56 PM8/26/10
to professi...@googlegroups.com
On Thu, Aug 26, 2010 at 4:30 PM, Robert Gonzalez
<robert.anth...@gmail.com> wrote:
> Sorry, without experience in CakePHP I wouldn't know how this would relate
> to it. As for MVC, we generally build the PDF templates as "views" and
> generate them with the same model data we would if we pushed regular HTML
> views.

Do you have a PDF layout that requires/imports the vendor package, and
the views set up the PDF, or do you import the vendor package per
view?

Do you have special templates for each page type (8.5x11, etc), or
force the view to rely on that?

I'm of the opinion the templates should import the vendor package, and
that we should have a layout where the package automatically generates
the page coordinates. Probably in some sort of helper.

--
Jack Timmons
@_Codeacula

Jack Timmons

unread,
Aug 26, 2010, 7:02:14 PM8/26/10
to professi...@googlegroups.com
On Thu, Aug 26, 2010 at 4:32 PM, sc_mach <jason....@gmail.com> wrote:
> So now it is really easy to create a pdf by selecting a layout that
> has the components you want and injecting a body. Or create a new
> layout, new components, edit components, and have it affect all files
> that include that component....so on and so on in to the limitless
> possibilities :)

Close to what I'm looking to do myself. Thanks.

--
Jack Timmons
@_Codeacula

Cesar Ramos

unread,
Aug 27, 2010, 11:09:33 AM8/27/10
to professi...@googlegroups.com
i used fpdf a long time ago for one of my projects and its ok although checking tcpdf and the html support it seems like a great addon for my apps going to try it

Jack Timmons
@_Codeacula

Robert Gonzalez

unread,
Aug 27, 2010, 11:16:02 AM8/27/10
to professi...@googlegroups.com
I was totally backward in what I said we were doing at work. We are not favoring fPDF but tcPDF. Jason (sc_mach) would know, he's our resident PDF generating guru. :)

Jack Timmons

unread,
Aug 27, 2010, 3:02:27 PM8/27/10
to professi...@googlegroups.com
On Fri, Aug 27, 2010 at 10:09 AM, Cesar Ramos <cesar...@gmail.com> wrote:
> i used fpdf a long time ago for one of my projects and its ok although
> checking tcpdf and the html support it seems like a great addon for my apps
> going to try it

I'm going to stick with TCPDF also, although I might change the name
to lowercase. I hate caps.

On Fri, Aug 27, 2010 at 10:16 AM, Robert Gonzalez
<robert.anth...@gmail.com> wrote:
> I was totally backward in what I said we were doing at work. We are not
> favoring fPDF but tcPDF. Jason (sc_mach) would know, he's our resident PDF
> generating guru. :)

I'll post back what I create myself, but it's apparent I'm right in my thinking.

And when am I ever wrong?

Me being wrong is like AC/DC not singing about the values of being a
man. It just doesn't happen.

--
Jack Timmons
@_Codeacula

Gaurav Kumar

unread,
Aug 27, 2010, 3:31:20 PM8/27/10
to professi...@googlegroups.com

Hey Jack,

There have been number of packages floated around to generate pdf on the fly. How you use the package depends upon the architecture you are following, whether it is MVC using CakePHP, Zend etc.

I have used the following PDF generation package in number of projects, with whole lot of flexibility. This package generates PDF from HTML on the fly. Save it on disk or send pdf through email with features like inserting images in pdf, paging, etc.. 

http://www.unclecode.com/2009/10/mpdf-create-pdf-from-html-using-php-on-the-fly/


Thanks,

Gaurav Kumar

http://www.UncleCode.Com
Download Scripts, Resources For Free



--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.

For information or project assistance please visit :
http://www.360psg.com

You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP



--


Reply all
Reply to author
Forward
0 new messages