Re: PDF pain
| From: | Richard Lynch | Date: | Sat, 20 Jul 2002 06:25:19 +0000 |
| Subject: | Re: PDF pain | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-108629@lists.php.net to get a copy of this message | ||
>I THINK I have installed pdflib and compiled it into PHP 4.2.1 OK.
>At least, configure/make/make install all went OK and it is trying to
>work.
You can confirm this FOR SURE with:
<?php phpinfo();?>
Be sure you have current PDF version.
>I copied the sample 'Hello World in PDF' script out of Programming PHP (p
>234) into a file named pdftest.pdf
Make sure that the:
header("Content-type: application/x-pdf");
is very near the bottom of the script, right before the pdf_close()
Otherwise, error messages from PHP are getting stuff into the PDF, and since
PHP error messages aren't PDF data, they really mess up the PDF file quite
badly...
>When I try to access http://localhost/pdftest.pdf, Konqueror
>tries to open
>a PDF file but then spits the dummy saying:
>
>"Could not open file
>blahblah.pdf which has type application/x-zerosize. KGhostview can only
>open Postscript and Portable Document Format files"
I'm seeing the word "zerosize" there, and wondering if maybe you need to do:
<?php
.
.
.
# End of script:
ob_start();
header("Content-type: application/x-pdf");
echo pdf_close();
$pdf = ob_get_contents();
header("Content-length: " . strlen($pdf));
ob_flush();
?>
This will allow to you sort of "snag" the PDF output, figure out how long it
is, tell the browser how long it is, and then send it for real.
http://php.net/ob_start
>So it looks like a header problem or something?
Could be lots of things, really...
Get the header() calls at the bottom so you can see any PHP error messages
in the browser, and you'll eliminate most of the possible problems.
>This raises a few other questions:
>
>- Should the PHP script be called pdftest.php or pdftest.pdf?
Yes. :-)
If you want to "fool" the maximum number of badly-broken browsers (IE)
you'll need "something.pdf" in the URL at the end of the "path" part.
One way is to just tack on a bogus "something.pdf", no matter how silly it
looks:
http://example.com/printable.php/something.pdf?file=whatever.pdf
Apache and PHP will pretty much ignore that extra crap 'something.pdf' in
there, but some versions of IE use the URL rather than the Content-type (MS
idiots!) to decide what kind of file they are getting.
Another way is to name your file pdftest.pdf, but then convince Apache that
it's really a PHP file with this in your ".htaccess" file:
<Files pdftest.pdf>
ForceType application/x-httpd-php
</Files>
>- Does httpd.conf need an AddType directive for pdf files? I've tried a
>few variations on "AddType application/x-pdf .pdf" etc
Yes, though it should already be getting that from the 'standard' mime-types
in that one doo-hickey file... I haven't read httpd.conf for a while, I
guess. Something about Magic Numbers maybe?
Adding a second one with the same settings might confuse Apache...
Upload a static PDF file, and see if it "works" If it does, don't mess with
this.
>- Should .pdf be added to the php AddType directive?
>eg. "AddType application/x-httpd-php .php .htm .pdf
I wouldn't do that, though -- You might some day want to have a static PDF
file on your web-server.
Using the <Files> directive above, you're telling Apache to *ONLY* force
that one single file to be PHP, even if it does end in .pdf, and you're
leaving all the other .pdf files (which really are PDF) alone.
>Basically, I'm confused. Anyone been there done that?
Yup.
http://uncommonground.com/events.htm
http://chatmusic.com/calendar.php
--
Like Music? http://l-i-e.com/artists.htm