Unable To Print PDF file Using Print Js

1.8k views Asked by At

Below Given is my code from which I am trying to print pdf file.

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script type="text/javascript" src="print.min.js"></script>
  <link rel="stylesheet" type="text/css" href="print.min.css">
</head>
<body>
  <script type="text/javascript">
    function sample(){
      printJS({
        printable: "test.pdf",
        type: "pdf",
      });
    }
  </script>
  <button onclick="sample()">Click Me</button>
</body>
</html>

but it is not working and diving below error on chrome console

Access to XMLHttpRequest at 'file:///test.pdf' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

I am following all the rules said in documentation of Print-js. my {.pdf} file and {.html} file are in same folder. It is Working Fine with {.jpg} File. Please help me.

I am sharing link of the script I have included in my head tag JS:- https://printjs-4de6.kxcdn.com/print.min.js CSS:- https://printjs-4de6.kxcdn.com/print.min.css

1

There are 1 answers

0
AudioBubble On

It was the problem regarding "Cross origin requests". To solve this you need to start your local server. in my case I started Xampp and moved my files to htdocs folder and BOOM it worked.. I Still dont know why it was working for images and not for pdfs.

HERE IS THE ANSWER ON STACK-OVERFLOW THAT HELPED ME. https://stackoverflow.com/a/20578692/10333164