0

so i want to print pdf using button and javascript but it will always produce blank pdf/empty pdf. i want something like https://i.stack.imgur.com/2Bm7o.jpg, but i got this instead https://i.stack.imgur.com/nYVuq.jpg

i try using window.open and then window.print but it still produce blank pdf, i also tried to put url inside window.open instead of declaring it first but that didn't work

HTML

<button type="button" onclick="printme()">click me...</button>

JavaScript

function printme(){
 var URL = "https://www.detik.com/";
 var W = window.open(URL);
 W.window.print();
}

2 Answers2

2

you need to do window.print() instead of w.window.print()

function printme(){
 var URL = "https://www.detik.com/";
 var W = window.open(URL);
 window.print();
}
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
0

The source of the following code is Print an external page without opening it
which was mentioned in one of the answers here Printing a web page using just url and without opening new window?
I only modified the link to include your website address:

<!doctype html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>MDN Example</title>
  <script type="text/javascript">
    function closePrint() {
      document.body.removeChild(this.__container__);
    }

    function setPrint() {
      this.contentWindow.__container__ = this;
      this.contentWindow.onbeforeunload = closePrint;
      this.contentWindow.onafterprint = closePrint;
      this.contentWindow.focus(); // Required for IE
      this.contentWindow.print();
    }

    function printPage(sURL) {
      var oHiddFrame = document.createElement("iframe");
      oHiddFrame.onload = setPrint;
      oHiddFrame.style.position = "fixed";
      oHiddFrame.style.right = "0";
      oHiddFrame.style.bottom = "0";
      oHiddFrame.style.width = "0";
      oHiddFrame.style.height = "0";
      oHiddFrame.style.border = "0";
      oHiddFrame.src = sURL;
      document.body.appendChild(oHiddFrame);
    }
  </script>
</head>

<body>


  <p><span onclick="printPage('https://www.detik.com');" style="cursor:pointer;text-decoration:underline;color:#0000ff;">Print external page!</span></p>

</body>

</html>
Hamza Dahmoun
  • 1,204
  • 10
  • 16
  • i tried to run your demon, but i got an error message Error: { "message": "SecurityError: Permission denied to access property \"__container__\" on cross-origin object", "filename": "https://stacksnippets.net/js", "lineno": 22, "colno": 0 } – Misael Indra Wijaya Sep 26 '19 at 02:37