Generating A Pdf

Hello everyone,

how can i generate a pdf with php in yii?

Is there any valid extension (i’ve just found some wrappers)?



I’ve had a great experience with pdf extension (I’m using it with mPDF)

I could do everything I needed:

  • writing to a PDF file directly from the controller

  • converting a view to a PDF file

  • using a base PDF template (e.g. a letterhead…)

  • use full CSS files

  • use inline CSS (in the view or controller)

  • use divs or tables

In my case (2 different webapps with several PDF reports and outputs), I’ve overcome limitations quite easily, and mPDF documentation is great with lots of samples.

he following C# class method shows how to create a basic PDF document in C#.NET project.

public static void SaveDocumentFile(REDocument document, string filePath);

public static void SaveDocumentFile(BaseImage image, string filePath, BaseEncoder enc);

public static void SaveDocumentFile(List<BaseImage> images, string filePath, BaseEncoder enc);

public static void SaveDocumentFile(REDocument document, string filePath, BaseEncoder enc);

I have tried various extensions with not much luck.

In the end I installed ‘wkhtmltopdf’ (Google it) and then wrote a PHP exec() call to use it - so no Yii specific stuff. It works well on Linux and Windows and you can configure the page orientation, headers/footers, margins, etc. I had to play around to get it to look correct, but it works well for me and my system uses it to dynamically generate reports.


If you can install wkhtmltopdf you can try my pdfable extension. It requires the wkhtmltopdf binary to be installed on the server, though.

Currently I’m using yii-pdf which is just a wrapper for mPDF. I needed something written entirely in PHP because I couldn’t run my own binaries on a shared hosting platform.

mPDF was tricky to get started but it did the job very well. I was suprised how many features it actually provides. It is slow though.

Anyway, I did an extensive research on the topic and if you really need to use HTML as templates to create PDF there is no perfect solution, because HTML itself is not fitted for printing. There are troubles with page breaking, footers and headers etc. That’s why mPDF implements a few extensions to HTML.

I’ve been using FPDF for many years, and I’m very happy with it.

A wrapper is not necessary.

Just to note, FPDF requires to use an API to make PDFs directly. Others like mPDF lets you convert HTML straight into PDF. This is slower, but often much easier AND you could get away with a wysiwyg editor.