yasirsiddiqui / php-google-cloud-print

PHP class to print documents using Google Cloud Print with OAuth2 authorization
91 stars 42 forks source link

How do i set paper size? #16

Closed vinaymcp closed 4 years ago

vinaymcp commented 9 years ago

I am having hard time to set paper size using api, can you please tell me how to do it ?

I wanted to know which file i need to modify in order to pass the parameters as discussed in another thread ?

yasirsiddiqui commented 9 years ago

You need to modify function sendPrintToPrinter in GoogleCloudPrint.php most probably $post_fields array at line # 184

vinaymcp commented 9 years ago

I tried adding 'media_size' => "100",

and

$ps = '"{"media_size": "1","fit_to_page":"100"}"'; 'PrinterDescriptionSection' => $ps

nothing seems to increase the size its just printing small on a POS printer since by default its taking A4 size, not sure what is the way out. Pleas share some light.

Thanks

yasirsiddiqui commented 9 years ago

Well i think first you have to get all the fields of specified printer using /printer api https://developers.google.com/cloud-print/docs/appInterfaces#printer then in response you will get all fields of the printer. Share the api result here then we can see what parameter should be used to set paper size?

der-lukas commented 9 years ago

Paste this function in "GoogleCloudPrint.php":

public function getPrinterInfo($printerid, $use_cdd = false) {

    // Check if we have auth token
    if(empty($this->authtoken)) {
        // We don't have auth token so throw exception
        throw new Exception("Please first login to Google");
    }

    // Prepare auth headers with auth token
    $authheaders = array(
    "Authorization: Bearer " .$this->authtoken
    );

    $post_fields = array(
        'printerid' => $printerid,
        'use_cdd' => $use_cdd   
    );

    $this->httpRequest->setUrl(self::PRINTER_INFO_URL);
    $this->httpRequest->setPostData($post_fields);
    $this->httpRequest->setHeaders($authheaders);
    $this->httpRequest->send();
    $responsedata = $this->httpRequest->getResponse();
    // Make Http call to get printers added by user to Google Cloud Print
    $printer_infos = json_decode($responsedata);
    // Check if we have printers?
    if(is_null($printer_infos)) {
        // We dont have printers so return blank array
        return "OH OH! Nothing here!";
    }
    else {
        // We have printers so returns printers as array
        return $this->arrayCastRecursive($printer_infos);
    }

}

Also paste this function:

private function arrayCastRecursive($array)
{
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $array[$key] = $this->arrayCastRecursive($value);
            }
            if ($value instanceof stdClass) {
                $array[$key] = $this->arrayCastRecursive((array)$value);
            }
        }
    }
    if ($array instanceof stdClass) {
        return $this->arrayCastRecursive((array)$array);
    }
    return $array;
}

This function just gives the output data some order!

And then just call the function:

$printerinfo = $gcp->getPrinterInfo($printerid, false);
print_r($printerinfo);
vinaymcp commented 9 years ago

This is the ouput what i get, if i take a manual print then this settings work fine for me.

"custom_display_name": "LT 8.5 x 11 in",

Not sure how do i configure it using api.

