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!
JavaScript

Avatar of undefined
Last Comment
Michel Plungjan

8/22/2022 - Mon
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>
NHBFighter

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>
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>
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
NHBFighter

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?
NHBFighter

Also how is it not working?  Is it giving any errors?
Mylor

ASKER
No it does not give me any errors, just doesn't do anything.
I have IE7 and Adobe 8
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
NHBFighter

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
Michel Plungjan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
NHBFighter

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
Michel Plungjan

It would not

So <a href="pdfFileWithEmbeddedJavascriptThatPrints.pdf">Print</a>
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy