Solved

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

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql update statement 3 22
paypal ipn url 5 57
Curl & PHP Command Help 4 23
How can I implement a "Select All" with this configuration...? 6 41
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 …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

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