{ "success": true, "xsrf_token": "AIp06Dh4Ni67BJRofErAsUpNgzsxWhkXZA:1439303217821", "request": { "time": "0", "users": [ "bombaypanjim@gmail.com" ], "params": { "extra_fields": [ "connectionStatus,semanticState,uiState,queuedJobsCount" ], "use_cdd": [ "true" ], "input_type": [ "" ], "printerid": [ "3e9b3611-5c2c-79c7-5e4f-1593afac329c" ] }, "user": "bombaypanjim@gmail.com" }, "printers": [ { "tags": [ "^recent", "^own", "^connector", "^can_share", "^can_update", "^can_delete", "cpchrome_version\u003d44.0.2403.130 m", "cpprinter-location\u003d", "cpprinter-make-and-model\u003dEPSON TM-T81II Receipt5", "cpsystem_driverinfo\u003dEPSON TM-T81II Receipt5;6.1.7600.16385 (win7_rtm.090713-1255);Microsoft\u00ae Windows\u00ae Operating System;6.1.7600.16385", "cpsystem_name\u003dWindows NT", "cpsystem_version\u003d6.1", "cptagshash\u003d50448389b719b93f93e17dbae806decd" ], "createTime": "1437205417593", "ownerName": "dhiraj S.m d", "uiState": { "summary": "IDLE", "severity": "NONE" }, "accessTime": "1439007570013", "updateTime": "1439297883490", "status": "4096", "ownerId": "bombaypanjim@gmail.com", "gcpVersion": "1.0", "capsHash": "99300b94a9a628eee4a6d3abc5b39ac9", "semanticState": { "cloud_connection_state": "ONLINE", "version": "1.0" }, "isTosAccepted": false, "access": [ { "scope": "bombaypanjim@gmail.com", "membership": "MANAGER", "email": "bombaypanjim@gmail.com", "name": "dhiraj S.m d", "role": "OWNER", "type": "USER" } ], "type": "GOOGLE", "id": "3e9b3611-5c2c-79c7-5e4f-1593afac329c", "connectionStatus": "ONLINE", "description": "", "proxy": "C3FAED0F-886C-45F9-AAF6-5D045C6D1DD2", "name": "EPSON TM-T81II Receipt", "defaultDisplayName": "", "local_settings": { "current": { "xmpp_timeout_value": 300 } }, "queuedJobsCount": 0, "capabilities": { "printer": { "supported_content_type": [ { "content_type": "application/pdf" } ], "color": { "option": [ { "is_default": true, "type": "STANDARD_MONOCHROME", "vendor_id": "1" } ] }, "media_size": { "option": [ { "custom_display_name": "A4 210 x 297 mm", "height_microns": 297000, "width_microns": 210000, "name": "ISO_A4", "vendor_id": "119" }, { "custom_display_name": "LT 8.5 x 11 in", "height_microns": 279400, "width_microns": 215900, "name": "NA_LETTER", "vendor_id": "120" }, { "custom_display_name": "Postcard 100 x 148 mm", "height_microns": 148000, "width_microns": 100000, "name": "JPN_HAGAKI", "vendor_id": "121" }, { "custom_display_name": "Roll Paper 58 x 297 mm", "height_microns": 297000, "width_microns": 58000, "vendor_id": "122" }, { "custom_display_name": "Roll Paper 58 x 3276 mm", "height_microns": 3276000, "width_microns": 58000, "vendor_id": "123" }, { "custom_display_name": "Roll Paper 80 x 297 mm", "height_microns": 297000, "width_microns": 80000, "is_default": true, "vendor_id": "124" }, { "custom_display_name": "Roll Paper 80 x 3276 mm", "height_microns": 3276000, "width_microns": 80000, "vendor_id": "125" } ] }, "page_orientation": { "option": [ { "is_default": true, "type": "PORTRAIT" }, { "type": "LANDSCAPE" }, { "type": "AUTO" } ] }, "dpi": { "option": [ { "vertical_dpi": 203, "is_default": true, "horizontal_dpi": 203 } ] } }, "version": "1.0" }, "displayName": "EPSON TM-T81II Receipt" } ] }

der-lukas commented 9 years ago

These are my settings for printing on photo-paper...

$printSettings = '{
        "version": "1.0",
        "print": {
            "page_orientation": { 
                "type":"LANDSCAPE",
            },
            "margins":{
                "top_microns": 0,
                "right_microns": 0, 
                "bottom_microns": 0, 
                "left_microns": 0
            },
            "copies":{
                "copies":1
            },
            "dpi": {
                "vertical_dpi":300,
                "horizontal_dpi":300,
                "vendor_id":"cijns:High"
            },
            "fit_to_page": {
                "type": 1 
            },
            "media_size": {
                "height_microns":152400,
                "width_microns":101600,
                "is_continuous_feed":false,
                "vendor_id":"psk:NorthAmerica4x6",
            },
        }
    }';

So for using "LT 8.5 x 11 in" use this:

$printSettings = '{
            "media_size": {
                                    "custom_display_name": "LT 8.5 x 11 in",
                                    "height_microns": 279400,
                                    "width_microns": 215900,
                                    "name": "NA_LETTER",
                                    "vendor_id": "120",
            },
        }
    }';

Look at the other settings I set for seeing other settings as landscape etc...

This is how you send the settings correctly:

$post_fields = array(
            'printerid' => $printerid,
            'title' => $printjobtitle,
            'ticket' => $printSettings,
            'contentTransferEncoding' => 'base64',
            'content' => base64_encode($contents), // encode file content as base64
            'contentType' => $contenttype   
        );

