Is there an alternative method to window.print()?

Asked

Viewed 1,267 times

1

I want to print an iframe, but the print() method does not work because my "src" attribute contains

"data:text/html;charset=utf-8,%3Chtml%3E%3Cbody%3Efoo%3C/body%3E%3C/html%3E",

There is a method that allows me to call the browser print window?

Update: Follow the error:

Uncaught Securityerror: Blocked a frame with origin "http://testeecm:8080" from accessing a frame with origin "null". The frame requesting access has a Protocol of "http", the frame being has accessed a Protocol of "data". Protocols must match.

my iframe:

 {
     xtype      : "component",
     hidden: true,
     id         : 'iframeDudu',
     items:[
         //framedudu
     ],

  autoEl     : {
    width : 1000,
    //hidden: true,
    height: 700,
    tag   : "iframe",
    //items: myPanel
    id:'frameAutoEl',
    //src   : "http://testeecm:8080/webdesk"

    //src   : "http://servidorweb/redmine/login?back_url=http%3A%2F%2Fservidorweb%2Fredmine%2F"
    //Uncaught SecurityError: Blocked a frame with origin "http://testeecm:8080" from accessing a
    // frame with origin "http://servidorweb". Protocols, domains, and ports must match.

    //html           : '<iframe src="http://forum.extjs.com.br/index.php?/topic/27562-resolvido-n%C3%A3o-est%C3%A1-exibindo-arquivo-pdf-na-window-do-extjs/?hl=imprimir"></iframe>',

    //html : thtml  //acontece nada

    //src : 'data:text/html;charset=utf-8,' + encodeURI(thtml)
    src : "data:text/html;charset=utf-8,%3Chtml%3E%3Cbody%3Efoo%3C/body%3E%3C/html%3E",
    // Uncaught SecurityError: Blocked a frame with origin "http://testeecm:8080" from accessing a frame
    // with origin "null".  The frame requesting access has a protocol of "http",
    // the frame being accessed has a protocol of "data". Protocols must match.

     }// final autoElement


}
  • 1

    "but the print() method doesn’t work" - like this? you can give an example in jsFiddle or here?

  • it returns a blank page to you? Because to print the frame you have to configure it on the server side

  • It does not print, error occurs in console

3 answers

0

Try this solution:

window.frames["printf"].focus();
window.frames["printf"].print();

But with source code it is easier to identify the problem.

0

An alternative:

JS

function printFrameContent(id) {
    var cFrame = document.getElementById(id).contentWindow;
    cFrame.focus();
    cFrame.print();
    return false;
}

HTML

<iframe id="conteudo" src=""></iframe>

<button onclick="printFrameContent('conteudo')"> Teste de Impressão </button>

0


I couldn’t find another method to print an iframe. The alternative was to create a window and pass the html content to her. I used the Handlebars library to pass the data to the html window. So I don’t need to print the iframe, nor do I need the property src iframe. Just print out the window with the content html previously incorporated already:

var imprimirAso = function (){
source = '<button id="imprimir" onclick="divPrint()">Imprimir</button>' +
'<table>' +
'<tr>' +
'<td style="width:100px">' +
'<img class="logo"/>' +
'</td>' +
'</td>' +
'<td><strong>Empresa:</strong> {{razaoSocial}}</td>' +
'<td><strong>CNPJ:</strong>    {{cnpj}}</td>' +
'</td>'',

var template = Handlebars.compile(source);

var data = { "razaoSocial": razaoSocial, "cnpj": cnpj, "endereco": endereco, "cidade": cidade, "estado": estado,
"fantasiaClinica"       : fantasiaClinica, "enderecoClinica": enderecoClinica, "bairroClinica": bairroClinica,
"cidadeClinica"         : cidadeClinica, "ufClinica": ufClinica, "cepClinica": cepClinica, "telefoneClinica": '',
"nomeCandidato"         : Ext.getCmp('nome').getValue(), "descSetor": Ext.getCmp('descSetor').getValue(),
"dataNascimento"        : Ext.get('dataNascimento').dom.value, "cpf": Ext.getCmp('cpf').getValue(),
"funcao"                : Ext.getCmp('funcao').getValue(),
"example"               : "<button> Hello </button>"
},

var result = template(data);
wnd = window.open('', '', 'width=1000,height=850');
wnd.document.write(result);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.