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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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…
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…

705 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

20 Experts available now in Live!

Get 1:1 Help Now