Part of the EllisLab Network
   
1 of 4
1
Plugin to generate PDFs on the fly
Posted: 09 May 2006 06:48 AM   [ Ignore ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6573
Joined  03-23-2006

EDIT.  There is now a wiki page for this at http://www.codeigniter.com/wiki/PDF_generation_using_dompdf/

Well… its not so much of a plugin as a “how-to”.  I’ve needed to be able to generate PDFs on the fly for the application I’m building.  I’ve played with FPDF but found it the syntax to onerous to be practical (I want users to be able to control the look of their PDFs, and with FPDF they’d essentially need to learn a new language).  So then I tried html2fpdf to create them on the fly from HTML pages.  I was very, very disappointed with its lack of support for simple HTML.  I then found xhtml2pdf and html2ps... same thing.  Finally, I hit upon dompdf.  EXCELLENT!  Decent support for what I needed to do, and has decent (not great) support for many CSS styles and XHTML.

So here’s how to implement it in your own project.
1) Get a copy from http://www.digitaljunkies.ca/dompdf/
2) Uncompress it.  You’ll get a folder (I renamed it “dompdf”), and put the whole thing into your plugins folder, ie: /system/plugins/dompdf
3) Create the plugin.  I’ve named it “to_pdf_pi.php”

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function
pdf_create($html, $filename)
{
require_once("dompdf/dompdf_config.inc.php");

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($filename.".pdf");
}
?>


4) use it in your controllers like this

function pdf()
{
     $this
->load->plugin('to_pdf');
     
// page info here, db calls, etc.     
     
$html = $this->load->view('controller/viewfile', $data, true);
     
pdf_create($html, 'filename');
}


A few notes:
- It is PHP 5 only.  Sorry, I know that goes against what CI is doing here… but the level of support for CSS in the other projects wasn’t acceptable for my app
- You might find that there is limited or no support for a specific CSS style you want (ie: floating) and you’ll need to work around with old-school tables.  Ugh <shutter>..... Sorry for that…
- I in no way can claim credit for writing this fine code.  dompdf was released under an lgpl license, and you’ll obviously need to respect that in your apps if you choose to use it.

Derek

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 09 May 2006 07:15 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  886
Joined  03-06-2006

Sounds interesting. I have used fpdf with CI and it works fine, but as you say, its API is a whole ‘nuther markup engine.

I’ll give this one a try when I get a chance.  The website looks good.

 Signature 

Corozal, Belize | Linux.bz | Using Kubuntu Linux 7.10 | CodeIgniter 1.5.3

Profile
 
 
Posted: 10 May 2006 04:02 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  20
Joined  04-10-2006

it`s nice, but there is no utf-8…
anyone know some good class for converting from utf-8 to iso -xxx ?

Profile
 
 
Posted: 10 May 2006 04:16 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  886
Joined  03-06-2006

sebkom, if you find anything, let me know.  I just reprinted a bunch of reports that had Spanish names like José Magaña changed to weird characters.
:-(

 Signature 

Corozal, Belize | Linux.bz | Using Kubuntu Linux 7.10 | CodeIgniter 1.5.3

Profile
 
 
Posted: 11 May 2006 07:55 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  20
Joined  04-10-2006

I found!
you can check that:

http://freshmeat.net/projects/convertcharacterset/

It`s working for me

Profile
 
 
Posted: 11 May 2006 08:15 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  886
Joined  03-06-2006

Thanks, sebkom.  It looks like that may do the trick.

For anyone interested, you can download a PHP class at http://freshmeat.net/projects/convertcharacterset/ that is supposed to convert almost any charset to almost any other. Seems well documented. I haven’t tried it yet, but will when I get a chance.

 Signature 

Corozal, Belize | Linux.bz | Using Kubuntu Linux 7.10 | CodeIgniter 1.5.3

