Jump to content

Recommended Posts

Hi, I have coded a COM script that opens an InternetExplorer.Application (browser) object and navigates it to a page, I currently have it printing via the printer (php_printer.dll) library after a screen capture using imagegrabscreen but I can't get the document positioned correctly on the printer. I was wondering if anyone knows if you can use COM to instantiate a PrintDocument object and print the HWND of a browser object.

 

 

Here is my code:

 

 


<?php
  function print_img($doc,$img,$res,$pos_x,$pos_y)
    {
     $img_size = getimagesize($img);
     $img_resolution = $res;


     $img_width = $img_size[0];
     $img_height = $img_size[1];


     $ptr_resolution_x = printer_get_option($doc, PRINTER_RESOLUTION_X);
     $ptr_resolution_y = printer_get_option($doc, PRINTER_RESOLUTION_Y);


     $img_scale_x = $ptr_resolution_x / $img_resolution;
     $img_scale_y = $ptr_resolution_y / $img_resolution;


     $ptr_width = $img_width * $img_scale_x;                            
     $ptr_height = $img_height * $img_scale_y;
  
     printer_draw_bmp($doc,$img,$pos_x,$pos_y,$ptr_width,$ptr_height);
    }


$browser = new COM('InternetExplorer.Application');
//$printer = new COM('PrintDocument.Application');


$handle = $browser->HWND;
$browser->Navigate('http://www.phantom.uk.net');
$browser->Visible        = true;
$browser->FullScreen     = true;
$browser->TheaterMode    = true;
$browser->ToolBar        = false;
$browser->MenuBar        = false;
$browser->AddressBar     = false;
$browser->StatusBar      = false;


while ( $browser->ReadyState != 4 )
{
    com_message_pump(1000);
}


$img = uniqid('img_');
$png = $img . '.png';
$bmp = $img . '.bmp';
sleep(3);
$im = imagegrabscreen();//($handle, 0);
imagepng($im, $png);
$browser->Quit();


`convert {$png} -resize 500% {$bmp}`;/*
    $ph = printer_open('Dell Color Laser 1320c');
        printer_start_doc($ph, "Test Document");
        printer_start_page($ph);
            print_img($ph, $bmp, 96, 0, 0);
        printer_end_page($ph);
        printer_end_doc($ph);
    printer_close($ph);*/
    
    //unlink($png);
    //unlink($bmp);
    die('Done!');


?>

 

 

Any help is appreciated.

 

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/238211-need-help-with-com/
Share on other sites

Still no luck with this, anyone have any idea if it's possible to implement a PrintDocument COM object using PHP? Ideally I want to pass the printing responsibility to the InternetExplorer.Application object and override the alertbox somehow. Perhaps there is a way to create a keypress event for CTRL + P? An ActiveX solution?

 

 

This is all new to me and I hate COM already lol -_- I wish there was an idiot proof manual for PHP COM objects :/

 

 

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/238211-need-help-with-com/#findComment-1224239
Share on other sites

OK I managed to get this working, here is my code:

 

 


