Generate PDF files with PHP
Posted: Nov 11, 05, 7:32 MatthewHSE
Joined: 20 Jul 2004
Location: Central Illinois, typically glued to a computer screen
I'm working on an application to pull data from either a text file or a database, format it, and write it to a PDF file. Ideally, the script will then e-mail the PDF file to me, but I haven't gotten that far yet.
Actually I haven't even gotten to first base; it looks like my host doesn't support the PHP functions necessary to generate PDF files. I ran the following PHP script that I got from devshed:
:: Code ::<?php
// create handle for new PDF document
$pdf = pdf_new();
// open a file
// start a new page (A4)
pdf_begin_page($pdf, 595, 842);
// get and use a font object
$arial = pdf_findfont($pdf, "Arial", "host", 1); pdf_setfont($pdf, $arial, 10);
// print text
pdf_show_xy($pdf, "There are more things in heaven and earth, Horatio,",50, 750);
pdf_show_xy($pdf, "than are dreamt of in your philosophy", 50,730);
// end page
// close and save file
Simple enough, I can follow that. The problem is when I try to run the script. It throws the following error:
:: Quote ::Fatal error: Call to undefined function: pdf_new() in /home/path/to/pdf.php on line 3
Obviously the server isn't configured to allow access to that function.
I guess I have three main questions:
1.) Is there a workaround I can use without my host changing any server settings?
2.) Do most hosts allow access to the PDF functions? I've tried two with the exact same error.
3.) Is there any reason for a host not to support those functions?
I really need to be able to generate this PDF file; it will make certain aspects of my job so much easier. Hopefully I can get my host to turn on those options, otherwise I'll need to find a workaround of some sort. Maybe a carefully-formatted HTML file would work.
Back to top
Hi MatthewHSE. Here's a good PHP pdf generation tutorial to get us started.
Let me run through your questions:
:: Quote ::1.) Is there a workaround I can use without my host changing any server settings?
Possibly, see below. this type of PHP PDF is an external module which must be installed. You can check to see if this module is available by creating a file with only this content:
:: Code ::<?php
Save it as info.php, upload it to root, run it. This will show you what your webserver offers you.
When you run it, search the displayed page for 'pdf'. If you have pdf support, you will see something like this:
:: Code ::pdf
PDF Support enabled
PDFlib GmbH Version 4.0.1
Revision $Revision: 18.104.22.168 $
It will be prettier of course...
If no PDF module is installed that's the end of the road.
:: Quote ::2.) Do most hosts allow access to the PDF functions? I've tried two with the exact same error.
You may or may not have noticed that we here at techpatterns.com are somewhat fanatical Pair Networks fans. There is a reason for this. Several years ago I got sick of hitting development roadblocks like you are hitting right now. Things that should work diddn't on hoster x or y.
Pair currently has one significant problem, they haven't upgraded to php 5 yet. I know why, but they will need to do this soon I think, even though it's a nightmare of support as all those script kiddie php scripts break left and right due to bad/deprecated global variable useage.
:: Quote ::3.) Is there any reason for a host not to support those functions?
Yes, they aren't good enough. They can't handle the security concerns. Their techs aren't paid enough to keep up on different components. Their servers aren't good enough.
This is why I very happily pay for rock solid servers where everything I need to work works.
So I uploaded the script, it runs, but with an error.
Back to the real question
This devshed thread gives a few pdf options, none of these are tested or reviewed by me:
The PDF script
I tried running it, but came across some gotchas right away. First, you have to create a folder with write/read permissions  to give the script somewhere to write to. Then I ran into this problem:
:: Code ::Fatal error: PDFlib error: Resource configuration file 'pdflib.upr' not found in ....
From php.net, set font. You'll find lots of fun stuff there too. I got your thing running upto the font thing, I quick support email to the hoster would probably answer the question, or a little more research.
this is the font library, you either have to make this, or it's somewhere on the server, I don't know the answer to that one.
But the main thing, the stuff worked on the Pair server, because the server has all the tools loaded on it. Then I could begin debugging thhe actual script instead of spending time worrying about how to get the hoster to support what I wanted to do.
Back to top
All times are GMT - 8 Hours