File link for viewing / downloading / printing

Asked

Viewed 1,353 times

1

Setting

I have a list of files dynamically generated, and would like to have 3 options:

  • Open up: display in browser (if it is PDF, JPG, etc);
  • Download: download directly;
  • Print out: send straight to standard printer.

What I got

Visualise with target="_blank on the tag <a>:

<td><a href="files/100-1533649017.pdf" target="_blank">MeuArquivo.pdf</a></td>

Download with download on the tag <a>:

<td><a href="files/100-1533649017.pdf" download>MeuArquivo.pdf</a></td>

Doubts

  • Right away, I couldn’t find send direct to printer, there is native HTML form for this?
  • If there is no native HTML form, which alternatives?
  • There is a another way, the one I used to visualise?
  • There is a another way, the one I used to download?
  • For print cases take a look at this link: Link

  • @Wesleyaraujo I appreciate the help, but wanted to direct in HTML, and also not only PDF, I have XML, JPG, RAR, etc

  • I don’t know if it’s possible with pure HTML. I think you at least have to put an attribute like "onclick(//JS code)" to get what you want

3 answers

0

  • Vinicius, the file already opens in the browser or downloads. There is no way to put window.print() on it. And if you put it in the link, it will print the page and not the file.

0

The following is a possible example to print a file (yes! -- it can be .pdf, .jpg, etc.; externalPage.html, in this case), extracted from MDN:

<!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('externalPage.html');" style="cursor:pointer;text-decoration:underline;color:#0000ff;">Print external page!</span></p>
</body>
</html>

0

There is no way to send a file for printing, but what you can do is display the contents of that file on a screen and run the javascript command for printing, as mentioned above. After this command close this screen with window.close. It is "almost" the same thing. It may be possible to do this inside an invisible iframe.

Browser other questions tagged

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