• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2698
  • Last Modified:

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

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
Dag-Egil
Asked:
Dag-Egil
  • 2
  • 2
1 Solution
 
Dag-EgilAuthor Commented:
Sorry folks! I forgot one essential line of code:
printer_set_option($ph, PRINTER_MODE, 'RAW');
0
 
shobinsunCommented:
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
 
Dag-EgilAuthor Commented:
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
 
shobinsunCommented:
Hello,

for unix print command:

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

Regards.
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now