We help IT Professionals succeed at work.

Need a print button in a browser that will print a PDF file

266 Views
Last Modified: 2012-05-09
I need to set up a print button in HTML that will print a certain PDF file, hopefully without having to download the file first.  I've tried a few things, including answers previously suggested here (e.g. https://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_20655206.html and
https://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_21528299.html ) -- but the best we get is something that works in IE only and I need it to be browser independent (as much as possible).   We're on Linux and have Javascript and PHP + ...  to work with.

Since these are OLD questions/answers (2003/1005), does anyone have any new ideas or ways that this could be done?
Comment
Watch Question

Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
For some reason this does not work (from second link) but it should...


<html>
<head>
      <title>print using link rel=alternate</title>
<link rel="alternate" href="test1.txt" media="print">
<script>
/* Print alternate prints using link
   Copyright (c) 2003-2010 Michel Plungjan "javascripts(a)plungjan.name" */
obj = document.getElementsByTagName('LINK');  
function changePrint(url) {
   if (url) obj[0].href=url;
   window.print();
   return false;
}
changePrint('pdf1.pdf')
</script>
</head>

Open in new window

Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:

Author

Commented:
Thanks. From that I gather only 2 possible ways that work with PDF's:
1. Embedding the file is not acceptable in our case due to download considerations -- we don't want to force a download just to have a print button which may or may not be used.
2.  I'll look into embedding javascript in the pdf as a solution, but then this would mean I'd have two copies of the pdf -- one that would automatically print and another for viewing/downloading, right?  Never done that before so if there are any tips/..., much appreciated.

If you or anyone knows a way that'll work ...
IT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
That worked for me and on my wife's Mac (Leopard 10.5.8 & Safari 4.0.2).  Sounds like the best we can hope for is a self-printing PDF.  You mentioned it didn't work "off the bat in MAC" -- if you could point me to any cases where I could at least warn users ahead of time, that'd help.

Thanks much for your help.
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
great news for you, less so for me since the previous comment should be turned around to be "this probably works for most including MAC however on some configurations like mine it may not work" I cannot tell you why, but I often have this with pdf
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
Accept mplungjan {33176563}
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.