Search code examples
javaandroidprintinggoogle-cloud-print

Google Cloud Print integration without webview


I use this tutorial to integrate my android application with Google Cloud Printer. It works fine, but I want do this without WebView.

I want to print with two steps:

  1. Generate pdf file
  2. Click button "Print" and submit print job

I shared my printer to anyone with link and want to print all pdf files from my application to my shared printer.

Can you share some code or tutorial?

Sorry for my english


Solution

  • I found this link and it helps me

    ADD:

    @Override
    protected Void doInBackground(Void... params) {
    
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
    
            String user = "[email protected]";
            String pass = "password";
            String source = "Cloud%20Printing%20Test";
    
            HttpGet authGet = new HttpGet(
                    "https://www.google.com/accounts/ClientLogin?accountType=HOSTED_OR_GOOGLE&Email="
                            + user
                            + "&Passwd="
                            + pass
                            + "&service=cloudprint&source=" + source);
    
            HttpResponse httpResponse;
    
            httpResponse = httpclient.execute(authGet);
    
            String authResponse = EntityUtils
                    .toString(httpResponse.getEntity());
            String authKey = authResponse.substring(authResponse
                    .indexOf("Auth=") + 5);
            authKey = authKey.replace("\n", "");
    
            MyLog.d(TAG, "Auth key: " + authKey);
    
            HttpPost printPost = new HttpPost(
                    "https://www.google.com/cloudprint/submit?output=json");
            printPost.setHeader("Authorization", "GoogleLogin auth=" + authKey);
            printPost.setHeader("X-CloudPrint-Proxy", source);
    
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("printerid", "ID"));
            nameValuePairs.add(new BasicNameValuePair("title", "TEST"));
            nameValuePairs.add(new BasicNameValuePair("capabilities", "{capabilities=[]}"));
            nameValuePairs.add(new BasicNameValuePair("content", "123"));
            nameValuePairs.add(new BasicNameValuePair("contentType", "text/plain"));
            printPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
            HttpResponse printResponse = httpclient.execute(printPost);
            String lol = EntityUtils.toString(printResponse.getEntity());
    
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    But now I can print only text. If i found solution how to print pdf - I'll post code here

    ADD2:

    This code send pdf files to print

    File file = new File("file.pdf");
    FileBody fb = new FileBody(file);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addTextBody("printerid", "ID");
    builder.addTextBody("title", "TEST2");
    builder.addTextBody("capabilities", "{capabilities=[]}");
    builder.addTextBody("contentType", "application/pdf");
    builder.addPart("content", fb);
    printPost.setEntity(builder.build());