$this->httpRequest->setPostData($post_fields);
yasirsiddiqui commented 9 years ago

@vinaymcp so you can now send any of the settings your printer supports like

$printSettings = '{ "media_size": { "custom_display_name": "Roll Paper 80 x 3276 mm", "height_microns": 3276000, "width_microns": 80000, "vendor_id": "125" }, } }';

$this->httpRequest->setPostData($post_fields);

der-lukas commented 9 years ago

I just found out what the best way for getting the perfect ticket options is:

1.) Got to Google Cloud Print, where you see your Print-Jobs. 2.) On the left there's the "Print"-Button, where you can make a direct upload and send a print-job, with all the settings you can make for the printer. 3.) (At least when you're in Chrome) you can see your submitted print-job in the "Network"-Tab in your Delevoper-Console (something-like "submit?printerid...") and when you look inside that object, you can find the ticket you've just sent (request > params > ticket), which you can copy directly and use in your config! ;)

yasirsiddiqui commented 9 years ago

@der-lukas How can i see object details when i send print using Print button in Google Cloud Print web console? Can you please share screen shots? I am uploading my screens but can't find where to get object details as you mentioned in step 3. screen8 screen7

der-lukas commented 9 years ago

At first I make all the adjustments and then press Print ("Drucken" in my case)

screenshot 2015-08-12 13 06 58

Then in Chrome Developer Console I choose the tab "Network" and look for the "submit"-request!

screenshot 2015-08-12 14 32 38

yasirsiddiqui commented 9 years ago

This is great thanks for sharing this.

jmg460 commented 8 years ago

This is fantastic code! The remote print capability was just what I needed. Thank you!

I was wondering if there is a way to send vendor capabilities through sendPrintToPrinter(). I'm searching for a way to print borderless images. When I change the margins to zero, there is still about an 1/8th inch border on the edges of the page. My printer has the capability to print borderless (full bleed), but I'm unable to get it working despite many variations. I've even tried changing the default printer settings in C.U.P.S.. There isn't a lot of info on the web about this particular usage/application, so I thought I'd ask the experts!

Here is a screenshot of my printer's capabilities with the borderless options highlighted. screen shot 2016-02-24 at 8 52 08 am

I've tried many variations of this: "page_borderless": { "type":"0" },

Do you have any idea if this will work (provided I have the right code), or if full bleed borderless printing is possible through Google Cloud Print?

Thank you!

yasirsiddiqui commented 8 years ago

@jmg460 can you follow @der-lukas comment https://github.com/yasirsiddiqui/php-google-cloud-print/issues/16#issuecomment-130263014 and see if that helps

jmg460 commented 8 years ago

@yasirsiddiqui -- Thank you for the quick response.

I've picked apart the developer console code and seemingly found what I needed, but copying that code didn't seem to work. I kept getting a ticket error response from GCP.

der-lukas commented 8 years ago

@jmg460 But can you achieve the right output by making the setting via the GCP interface?

jmg460 commented 8 years ago

@der-lukas I can't get the right output through the GCP interface. I had tried, but I didn't check every combination until a few moments ago. Even though the advanced borderless option appears to be available, it doesn't work no matter the page fit option or border margin combination I choose. That makes me think this isn't possible, at least for now...

Thank you for the quick response! This is my first experience on GitHub. I will definitely stick around.

screen shot 2016-02-24 at 9 23 50 am

danirub commented 7 years ago

thanks for the help here. but in the code how can I set the parameters off the page size?

An error occured while printing the doc. Error code:424 Message:Failed to parse the print job's print ticket. thanks Dani

mikesteiner99 commented 7 years ago

WOW !!! Vielen Dank Lukas!!! . Nach 8 Stunden ärgern, diese Lösung zu finden ist wunderbar.

der-lukas commented 7 years ago

@mikesteiner99 Freut mich! Hab mich damals auch sehr lang herumgeärgert, bis ich den Trick gefunden habe! ;)

ebofi commented 5 years ago

@vinaymcp so you can now send any of the settings your printer supports like

$printSettings = '{ "media_size": { "custom_display_name": "Roll Paper 80 x 3276 mm", "height_microns": 3276000, "width_microns": 80000, "vendor_id": "125" }, } }';

$this->httpRequest->setPostData($post_fields); @yasirsiddiqui I need to specify the media settings, how can i add this to current version of GCP