printer_open()

Discussion in 'PHP' started by robg, Aug 19, 2008.

  1. #1
    Trying to print out a label directly from my app (localhost) to a printer on my computer. So far i've got this: It's not throwing up any errors but is not printing out. Could anyone shed any light on this. Thanks.

    
    function test_print()
            {            
                $pHandle = fopen("./pdf_print/test.pdf", "r");
                
                $handle = printer_open("DYMO LabelWriter 400");
                if(!$handle || $handle == NULL)
                {
                  die('Error connecting to Printer');
                }
                else
                {
                    printer_set_option($handle, PRINTER_MODE, "raw");
                    printer_write($handle,$pHandle);
                    printer_close($handle); 
                } 
               
            }
    
    Code (markup):
     
    robg, Aug 19, 2008 IP
  2. robg

    robg Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    printer_set_option($handle, PRINTER_MODE, "raw");

    taken this line out and something prints, but i get resource id#44 instead of the the pdf?
     
    robg, Aug 19, 2008 IP
  3. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Have you tried to make a simple test of the code?

    Just this:
    <?
    $handle = printer_open("DYMO LabelWriter 400");
    printer_write($handle,"Test label");
    printer_close($handle);
    ?>

    Does this work (I haven't tested it).
     
    elias_sorensen, Aug 19, 2008 IP
  4. robg

    robg Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    cheers elias.

    It seems to be finding the printer and printing, but it's not printing the file just the printing the error resource id #44.

    I did this to check file was ok.

    
    $pHandle = fopen("./pdf_print/test.pdf", "r+");
                if(!$pHandle || $pHandle == NULL)
                {
                    die('Cant find file');
                }
                $handle = printer_open("DYMO LabelWriter 400");
                if(!$handle || $handle == NULL)
                {
                  die('Error connecting to Printer');
                }
                else
                {
                    printer_write($handle,$pHandle);
                    printer_close($handle); 
                } 
    
    Code (markup):
     
    robg, Aug 19, 2008 IP
  5. Sevby

    Sevby Guest

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This can be easily explained. Take a look at the following lines.
    printer_write($handle,$pHandle);
    PHP:
    $pHandle = fopen("./pdf_print/test.pdf", "r+");
    PHP:
    As you can see, you forgot to read the file - you've only opened it. The following should work.
    printer_write($handle, fread($pHandle, filesize("./pdf_print/test.pdf")));
    PHP:
     
    Sevby, Aug 19, 2008 IP