Creating a PDF report from a JSP page: Step-by-step guide

I'm experiencing difficulty generating the PDF report from this JSP page. While I can create the form in a .pdf format, it's not opening correctly in 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>

Answer №1

It's not possible to combine two different mimetypes on the page as you are attempting to do.

To resolve this issue, remove all HTML content and follow these steps:

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

The above code snippet simply sets the content type and filename for the PDF file download. To actually display the PDF, you need to read its bytes and write them to response.getOutputStream(). This is typically done in a servlet because calling response.getOutputStream() in JSP may cause errors.

Furthermore, the line:

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

Merely informs the browser of the filename to expect, it does not fetch the PDF itself. You must manually load the PDF, convert it into a byte array, and then send it to response.getOutputStream()

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble using Popper.js with Bootstrap 4 Beta in Webpack 3.x? You might encounter the error message "Popper is

Exciting news - Bootstrap 4 Beta has been released! However, Tether has been replaced with Popper.js for tooltip functionality (among other features). This change has triggered a new error in the console, prompting me to switch to Popper.js: Bootstrap drop ...

Retrieving latitude and longitude data from the database and assigning it to a JavaScript function

My current issue involves integrating Google Maps on my website. I have a RadComboBox with locations populated from the database and an ASP panel displaying the Google Map on the right-hand side. I want users to select a location from the RadComboBox, whic ...

JavaScript framework enabling front-end communication with RESTful APIs

I am searching for a lightweight javascript framework to build a client-side web application that will interact with the server via a REST API. I initially considered using react.js, but my team members rejected the idea because it lacks templating. Angul ...

How can I display an iframe element only if it exists within an object in Angular?

I'm currently exploring options to specifically display the iframe element within a block of HTML content being sent to my Angular application. While I can use a *ngIf directive in my template to check for the presence of the tag, I am unsure of how ...

You won't find Socket.io on the client-side

Server Side Code: const path = require('path'); const http = require('http'); const express = require('express'); const socketio = require('socket.io'); const app = express(); const server = http.createServer(app ...

The vertical scrollbar appears to be malfunctioning

I'm experiencing an issue where the scrollbar is visible but not functioning as expected. I am utilizing CSS for styling the scrollbar and other layouts, along with HTML to apply the styling. #sub_menu, #content{ display: inline-block; } #sub_ ...

What could be causing my code to fail in properly iterating through the array of objects in React using the id as the key?

I have successfully printed the blog array in the console, which includes the object. My goal is to utilize the object components by mapping through the id as the key, but I am unable to access the map function. Interestingly, I have used a similar map s ...

Dealing with the challenge of JavaScript's Undefined problem when working with a

I am having some trouble populating a table with data from a JSON string. Here is the code snippet I am using: tr = "<tr><td>" + data[i]["code"] + "</td><td>" + data[i]["codeDesc"] + "</td></tr>"; However, when I run t ...

What is the best method to invoke a function recursively with a delay of 1 second following the completion of an ajax

I am facing a situation where I need to implement a delay of 1 second after an ajax request is completed, regardless of the outcome. Instead of calling a method every second, I specifically want to call a function 1 second after the ajax request finishes. ...

Achieving Automatic Scrolling of Drawer in Material UI React JS

Looking for assistance, can anyone lend a hand with this code snippet I've been working on? See it here: https://codesandbox.io/s/5xoj9zw56l I referenced this code as part of my project development: https://codesandbox.io/s/6jr75xj8y3 ...

Utilizing Dynamic Routing URIs with Apache Camel

Currently, I am diving into chapter 8 of the book "Camel in Action" and experimenting with Dynamic Router. The setup involves defining two routes as shown below: from("direct:start") .dynamicRouter(method(DynamicRouterBean.class, "route")) .log("dynamicRo ...

Is it possible to utilize AJAXToolKit and Jquery on a single webpage?

Is it possible to utilize both AJAXToolKit and jQuery on a single page? For example, let's say we have ScriptManager in the same page along with including ...

The use of JavaScript variables for classes or IDs

I am currently working on a JavaScript code in a .js file where adding a class to an HTML element, like 'open-product', triggers the opening of a product popup. $('.open-product').on('click', function(){ showPopup($(& ...

Having trouble incorporating Duo Web SDK into angular application

We are currently working on incorporating Duo Two-factor authentication into our Angular application. For instructions, you can take a look at the documentation available here. The issue we are encountering is that their JavaScript file searches for an i ...

Creating a conditional statement within an array.map loop in Next.js

User Interface after Processing After retrieving this dataset const array = [1,2,3,4,5,6,7,8] I need to determine if the index of the array is a multiple of 5. If the loop is on index 0, 5, 10 and so on, it should display this HTML <div class="s ...

Using jQuery to retrieve the nth child from a table after dynamically creating it with AJAX

When using AJAX in the code below, I fill and create simple data after retrieving it. $.ajax({ method: 'GET', url: '/analyzePage/searchTag/' + tagName, contentType: false, processData: false, success: function (data ...

Is there a way to ensure that the size of the second div box can adjust dynamically in CSS?

Is there a way to make multiple boxes expand dynamically in size when one of them contains more words? Currently, the problem is that only the box with more text expands while the others remain unchanged. I want all the boxes to follow the same size dynami ...

What is the best way to link CSS files from libraries that are downloaded using npm?

For instance, let's say I installed a package: npm install --save package and it gets saved in ./node_modules/package/ Inside that folder, there might be a directory named styles and within that directory, you could find style.css. How can I link ...

lite-server is failing to serve the .js file located in the /js directory, resulting in the browser not sending a request

I am currently using a lite-server by running npm run lite Despite my jQuery file and CSS being loaded without issue, the browser is not even attempting to make a GET request for the main.js file. The main.js file is located in the /js folder, just like ...

Refreshing a textarea manually in Typescript

My textarea has the CSS property height: auto;. However, I noticed that when I reset the variable associated with the [ngModel] of this textarea (e.g. using this.myNgModelVar = "";), the height of the textarea doesn't automatically decrease unless the ...