Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Servlets; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Image; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.*; import com.itextpdf.text.Phrase; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Allan */ @WebServlet(name = "GenerarPDF", urlPatterns = { "/GenerarPDF" }) public class GenerarPDF extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ ServletContext d = getServletContext(); Document document = new Document(); com.itextpdf.text.Font catFont = new com.itextpdf.text.Font( com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 14, com.itextpdf.text.Font.BOLD); try { PdfWriter.getInstance(document, response.getOutputStream()); document.open(); Paragraph preface = new Paragraph(); Paragraph title = new Paragraph("\n\n Reporte \nEstadisticas\n\n", catFont); title.setAlignment(Element.ALIGN_CENTER); String urllogo = "/img/header-ittoluca2.png"; String absoluturl = d.getRealPath(urllogo); Image logo = Image.getInstance(absoluturl); logo.scaleAbsoluteWidth(500f); logo.scaleAbsoluteHeight(60f); logo.setAbsolutePosition(50f, 750f); preface.add(logo); document.add(title); document.add(preface); document.add(new Paragraph("\n\n")); document.close(); } catch (DocumentException e) { } } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }