Solved

printing pdf in php

Posted on 2011-02-28
7
498 Views
Last Modified: 2012-05-11
Hi:
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?
Thanks!
//<?php
//echo 'Invoice ' .$_POST['filename'] ;
//if($file== "http://localhost/".$_POST['filename'].".pdf")
//{
//header("Location: $file");
//exit();
//}

//?>
<html>
<head>

<title>Download a PDF file</title>

</head>
<body>
<?php
//**** This section opens the pdf
header("Cache-control: private");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=http://localhost//review.pdf");
readfile("http://localhost//review.pdf");
//**** pdf end
?>
</body>
</html>

Open in new window

0
Comment
Question by:ClaytonGlass
  • 3
  • 3
7 Comments
 
LVL 7

Expert Comment

by:szewkam
ID: 35004648
you have to use one of the libraries that allows you to manipulate pdf files. For example
http://www.php.net/manual/en/function.pdf-new.php 
Open your invoice and using method pdf_show_xy fill it with invoice information in right places
0
 
LVL 5

Expert Comment

by:Kendor
ID: 35004675
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:


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

if (file_exists($filename)) {
    header("Location: $file");
} else {
    print "The file $filename does not exist";
}
?>
0
 
LVL 5

Expert Comment

by:Kendor
ID: 35004712
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:
<?php

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

if (file_exists($filename)) {
  header('Content-type: application/pdf');
  header('Content-Disposition: attachment; filename="review.pdf"');
  readfile($filename);
} else {
    print "The invoice $filename does not exist";
 // here you can also output html and such or go back to thepage you came from
}
?>
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:ClaytonGlass
ID: 35004841
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?
0
 
LVL 5

Accepted Solution

by:
Kendor earned 125 total points
ID: 35004914
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)
0
 

Author Comment

by:ClaytonGlass
ID: 35004991
Kendor:
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!
0
 

Author Closing Comment

by:ClaytonGlass
ID: 35005907
Thanks!
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!
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question