printing pdf in php

I have a sales archive in mysql; interrogated by php. I also have pdfs of every invoice. I would like the user to enter the invoice number and display the pdf. I have got bits of the solution, but not all.
The  attached code shows what I have. The top section (commented out) shows how I can call the invoice number('filename') from the invoice.php page. This works
The second section calls a predetermined pdf file ('review.pdf'). This works, too.
What I need to do is replace the predetermined part with the $_POST part; but I am completely lost!
Does anyone know how I can do this?
//echo 'Invoice ' .$_POST['filename'] ;
//if($file== "http://localhost/".$_POST['filename'].".pdf")
//header("Location: $file");


<title>Download a PDF file</title>

//**** This section opens the pdf
header("Cache-control: private");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=http://localhost//review.pdf");
//**** pdf end

Open in new window

Richard TeasdaleFinancial ControllerAsked:
Who is Participating?
KendorConnect With a Mentor Commented:
ah - there might be minor bugs in that code... I couldn't test it but i think there's need for double slashes...

you say you can access: http://localhost//review.pdf? and the invoice files are in the exact same directory? meaning you can also access: http://localhost//invoice1.pdf?

check which directory your php file is and use either absolute or relative paths to access the files.

and there I see that if there is already a file called review.pdf you might get problems when "creating" a new one in the php script on the line:  header('Content-Disposition: attachment; filename="review.pdf"');
so try to rename one of those - you could use:  header('Content-Disposition: attachment; filename="your-review.pdf"');

the code should then actually open the pdf (or at least the browser should give you the choice: open or save)
you have to use one of the libraries that allows you to manipulate pdf files. For example 
Open your invoice and using method pdf_show_xy fill it with invoice information in right places
i think you should not output anything before you call header()
so try without all the html code and only use the php part.

something like this:

$filename = "http://localhost/".$_POST['filename'].".pdf";

if (file_exists($filename)) {
    header("Location: $file");
} else {
    print "The file $filename does not exist";
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

ah, maybe I got you wrong. Let me ask for clarification:
- you already have those PDFs, they are not needed to be created using PHP? if no -> use PHP PDFlib
- the invoices are stored under invoicexxx.pdf and you want them to be outputed as review.pdf?

try this:

$filename = "http://localhost/".$_POST['filename'].".pdf";

if (file_exists($filename)) {
  header('Content-type: application/pdf');
  header('Content-Disposition: attachment; filename="review.pdf"');
} else {
    print "The invoice $filename does not exist";
 // here you can also output html and such or go back to thepage you came from
Richard TeasdaleFinancial ControllerAuthor Commented:
Thanks, szewkam, but I think that site refers to creating pdfs. Kendor; thank you. This looks like it will work - except I keep getting 'the file does not exist'. This is Linux and I was wondering if I need to specify a directory? The test file is in the same directory as the php files; should I point elsewhere? I have renamed the file to make sure there are no stray blanks.
And assuming I do find the file, will you code actually display the pdf? Or do I need to incorporate some other code to specifically display the pdf?
Thanks to you both; I would be very grateful if you could suggest where I am going wrong now?
Richard TeasdaleFinancial ControllerAuthor Commented:
Our last postings crossed. Sorry if there is any confusion!
The invoices are stored as pdfs. The name of each pdf is the same as the invoice number. example - 123456.pdf.
I want the user to type '123456' into a text box and that pdf to be displayed. Not as review.pdf, but as itself.
The code I submitted was very clumsy! I am a complete novice at php so wrote the first part to prove to myself that $_POST did what I wanted it to do - in this case the text '12346' comes through as an array of some kind.
The second part was an attempt to  print ANY pdf - in this case a specific document called 'review.pdf''. This part of the code worked also - displaying 'review.pdf'.
What I cannot do is replace the fixed content of 'review.pdf' with the array. I think you have definitely got me in the right direction; I just fail on 'the file 'http://localhost/123456.pdf cannot be found' - when it is in the same directory asthe php files!
Thanks for your patience!
Richard TeasdaleFinancial ControllerAuthor Commented:
If  I took out the if statement it works!
I also changed the filename to $filename
The result is a little awkward - chrome manages better than ie and firefox - but a result, thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.