Access denied for printing iframe in Firefox

Asked

Viewed 143 times

0

I have a system where I need to print a PDF by clicking the button.

The url is built according to the shipment ID, so dynamically, I use an iframe that will load the data and, when loaded, I call a function to print what is in the iframe.

I’m doing like this:

$scope.imprimir = function imprimir (protocolo_id) {

    if (imprimir.carregando) return;

    var url_protocolo = '/protocolos/imprimir/' + protocolo_id;

    imprimir.carregando = true;

    angular.element('#imprimir_protocolo').attr('src', url_protocolo).on('load', function () {

        var frame = $window.frames['imprimir_protocolo'];

        frame.focus();

        frame.print();


        delete imprimir.carregando;

        $scope.$apply();
    })
}

On the line where I make the call from frame.focus(), when I use Firefox, the following error appears:

Permission denied to access Property "print"

1 answer

1

You cannot run some scripts in an iframe directly, you can try using contentWindow:

var cw = frame.contentWindow;
cw.focus();
cw.print();

If it’s in different domains or different protocols maybe it’s not accessible yet, then you can try injecting a function into the iframe, something like:

<script>
function printPage() {
    window.focus();
    window.print();
}
</script>

And call:

frame.contentWindow.printPage();

I haven’t worked with this for some time, but I remember that each browser had a behavior and varied a lot.

  • Aff, this problem of behaving different in each browser! To almost thinking of leaving a link opening the PDF :p

  • @Wallacemaxters find a good way, especially for mobiles :)

Browser other questions tagged

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