Solved

printing pdf in php

Posted on 2011-02-28
7
496 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP extract HTML using DOMXpath from nested tables 8 47
PHP7 and Sql Server Windows 2008 R2 13 85
PHP_POST() error message 9 40
WordPress syntax 2 25
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now