Print to Google Cloud Print with dynamic URL

强颜欢笑 提交于 2020-01-13 06:43:09

问题


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.com/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 come up with?

Thank you for reading.


回答1:


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();



回答2:


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



来源:https://stackoverflow.com/questions/7880124/print-to-google-cloud-print-with-dynamic-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!