Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Write data to PDF

Here, we are going to see how we can write data into PDF using servlet technology. We are simply writing some data using servlet and it will get displayed in the PDF.

To create such application, you need to have the spdf.jar file. If you download this example, you will get the example with jar file.

Example to write data into PDF using servlet

Let’s see the simple example of writing data into PDF using servlet. In this example, we have mentioned the content type application/pdf that must be specified to display data in the PDF format.

  1. package com.Kreationnext;
  2. import*;
  3. import java.util.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6. import com.darwinsys.spdf.PDF;
  7. import com.darwinsys.spdf.Page;
  8. import com.darwinsys.spdf.Text;
  9. import com.darwinsys.spdf.MoveTo;
  10. public class ServletPDF extends HttpServlet {
  11. public void doGet(HttpServletRequest request,
  12.         HttpServletResponse response) throws IOException {
  13. PrintWriter out = response.getWriter();
  14. response.setContentType(“application/pdf”);
  15. response.setHeader(“Content-disposition”,“inline; filename=’Kreationnext.pdf'”);
  16. PDF p = new PDF(out);
  17. Page p1 = new Page(p);
  18. p1.add(new MoveTo(p, 200700));
  19. p1.add(new Text(p, “”));
  20. p1.add(new Text(p, “by Sonoo Jaiswal”));
  21. p.add(p1);
  22. p.setAuthor(“Ian F. Darwin”);
  23. p.writePDF();
  24. }
  25. }