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

Posted on 2009-04-01
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.
Question by:Dag-Egil
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

Author Comment

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

Expert Comment

ID: 24038293

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');


Accepted Solution

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));
            // 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);
              $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

LVL 14

Expert Comment

ID: 24038326

for unix print command:


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Title # Comments Views Activity
curl parse data from site 20 64
MySQL-Design Help 12 66
Echo values after a query in php 5 50
php mail() send emails but the message is lost 4 33
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 count occurrences of each item in an array.

732 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