Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

printing pdf in php

Posted on 2011-02-28
7
Medium Priority
?
504 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 500 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

610 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