?
Solved

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

Posted on 2009-04-01
4
Medium Priority
?
2,111 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
[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
  • 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.
Suggested Courses

777 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