0

I'm very new to coding and need to have a button that prints the object only. I have looked at so many other questions but cannot find a solution that works for me. This is my code so far, but it only works on IE. I need it to work on other browsers especially Chrome and Firefox. Any ideas please?

<object data="folder/thedoc.pdf" id="myDoc" type="application/pdf" width="300" height="200">
  alt : <a href="folder/thedoc.pdf">thedoc.pdf</a>
</object>

<script>
function printDocument() {
var x = document.getElementById("myDoc");
x.print();
}

</script>

<button onclick="printDocument()">Print</button>
sd0093
  • 13
  • 7
  • You're PDF is in an Object which is an embed. Embedded assets have security issues and they differ from browser to browser. If you were to use an iframe instead of an object, you could use Chrome. – zer00ne Jun 07 '17 at 10:50
  • Have you tried solutions mentioned in [Print PDF directly from JavaScript](https://stackoverflow.com/questions/16239513/print-pdf-directly-from-javascript)? – Stanislav Kvitash Jun 07 '17 at 10:52
  • @zer00ne I tried an iframe and couldn't get that to work either. And correct me if I'm wrong but are iframes obsolete now? – sd0093 Jun 07 '17 at 10:57
  • Thanks @StanislavKvitash but yes, I tried those solutions and couldn't get any to work. I can try them again. – sd0093 Jun 07 '17 at 10:58
  • @sd0093 the iframe isn't deprecated and they are in use everywhere. Try the iframe again but this time add this attribute `allow-modals` but be aware that this is a sandbox feature. [See this for details](https://googlechrome.github.io/samples/block-modal-dialogs-sandboxed-iframe/) – zer00ne Jun 07 '17 at 11:03
  • Thank you @zer00ne I tried again with the iframe but when I add the attribute `allow-modals` the PDF does not display at all. – sd0093 Jun 07 '17 at 11:19

0 Answers0