如何从JSP页面生成PDF报告?

时间:2014-05-01 15:44:41

标签: java javascript css jsp pdf

我似乎无法从此JSP页面生成PDF报告。虽然我能够以.pdf格式生成表单,但无法在Adobe Reader中打开。

<%

//response.setCharacterEncoding("utf-8");
//response.setHeader("Content-Transfer-Encoding", "binary");
//response.setHeader("Content-Type", "application/pdf");
response.setContentType("application/pdf");
response.setHeader("content-disposition", "attachment;filename=Donation Report.pdf");
%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>certificateword</title>
</head>
<body>
    <h1></h1>
     <style> 
.crtf_div{width:900px; height:auto; text-align:center; font-family:Arial, Helvetica, sans-serif; border:1px solid #000; padding:25px 0px;}
.crtf_div p{width:850px; height:auto; margin:20px 25px; text-align:justify; font-size:14px;}
.crtf_head{width:900px; height:auto; text-align:center; font-size:16px; font-weight:bold;}
.crtf_head .head_small{font-size:12px;}

.crtf_table{width:850px; height:auto; margin:10px 25px; border:1px solid #000; text-align:left; font-size:14px;}
.crtf_table tr td input{width:670px; height:20px; border:0px;}
.crtf_table tr td textarea{width:670px; height:50px; border:0px;}0


.crtf_list{width:auto; height:auto; margin:10px 25px; text-align:justify; font-size:14px;}
.crtf_list ol li{width:auto; height:auto; margin-bottom:10px;}
</style>
 <center>
  <div class="crtf_div">
<Div class="crtf_head">Office of the <br />
Director of Income Tax (E),<br />
3rd Floor, Aaykar Bhawan,<br />
District Centre Laxmi Nagar, Delhi - 110092<br />
<div class="head_small">Tel. No. 011-2055545, 22054777</div>
</Div>
<br />

<table class="crtf_table" style="border:0px; text-align:center;">
<tr>
    <td>NQ.DIT (E) I 2013-14/</td>
    <td>DEL - XXXXXXX XXXXXXXX</td>
    <td>Dated &nbsp; &nbsp; &nbsp; &nbsp; 05/05/2014</td>
</tr>
</table>
<table class="crtf_table" border="1" cellspacing="0" cellpadding="5">
<tr>
  <td width="150">NAME</td>
  <td ><input type="text" name="name" value=" "/></td>
</tr>
<tr>
  <td>ADDRESS</td>
  <td><textarea name="add" rows="4" cols="20" value=" "></textarea></td>
</tr>
<tr>
    <td>Legal Status</td>
    <td><input type="text" name="lstatus" value="----"/></td>
</tr>
<tr>
    <td>PAN NO. </td>
    <td><input type="text" name="pan" value="XXXXX"/></td>
</tr>
<tr>
    <td>GIR NO.</td>
    <td><input type="text" name="gir" value="XXXX-XXXX"/></td>
</tr>
</table>
<p><b>Sub :- ORDER UNDER SECTION 80G (5)(vi) OF THE INCOME TAX ACT, 1961</b></p>
<p>&nbsp; &nbsp; &nbsp; On verification of the facts stated before me/hearing before me, I have come to the conclusion that this organization satisfies the conditions u/s 80G of .the Income Tax act, 1961. The institution/Fund is granted approval subject to the following conditions--</p>

<div class="crtf_list">
    <ol type="i">
    <li>The Donee institution shall forfeit this benefit provided under the law, if any of the conditions stated herein is not 
    complied with/abused/whittled down or in any way violated.</li>
    <li>This exemption is valid for the period from A.Y.2013-14 onwards till it is rescinded    and subject to the following conditions</li>
</ol>
</div>

<p><b>Conditions:</b></p>

<div class="crtf_list">
<ol type="i">
    <li>You shall maintain your accounts regularly and also get them audited to comply with sec. 80G (5)(iv) read with section 12A(b) 
    and 12A(c) and submit the same before the assessing officer by the due date as per section 139-(t) of the Income tax Act 1961.</li>
    <li> Every receipt issued to donor shall bear the number and date; of this order and shall state the date up to which this 
    certificate is valid from   A.Y.2013-14 onwards till it is rescinded.</li>
    <li> No change in the deed of the trust/association shall be affected without the due procedure of Law and its intimation 
    shall be given immediately to this office.</li>
    <li> The approval to the institution/fund shall aroply to the donations received only if the fund/institution, established 
    in India for charitable purpose, fulfills the conditions as laid down in section 80G(i), (ii), (iii), (iv) & (v) of the Income Tax Act 1961.</li>
    <li> This office and the assessing officer shall also be informed about the managing trustees or Manager of your Trust/Society/Non Profit 
    Company and the places where the activities of the Trust/Institution are undertaken/likely to be undertaken to satisfy the claimed objects.</li>
    <li> You shall file the return of income of liour fund/institution as per section  139(1)/(4A)/(4C) of the Income Tax Act.</li>
</ol>
</div>
<br /><br />

 <table class="crtf_table" style="border:0px; line-height:20px;">
<tr>
    <td><b>Copy to:</b><br />
    1. The applicant as above<br />
    2. The Assessing Officer
    </td>
    <td style="float:right; text-align:center;"><br />
    Director of Income Tax (Exemption)<br />
    DELHI<br /><br /><br />
    <br />
    Income Tax Officer (Exemption) (Hqrs.)<br />
    For Director of Income Tax (Exemption) DELHI
    </td>
</tr>
</table>
 <p>As it is a computer generated letter, No signature is required</p>

 </div>
 </center><br><br>

</body>
  </html>

1 个答案:

答案 0 :(得分:2)

您无法在页面上混合两种mimetypes,就像您尝试做的那样。

取出所有HTML。经过这两行

response.setContentType("application/pdf");
response.setHeader("content-disposition", "attachment;filename=Donation Report.pdf");

所有应该做的就是打印出PDF文件的字节。您可能必须在servlet中执行此操作,因为在JSP中尝试执行此操作通常会收到response.getOutputStream()已被调用的错误。

response.setHeader("content-disposition", "attachment;filename=Donation Report.pdf");

除了告诉浏览器之后您要打印的文件的文件名。它不会去取PDF本身。您必须打开PDF,将其读入字节数组,然后将其打印到response.getOutputStream()