Solved

Is it possible to send pdf files to a printer using php?

Posted on 2009-04-01
4
1,837 Views
Last Modified: 2012-05-06
I have several pdf files in a folder on the web server. I want to make a script with php(server side) which list the files and send them to a printer. Listing the files is no problem. I tried using the printer_write functions but it seems not to work correctly, thats is: I get a lot of pages containing some ugly characters.
0
Comment
Question by:Dag-Egil
  • 2
  • 2
4 Comments
 
LVL 1

Author Comment

by:Dag-Egil
ID: 24038197
Sorry folks! I forgot one essential line of code:
printer_set_option($ph, PRINTER_MODE, 'RAW');
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24038293
Hello,

Direct printing of pdf documents is not possible using php's print_* functions.

You can convert pdf to bmp format (e.g. using Imagemagicks convert command: convert filename.pdf  filename.bmp) and print out bmp file using printer_draw_bmp()

Use unix system commands for printing: system('lpr -PNAMEOFPRINTER filename.pdf');

Regards.
0
 
LVL 1

Accepted Solution

by:
Dag-Egil earned 0 total points
ID: 24038322
Well, after I put in the missing line of code the script works for me. I am now able to list all pdf files in a folder and send the files to a printer. Se enclosed code snippet.
          // Open file and get file content
          $filenameTemp = $directory.'/utskrift/'.$FileName;
          if($filehandler = fopen($filenameTemp, 'rb')){
            $filecontent = fread($filehandler, filesize($filenameTemp));
            fclose($filehandler);
            
            // Print out the file content
            $printername = "HP Color LaserJet 4700 PCL 6";
            if($ph = printer_open($printername)){
              printer_set_option($ph, PRINTER_MODE, 'RAW');
              printer_write($ph, $filecontent);
              printer_close($ph);
              
              $strLogResult .= ' sendt til printer '.$printername;
              // Move the file to another directory
              if(rename($directory.'/utskrift/'.$FileName, $directory.'/arkivering/'.$FileName)){
                $strLogResult .= '. Filen er klar for arkivering.';
              } else {
                $strLogResult .= ' Klarte ikke å flytte filen. ';
              }
            } else {
              $strLogResult .= ' feilet, klarte ikke å åpne printer '.$printername;
            }
          } else {
            $strLogResult .= ' feilet, klarte ikke å åpne filen '.$filenameTemp;
          }

Open in new window

0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24038326
Hello,

for unix print command:

http://www.faqs.org/docs/Linux-HOWTO/Printing-Usage-HOWTO.html

Regards.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

830 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