最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to print a pdf? - Stack Overflow

programmeradmin1浏览0评论

I have a pdf url, embedded in a rectangular area of an iframe. I am trying to print it on click of a button . My code looks like this :

JavaScript to print :

function printPDF(pdfUrl)
{
var w = window.open(pdfUrl);
w.print();
w.close();
}

HTML-Code :

<table>
<tr>
<td><input type="submit"  value="Print"
name="Submit" id="printbtn"
onclick="printPDF('.pdf')" /></td>
</tr>

Now in MF it is ing like this :

The problem , its not working properly in chrome and IE. I have chrome 20 and IE 8. In chrome the print window gets loaded in a new tab(which is ok) but pdf does not get loaded in preview. In IE the pdf just gets opened in a new tab and print prompt does not e up.

Before any one asks me, pdf might have inherent print functionality. but the pdfs that i am processing need to have a print button. People, kindly help me out here. Is there a solution, which is applicable in all the 3 browsers .

I have a pdf url, embedded in a rectangular area of an iframe. I am trying to print it on click of a button . My code looks like this :

JavaScript to print :

function printPDF(pdfUrl)
{
var w = window.open(pdfUrl);
w.print();
w.close();
}

HTML-Code :

<table>
<tr>
<td><input type="submit"  value="Print"
name="Submit" id="printbtn"
onclick="printPDF('http://www.irs.gov/pub/irs-pdf/fw4.pdf')" /></td>
</tr>

Now in MF it is ing like this :

The problem , its not working properly in chrome and IE. I have chrome 20 and IE 8. In chrome the print window gets loaded in a new tab(which is ok) but pdf does not get loaded in preview. In IE the pdf just gets opened in a new tab and print prompt does not e up.

Before any one asks me, pdf might have inherent print functionality. but the pdfs that i am processing need to have a print button. People, kindly help me out here. Is there a solution, which is applicable in all the 3 browsers .

Share Improve this question edited Dec 19, 2012 at 21:19 PointedEars 15k4 gold badges35 silver badges33 bronze badges asked Dec 19, 2012 at 8:34 The Dark KnightThe Dark Knight 5,58513 gold badges58 silver badges104 bronze badges 3
  • Why not just put window.print() on onload event of body of your iframe page. – vusan Commented Dec 19, 2012 at 8:37
  • I did not understand what you are telling me to do. onloading a body is a bad move also i think – The Dark Knight Commented Dec 19, 2012 at 8:40
  • Related thread - stackoverflow./questions/687675/… – KV Prajapati Commented Dec 19, 2012 at 8:43
Add a ment  | 

1 Answer 1

Reset to default 2

Folks, just got a solution , which works fine for me .

function printPage(htmlPage)
{
    var w = window.open("about:blank");
    w.document.write(htmlPage);
    if (navigator.appName == 'Microsoft Internet Explorer') window.print();
    else w.print();
}

So, if it is MSIE, then we can just use : window.print();

发布评论

评论列表(0)

  1. 暂无评论