admin管理员组

文章数量:1389833

I'd like a "print" button on my web-site to print a document with Google Cloud Print, but I want the "url" of the target – a PDF file – to change dynamically, after the button has been clicked (notably, based on what's returned from an AJAX call).

The GCP Web Element (GCPWE) seems to have similar functionality, but not quite what I desire. In particular, it seems as though one can only change the "target" (by calling setPrintDocument) before the button is clicked.

Is there a way to specify the URL for GCPWE after the button has been clicked?

Here's the example code from the GCPWE site:

<div id="print_button_container"> </div>


<script src=".js">
</script>
<script defer="defer">
    var gadget = new cloudprint.Gadget();
    gadget.setPrintButton(
          cloudprint.Gadget.createDefaultPrintButton("print_button_container"));
    gadget.setPrintDocument("url", "[document title]", "[document URL]");
</script>

I've considered hiding the <div id='print_button_container'> and triggering a .click event on it after a visible print button has been clicked, but this seems rather inelegant & improper. I've looked at the gpwidget.js linked to above, but the code has been minimized and is unintelligible (to me, at any rate).

What I seem to desire is a Javascript print function that you pass a [document URL] and [document title] to.

Is there a better way to achieve the desired functionality, rather than the 'clicking the hidden button' I've e up with?

Thank you for reading.

I'd like a "print" button on my web-site to print a document with Google Cloud Print, but I want the "url" of the target – a PDF file – to change dynamically, after the button has been clicked (notably, based on what's returned from an AJAX call).

The GCP Web Element (GCPWE) seems to have similar functionality, but not quite what I desire. In particular, it seems as though one can only change the "target" (by calling setPrintDocument) before the button is clicked.

Is there a way to specify the URL for GCPWE after the button has been clicked?

Here's the example code from the GCPWE site:

<div id="print_button_container"> </div>


<script src="http://www.google./cloudprint/client/cpgadget.js">
</script>
<script defer="defer">
    var gadget = new cloudprint.Gadget();
    gadget.setPrintButton(
          cloudprint.Gadget.createDefaultPrintButton("print_button_container"));
    gadget.setPrintDocument("url", "[document title]", "[document URL]");
</script>

I've considered hiding the <div id='print_button_container'> and triggering a .click event on it after a visible print button has been clicked, but this seems rather inelegant & improper. I've looked at the gpwidget.js linked to above, but the code has been minimized and is unintelligible (to me, at any rate).

What I seem to desire is a Javascript print function that you pass a [document URL] and [document title] to.

Is there a better way to achieve the desired functionality, rather than the 'clicking the hidden button' I've e up with?

Thank you for reading.

Share Improve this question edited Oct 24, 2011 at 19:02 Brian M. Hunt asked Oct 24, 2011 at 18:31 Brian M. HuntBrian M. Hunt 83.9k76 gold badges234 silver badges349 bronze badges 1
  • Here's what I was able to do with the answer below... see this thread: stackoverflow./questions/8593065/… – Thomas Butler Commented May 22, 2012 at 18:23
Add a ment  | 

2 Answers 2

Reset to default 4

You can open print dialog whenever you want that, just call:

gadget.openPrintDialog()

and that will open printer dialog. So for your case you might want to create print button using static method:

cloudprint.Gadget.createDefaultPrintButton("print_button_container");

then attach custom handlers for the button, and whenever you are ready just call:

var gadget = new cloudprint.Gadget();
gadget.setPrintDocument(...);
gadget.openPrintDialog();

You can call method gadget.setPrintDocument() at any moment, even after the print dialog has been opened.

本文标签: javascriptPrint to Google Cloud Print with dynamic URLStack Overflow