123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <template></template>
- <script>
- export default {
- methods: {
- init (obj) {
- var this_ = this;
- if(undefined != obj){
- this_.writeIframe(obj);
- }
- },
- writeIframe: function (content) {
- var w, doc, iframe = document.createElement('iframe'),
- f = document.body.appendChild(iframe);
- iframe.id = "myIframe";
- iframe.style = "position:absolute;width:0;height:0;";
- w = f.contentWindow || f.contentDocument;
- doc = f.contentDocument || f.contentWindow.document;
- doc.open();
- doc.write(content);
- doc.close();
- this.toPrint(w);
- setTimeout(function () {
- document.body.removeChild(iframe)
- }, 100)
- },
- toPrint: function (frameWindow) {
- try {
- setTimeout(function () {
- frameWindow.focus();
- try {
- if (!frameWindow.document.execCommand('print', false, null)) {
- frameWindow.print();
- }
- } catch (e) {
- frameWindow.print();
- }
- frameWindow.close();
- }, 10);
- } catch (err) {
- console.log('err', err);
- }
- }
- }
- }
- </script>
|