We help IT Professionals succeed at work.

HTML/PHP Code To Embed PDF Page

new435
new435 used Ask the Experts™
on
Hey guys, I am in a bit of a rut. I know how to write HTML code but I need to use PHP to connect to a MySQL Database, pull a PDF file from a table, and display it on a website. Can someone give me some advice as to how to go about doing this?

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
No browser will directly display PDF files yet, the user has to have the PDF reader plugin.  Do you know anything about PHP and MySQL?  If not, you are starting an educational project.  The PHP http://www.php.net and MySQL http://dev.mysql.com/doc/ have excellent doumentation.
Commented:
Is PHP the correct Language to use?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
If you already have PHP and MySQL, then yes.  Is the PDF already in the database or is there just a link to a file in the database?  If it's a link to a file, that pretty easy.  If the PDF itself is in the database, then you have to retrieve the data and create the appropriate headers to tell the browser that it is a PDF and then send the info to the browser.  It's a bit more complicated.

What do you know how to do so far?

Author

Commented:
Ive got an admin page built with a place to upload a PDF file to a MySQL database.

I know how to establish the connection to the database but Im unsure how to tell the page to pull the pdf and display it.

I'm pretty sure I have to do something like

header('Content-type: application/pdf');
readfile('pdffile.pdf');

Open in new window


but that's after i have some way to pull the file.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
That's one way to do it.  Most of us will recommend that you save the PDF in a directory and only put the URL in the database.  That way your PHP code only has to supply a URL /  link to the page for the client to download it.  It's a simpler method and requires less of the database and database connections.

Author

Commented:
What is the point of keeping the database then? Shouldn't the database be thrown out in that case?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Depends on what else you are using the database for.  If storing the PDF files is the only thing, then I wouldn't use the database at all.  If instead, you are associating user ids with the PDF files, then the database has a purpose that just files won't provide.  Then you can use the user id to lookup the PDF files for that user.

The biggest problem with storing files in a database is that it makes it much bigger and likely slower just simply because it requires more disk access.  To keep the response time of the database as quick as possible, you really want to have it deal with as little info as possible.  Frankly, most of the time it doesn't matter but if you expect the database to grow very large it can make a big difference.

Author

Commented:
figured out how to connect to the DB and select the file i needed.