<?php
try
{
   $browser = new COM('InternetExplorer.Application');
   $handle  = $browser->HWND;
   $browser->Navigate('http://www.phantom.uk.net');
   $browser->Visible        = false;
   $browser->FullScreen     = true;
   $browser->TheaterMode    = true;
   $browser->ToolBar        = false;
   $browser->MenuBar        = false;
   $browser->AddressBar     = false;
   $browser->StatusBar      = false;


   while ( $browser->ReadyState != 4 )
   {
       com_message_pump(1000);
   }
   sleep(5);
   $browser->ExecWB(6,2); // print document, dont prompt user.
   sleep(5);
   $browser->Quit();
}
catch (com_exception $e)
{
   echo $e->getMessage();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/238211-need-help-with-com/#findComment-1224548
Share on other sites

I commented the code, in case anyone wants to use it. It is intended to be used as an AJAX request, will successfully print a document to a windows servers default printer. It requires the CURL extension and you may need to change WAMP and

Browser configuration utility services settings allowing them to interact with desktop (im not 100% sure about this tho.


<?php
/*
#####################################################################################
    ***-! DRAWBACKS    !-***
    WILL ONLY RUN ON A WINDOWS SERVER (USES COM),
    CAN ONLY PRINT TO ONE PRINTER PER SERVER (DEFAULT PRINTER)


#####################################################################################
    ***-! REQUIREMENTS !-***
    ENABLE CURL IN PHP EXTENSIONS,
    SET MAX_EXECUTION_TIME TO 300 IN PHP.INI,
    CHANGE PAGE SETUP SETTINGS IN IE (CHECK PRINT BACKGROUND AND IMAGES),
    SET DEFAULT PRINTER AS REQUIRED,
    MUST RUN ON WINDOWS SERVER.


#####################################################################################
    ***-! USAGE !-***
    script_name.php?url=url_to_print&response_type=text
    
    @url           -required ( the url of the page you wish to print );
    @response_type -optional ( if set as text you will get a textual response, otherwise integer response );


#####################################################################################
    ***-! RESPONSE !-***
    INTEGER
            1 - Means no url was set in the query string;
            2 - Means a curl request on the url returned a 404;
            3 - Means the document printed successfully.
    TEXT
            1 = No url set in query string.;
            2 = {$url} returned a 404 HTTP error.;
            3 = {$url} was printed successfully.;


#####################################################################################
*/
function defines($url, $response = false)
{
    if ( strtolower($response) == 'text' )
    {
        define('NO_URL_SET',       'No url set in query string.');
        define('URL_RETURNED_404', stripslashes(htmlentities($url, ENT_QUOTES)) . ' returned a 404 HTTP error.');
        define('PRINT_SUCCESS',    stripslashes(htmlentities($url, ENT_QUOTES)) . ' was printed successfully.');
    }
    else
    {
        define('NO_URL_SET',       1);
        define('URL_RETURNED_404', 2);
        define('PRINT_SUCCESS',    3);
    }
}


function check_404($url)
{
    $return = true;
    $handle = curl_init($url);
    curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);


    /* Get the HTML or whatever is linked in $url. */
    $response = curl_exec($handle);


    /* Check for 404 (file not found). */
    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    if($httpCode == 0) {
        $return = false;
    }


    curl_close($handle);
    
    return $return;
}
    
try
{
    $url      = (isset($_GET['url']) && !empty($_GET['url'])) ? $_GET['url']            : false;
    $response = isset($_GET['response_type'])                 ? $_GET['response_type']  : false;
    //set defines
    defines($url, $response);
    
    if ( !$url )
    {
        throw new Exception(NO_URL_SET);
    }


    if ( substr($url, 0, 7) !== 'http://' )
        $url = 'http://' . $url;
    //curl request to check if page exists
    if ( !check_404($url) )
        throw new Exception(URL_RETURNED_404);


    //instantiate COM object (Internet Explorer Window)
    $browser = new COM('InternetExplorer.Application');
    //Get handle window
    $handle  = $browser->HWND;
    //navigate to $url
    $browser->Navigate($url);
    $browser->Visible        = false;
    $browser->FullScreen     = true;
    $browser->TheaterMode    = true;
    $browser->ToolBar        = false;
    $browser->MenuBar        = false;
    $browser->AddressBar     = false;
    $browser->StatusBar      = false;
    
    //until page is loaded fully sleep for 1 second
    while ( $browser->ReadyState != 4 )
    {
        com_message_pump(1000);
    }
    sleep(3);
    //print the webpage in the IE window
    $browser->ExecWB(6,2);
    sleep(3);
    $browser->Quit();
    
    throw new Exception(PRINT_SUCCESS);
    
}
catch (com_exception $e)
{
    echo $e->getMessage();
}
catch(Exception $e)
{
    echo $e->getMessage();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/238211-need-help-with-com/#findComment-1224570
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.