Solved

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

Posted on 2009-04-01
4
1,716 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

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

24 Experts available now in Live!

Get 1:1 Help Now