Link to home
Start Free TrialLog in
Avatar of Mylor
Mylor

asked on

Print PDF form a link

Ok, I m driving my self crazy. I need to print several PDF files from links in my page, can anyone help please.

ie:

Print File 1 (will print, www.mysite.com/adobedoc1.pdf )
Print File 2 (will print, www.mysite.com/adobedoc2.pdf )
Print File 3 (will print, www.mysite.com/adobedoc3.pdf )

Thanks in advance!
Avatar of NHBFighter
NHBFighter

This will work in internet explorer:

<html>
<head>
<script>
function printPDF(url){
      document.getElementById('Pdf1').src=url;
      document.getElementById('Pdf1').printWithDialog();
}
</script>
</head>
<body>
<a href="javascript:printPDF('www.mysite.com/adobedoc1.pdf');">Print File 1</a><BR>
<a href="javascript:printPDF('www.mysite.com/adobedoc2.pdf');">Print File 2</a><BR>
<a href="javascript:printPDF('www.mysite.com/adobedoc3.pdf');">Print File 3</a>
<object id="Pdf1" type="application/pdf"  style="width:0;height:0"></object>
</body>
</html>





For other browsers you will probably have to create a separate embed or object for each pdf you want to print and have them get loaded when the page loads. (Similar to one of your previous posts):

<html>
<head>

<script>
function printPDF(elementId){
      var x = document.getElementById(elementId);
      x.click();
      x.setActive();
      x.focus();
      x.print();
}
</script>

</head>
<body>
<a href="javascript:printPDF('PDF1');">Print File 1</a><BR>
<a href="javascript:printPDF('PDF2');">Print File 2</a><BR>
<a href="javascript:printPDF('PDF3');">Print File 3</a>


<embed id="PDF1" src ="www.mysite.com/adobedoc2.pdf" width="0" height="0">
<embed id="PDF2" src ="www.mysite.com/adobedoc2.pdf" width="0" height="0">
<embed id="PDF3" src ="www.mysite.com/adobedoc3.pdf" width="0" height="0">

</body>
</html>
I can get this to work in fire fox but in IE it prints all the links and not the pdfs

<html>
<head>
<script>
function printPDF(elementId){
      var elem=document.getElementById(elementId);
      elem.contentWindow.focus();
      elem.contentWindow.print();
}
</script>
</head>
<body>
<a href="javascript:printPDF('PDF1');">Print File 1</a><BR>
<a href="javascript:printPDF('PDF2');">Print File 2</a><BR>
<a href="javascript:printPDF('PDF3');">Print File 3</a><BR>
<iframe id="PDF1" name="PDF1" src ="PCNMap.pdf" width="0" height="0" style="visibility:hidden"></iframe>
<iframe id="PDF2" name="PDF2" src ="PCNMap.pdf" width="0" height="0" style="visibility:hidden"></iframe>
<iframe id="PDF3" name="PDF3" src ="PCNMap.pdf" width="0" height="0" style="visibility:hidden"></iframe>
</body>
</html>
Avatar of Mylor

ASKER

I really like this solution, but for some reason it doesn't work:



<html>
<head>
<script>
function printPDF(url){
      document.getElementById('Pdf1').src=url;
      document.getElementById('Pdf1').printWithDialog();
}
</script>
</head>
<body>
<a href="javascript:printPDF('www.mysite.com/adobedoc1.pdf');">Print File 1</a><BR>
<a href="javascript:printPDF('www.mysite.com/adobedoc2.pdf');">Print File 2</a><BR>
<a href="javascript:printPDF('www.mysite.com/adobedoc3.pdf');">Print File 3</a>
<object id="Pdf1" type="application/pdf"  style="width:0;height:0"></object>
</body>
</html>
Is it not working in IE?  I've tried it with IE 6 and 7.  And I have Adobe 7.0 on my machine.
What configuration do you have?
Also how is it not working?  Is it giving any errors?
Avatar of Mylor

ASKER

No it does not give me any errors, just doesn't do anything.
I have IE7 and Adobe 8
Hmmm,  printing PDFs using javascript has been a struggle for me for years, I've never found a real solid solution.  If you expand the size of the <object> so you can see it does the PDF acctually get loaded into the object? (can you see the pdf when you click the print link?)
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mplungjan,

So how would you have the javascript with in the PDF get executed when the user clicks on a link in the HTML page?  

So <a href="javascript:makePDFPrint()">Print</a>  What would makePDFPrint() do to call the javascript in the PDF?  

Dave
It would not

So <a href="pdfFileWithEmbeddedJavascriptThatPrints.pdf">Print</a>