Friends, I am have implemented a jsp form which takes inputs like title, description and content of the pdf file. When the jsp form is submitted, the pdf is created using itext with the help of the servlet called 'pdfGenServlet.java'.
my jsp form is
<form action="pdfGenServlet1" method="get" enctype="application/x-www-form-urlencoded">
<!-- input notes title-->
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" placeholder="Title of the notes" name="title">
</div>
</div>
<!-- input notes description-->
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" placeholder="Enter short description" name="description">
</div>
</div>
<div class="form-group">
<textarea name="content" id="myEditor"></textarea>
<div id="button-panel" class="panel panel-default">
<p>
<button type="submit" class="btn btn-primary "><span class="glyphicon glyphicon-plus"></span><strong> Create Note</strong></button>
<button class="btn btn-primary" type="reset"><strong>Reset</strong></button>
</p><!-- buttons -->
</div><!-- panel Button -->
</div>
</form>
The servlet 'pdfGenServlet.java'
//imports for itext
import java.io.FileOutputStream;
import java.io.StringReader;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.html.simpleparser.HTMLWorker; // deprecated
import com.itextpdf.text.pdf.PdfWriter;
//servlet imports
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//File handling and java util
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Date;
@WebServlet("/pdfGenServlet1")
public class pdfGenServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//Font for using with itext
Font bfBold18 = new Font(FontFamily.TIMES_ROMAN, 18, Font.BOLD, new BaseColor(0, 0, 0));
Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
String title = request.getParameter("title");
String description = request.getParameter("description");
String notes_content = request.getParameter("content");
Date date = new Date();
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream("C://BEProject//PreparedNotes//testpdf1.pdf"));
HTMLWorker htmlWorker = new HTMLWorker(document);
document.open();
document.addAuthor("Real Gagnon");
document.addCreator("Real's HowTo");
document.addSubject("Thanks for your support");
document.addCreationDate();
document.addTitle("Please read this");
//
document.addCreationDate();
document.add(new Paragraph("TITLE: ", bfBold18));
document.add(new Paragraph(title,bfBold12));
document.add(new Paragraph("\n"));
document.add(new Paragraph(String.format("Created on: " + date.toString())));
document.add(new Paragraph("DESCRIPTION: ", bfBold18));
document.add(new Paragraph(description,bfBold12));
document.add(new Paragraph("\n"));
htmlWorker.parse(new StringReader(notes_content));
// step 5
document.close();
response.setHeader("Content-disposition", "attachment; filename= testpdf1.pdf");
response.setContentType("application/pdf");
} catch (DocumentException e) {
e.printStackTrace();
}
}
} Please try the code urself. You'll see the pdf file created downloads automatically but when opened it show loading but doesnt loads like this
When the same file made at directory displayed in pdfGenServlet at 'C://BEProject//PreparedNotes//testpdf1.pdf' . When the testpdf1.pdf is opened manually it opens properly. Please help