4

私は現在、php バックエンド システムに直接印刷できるようにする機能を追加しており、Google のクラウド プリントと連携させようとしています。アプリをオンライン ショッピング カートと想像してみてください。誰かがログインする必要なく、ピッキング メモ(完了した注文)を印刷したいと考えています。サーバーはリモートで、宛先にはクラウド対応プリンターがあります。

これまでのところ、HTML、プレーンテキスト、または URL を PDF に渡すだけで、インターフェースを使用して印刷することに成功しています。印刷をカラー、フチなし、印刷品質に設定できます。

ただし、問題が発生したのは、システムが作成する PDF が公開されていないため、ファイルに URL を渡すことができず、ファイルの内容を渡す必要があることです。

Web で見つけた例の 1 つを修正しようとしても成功しませんでした。しかし、私は言語を知らないので、苦労しています。

Python HEREの別の例をもう一度試してみましたが、成功しませんでした!

インターフェイスを操作するために、PHP と Zend フレームワークを使用しています。これは私が試した1つのサンプルで、送信するファイルを準備しようとしているところまで切り詰めています。たとえば、pythonからphpへの変換について、またはpythonスクリプトが機能するかどうかはよくわかりませんが、これは何ですか私が思いついた:

<?php
// Test print a job:
$b64_pathname = PDF_PATH.'ec22c3.pdf'.'.b64';
$fileType = "application/pdf";
// Open the original file and base64 encode it:
$dataHandle = fopen(PDF_PATH.'ec22c3.pdf', "rb");
$dataContent = fread($dataHandle, filesize(PDF_PATH.'ec22ed167763a15e8591a3776f3c65c3.pdf'));
fclose($dataHandle);
$b64data = $fileType.base64_encode($dataContent);
// Store the base64 encoded file:
$ourFileHandle = fopen($b64_pathname, 'w');
fwrite($ourFileHandle, $b64data);
fclose($ourFileHandle);
// Read the contents of the base64 encoded file and delete it:
$fileHandle = fopen($b64_pathname, "rb");
$fileContent = fread($fileHandle, filesize($b64_pathname));
fclose($fileHandle);
unlink($b64_pathname);
// URL encode the file contents:
$file = urlencode($fileContent);
// Add the file and send to the printer:
$client->setParameterPost('content', $file);
$client->setParameterPost('contentType', $fileType);
$client->request(Zend_Http_Client::POST);
?>
4

2 に答える 2

2

cUrl を使用した php のメソッドを次に示します (_auth、_username、_password、_printerId というオブジェクト レベルの変数があることに注意してください)。

まず、cUrl で投稿する関数を作成します。

function processRequest($url, $postFields, $referer) {  
    $ret = "";
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_USERAGENT, "");
    if(!is_null($postFields)) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,
             $postFields);
        // http_build_query() will properly escape the fields and
        //  build a query string.
    }

    if(strlen($this->_auth) > 0) {
        $headers = array(
        "Authorization: GoogleLogin auth=". $this->_auth,
        //"GData-Version: 3.0",
        "X-CloudPrint-Proxy", "yourappname"
        );
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_REFERER, $referer);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    

    $ret = curl_exec ($ch);

    curl_close ($ch); 

    return $ret;
}

次に、Google に対して承認する関数:

  public function authorize() {
    $url = "https://www.google.com/accounts/ClientLogin";
    $post = array("accountType" => "HOSTED_OR_GOOGLE",
    "Email" => $this->_username,
    "Passwd" => $this->_password,
    "service" => "cloudprint",
    "source" => "yourappname");
    $resp = $this->processRequest($url, $post, "");

    preg_match("/Auth=([a-z0-9_\-]+)/i", $resp, $matches);
    $this->_auth = $matches[1];
  }

最後に、クラウド プリンターに送信する関数を作成します。

function printDocument($title, $docBytes)
{
    $url = "http://www.google.com/cloudprint/submit?printerid=". $this->_printerId."&output=json";

    $post = array(
        "printerid" => $this->_printerId,
        "capabilities" => "",
        "contentType" => "dataUrl",
        "title" => $title,
        "content" => 'data:application/pdf;base64,'. base64_encode($docBytes)
    );

    $ret = $this->processRequest($url, $post, "");

    echo $ret;
}

使用中は、authorize() を呼び出して認証トークンを取得します。次に、ファイルを (どこからでも) 変数に読み込み、それをタイトルと共に printDocument に渡します。

于 2012-07-07T16:23:24.743 に答える
1

base64でエンコードされたコンテンツを送信するには、送信リクエストで別のパラメーターを送信する必要があります。$ client-> setParameterPost('contentTransferEncoding'、'base64');

于 2012-03-15T20:51:08.627 に答える