2

I wrote a script that prints some test pages from url on Web-site, and every time I press a print button, a dialog frame for choosing printer appears . But I want to avoid this because my account synchronized with printer.

window.onload = function() {
var gadget = new cloudprint.Gadget();
gadget.setPrintButton(
    cloudprint.Gadget.createDefaultPrintButton("print_button_container")); // div id to contain the button
    gadget.setPrintDocument("url", "Test Page", "https://www.google.com/landing/cloudprint/testpage.pdf");
}
4

1 に答える 1

0

これを実現するには、ガジェットではなく宣誓と html ボタンを使用できます。これには、Google 開発者コンソールを使用して oauth 権限を取得する必要があります。

次に、クラウド プリント サービスを承認する必要があります。

次の一連の関数は、Google Apps Scripts での使用に特に適していますが、適応させることができます。最初に行うことは、クラウド プリント サービスを認証するためにアクセスできる URL リンクをログに記録することです。

function showURL() {
  var cpService = getCloudPrintService();
  if (!cpService.hasAccess()) {
  Logger.log(cpService.getAuthorizationUrl());
  }
} 

この一連の関数の次のコンポーネントで、クライアント ID とシークレットを必ず置き換えてください。

function getCloudPrintService() {
  return OAuth2.createService('print')
  .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
  .setTokenUrl('https://accounts.google.com/o/oauth2/token')
  .setClientId('**YOUR CLIENT ID FROM GOOGLE DEVELOPER CONSOLE**')
  .setClientSecret('**YOUR CLIENT SECRET**')
  .setCallbackFunction('authCallback')
  .setPropertyStore(PropertiesService.getUserProperties())
  .setScope('https://www.googleapis.com/auth/cloudprint')
  .setParam('login_hint', Session.getActiveUser().getEmail())
  .setParam('access_type', 'offline')
  .setParam('approval_prompt', 'force');
}

function authCallback(request) {
  var isAuthorized = getCloudPrintService().handleCallback(request);
  if (isAuthorized) {
   return HtmlService.createHtmlOutput('You can now use Google Cloud Print from Apps Script.');
  } else {
  return HtmlService.createHtmlOutput('Cloud Print Error: Access Denied');
  }
}

次に、使用するクラウド プリント プリンターの ID を取得します。これは Chrome の設定メニューで取得できます。設定 --> 詳細設定を表示 --> クラウド プリントの [管理] --> 使用するプリンターを選択 [管理] --> 詳細設定

クラウド プリントを開始するには、詳細をチケットに追加する必要があります。

var ticket = {
  version: "1.0",
  print: {
    color: {
      type: "STANDARD_COLOR",
      vendor_id: "Color"
    },
    duplex: {
      type: "LONG_EDGE"
    },
    copies: {copies: 1},
    media_size: {
       width_microns: 215900,
       height_microns:279400
    },
    page_orientation: {
      type: "PORTRAIT"  
    },
    margins: {
      top_microns:0,
      bottom_microns:0,
      left_microns:0,
      right_microns:0
    },
    page_range: {
      interval: 
        [{start:1,
        end:????}]
    }
  }
};

チケットに追加できるオプションは多数あります。ドキュメントを見る

最後に、クラウド プリント サービスを開始する必要があります。ここで、必要な特定のプリンターを定義します。

var payload = {
"printerid" : '**COPY YOUR PRINTER ID HERE**',
"title"     : "Prep Print",
"content"   : PUT YOUR CONTENT HERE...(e.g. If you do all of this using Google Apps Script...HtmlService.createHtmlOutput(VARIABLE).getAs('application/pdf')),
"contentType": 'text/html',
"ticket"    : JSON.stringify(ticket)
};
var response = UrlFetchApp.fetch('https://www.google.com/cloudprint/submit',     {
 method: "POST",
 payload: payload,
 headers: {
  Authorization: 'Bearer ' + getCloudPrintService().getAccessToken()
 },
 "muteHttpExceptions": true
 });


 response = JSON.parse(response);

 if (response.success) {
  Logger.log("%s", response.message);
 } else {
  Logger.log("Error Code: %s %s", response.errorCode, response.message);}

  var outcome = response.message;
 }
于 2016-03-22T15:41:55.340 に答える