Profile
 
 
Posted: 11 May 2006 12:08 PM   [ Ignore ]   [ # 6 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6573
Joined  03-23-2006

It does indeed look good.  Thanks Sebkom. If you implement it, it would be great if you could share the code back.  I’ll get to it at some point down the road… but if you get it done first wink

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 11 May 2006 04:53 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  20
Joined  04-10-2006

I will try to do it with fpdi(clone of fpdf). Good news -> it is php4

Profile
 
 
Posted: 16 May 2006 04:48 PM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  20
Joined  04-10-2006

I`ve got good news - check that link:

http://codeigniter.com/forums/viewthread/45166/

Profile
 
 
Posted: 06 July 2006 09:18 AM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  317
Joined  04-25-2006

Well, now I need add this functionallity to my project. I read the article and understand almost everything here but where I the script to_pdf_pi.php need to be placed? Into system/plugins directory or where?

Regards,

 Signature 

ReynierPM | Joven Club de Computación y Electrónica Granma

Profile
 
 
Posted: 06 July 2006 12:28 PM   [ Ignore ]   [ # 10 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6573
Joined  03-23-2006

Yep, you got it Reynier.  Put it in /system/plugins

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 15 July 2006 03:17 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  18
Joined  05-19-2006

Thanks for the plugin, but it’s not working here… It keeps saying “Unable to stream pdf: headers already sent”.

Profile
 
 
Posted: 15 July 2006 11:00 PM   [ Ignore ]   [ # 12 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6573
Joined  03-23-2006

That happens when you’ve already sent content to the screen.  It could be a single line break or space.  Poke around these forums, as the issue has come up multiple times.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 16 July 2006 07:42 AM   [ Ignore ]   [ # 13 ]  
Summer Student
Total Posts:  18
Joined  05-19-2006

Thank you, Iwish, I’ve found one of these threads. As described, the problem really was a blank line after the closing php tag in the dompdf_pi.php file.

And Dallard, thank you, the plugin is great.

Profile
 
 
Posted: 17 July 2006 11:08 AM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  18
Joined  05-19-2006

Now, the infamous unicode problem. I’m trying to solve it, hope some of you can help me.

So, I have this dompdf_pi file. I´ve extracted the convertcharset package in the plugins directory, and changed my dompdf_pi file so it looked like this:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function
pdf_create($html, $filename)
{
require_once("dompdf/dompdf_config.inc.php");
require_once(
"convertcharset/ConvertCharset.class.php");

$dompdf = new DOMPDF();
$convertcharset = new ConvertCharset();
$html = $convertcharset->Convert($html, 'utf-8', 'iso-8859-1');
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($filename.".pdf");
}
?>

Well, it should work, because when I output the results to firefox the text is indeed converted. However, when outputing to the PDF file the text remains filled with weird characters… Am I missing something here?

Profile
 
 
Posted: 17 July 2006 11:12 AM   [ Ignore ]   [ # 15 ]  
Summer Student
Total Posts:  18
Joined  05-19-2006

Woo-hoo, it worked! I just had to change from PDFlib to CPDF! So, a small HOW-TO:

HOWTO: Generating PDF (dealing with unicode):

1- First, follow the steps to make DOMPDF work. I’ve named my to_pdf_ci.php dompdf_ci.php, so don’t be confused!

2- Download the convertcharset class from http://freshmeat.net/projects/convertcharacterset/

3- Extract it to plugins/convertcharset

4- Edit your dompdf_ci.php (or to_pdf_ci.php) so it looks like this:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function
pdf_create($html, $filename)
{
require_once("dompdf/dompdf_config.inc.php");
require_once(
"convertcharset/ConvertCharset.class.php");

$dompdf = new DOMPDF();
$convertcharset = new ConvertCharset();
$html = $convertcharset->Convert($html, 'utf-8', 'iso-8859-1');
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($filename.".pdf");
}
?>

5- In your plugins/dompdf/dompdf_config.inc.php, change from:

define("DOMPDF_PDF_BACKEND", "auto");

to:

define("DOMPDF_PDF_BACKEND", "CPDF");

Profile
 
 
   
1 of 4
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 62417 Total Logged-in Users: 42
Total Topics: 76654 Total Anonymous Users: 1
Total Replies: 414102 Total Guests: 467
Total Posts: 490756    
Members ( View Memberlist )
Newest Members:  Glikstasprugmandgil2004leiframseySpadXIIIharyAVcompleetyouknowwhord Limosinmitcha