使用iText将HTML表单转换为PDF

时间:2014-01-14 11:04:23

标签: java html forms pdf itext

我尝试使用“生成pdf”按钮创建HTML表单。该按钮成功将HTML表单转换为PDF,但它不会将HTML中的文本字段值(firstname)检索为PDF。

carrier.html

<form id="carrierform" method="post" action="PDFServlet">
    <div  class="wrapper">
     <span id="wrappspan">First Name:*</span>
     <input type="text" class="input" id="firstname">
    </div>
     <div  class="wrapper">
     <span id="wrappspan">Middle Name:</span>
     <input type="text" class="input" id="middlename" >
    </div>
     <div  class="wrapper">
     <span id="wrappspan">Last Name:*</span>
     <input type="text" class="input" id="lastname" >
    </div>
    <div class="wrapper">
          <span id="wrappspan">Date of birth:*</span>
          Day<input id="spinner1">
          Month<input id="spinner2">
          Year<input id="spinner3">
    </div> 
    <div class="wrapper" >
        <span id="wrappspan">Sex:*</span> 
        <input type="radio" name="sex" value="Male" size="17">Male
        <input type="radio" name="sex" value="Female" size="17">Female
    </div>
    <div class="wrapper"> 
        <span id="wrappspan">Degree:*</span> 
         <select>
           <option class="selectWrap" value="1">B-TECH</option>
           <option class="selectWrap" value="2">M-TECH</option>
           <option class="selectWrap" value="3">MS</option>
         </select> 
    </div>   
     <div class="wrapper"> 
        <span id="wrappspan">Type:</span> 
         <select>
           <option class="selectWrap" value="1">Full Time</option>
           <option class="selectWrap" value="2">Part Time</option>
           <option class="selectWrap" value="3">Open</option>
         </select> 
    </div>    
   <div  class="wrapper">
        <span id="wrappspan">Resume:*</span>
    <input id="filebrowse" type="file" name="datafile" size="40">
    <input type="submit" value="upload" />      
   </div>               
   <div class="wrapperbutton">
     <!--    <button id="getvalue">Submit</button> -->

   </div>       


    <button id="cmd" >generate PDF</button>  
 </form>

的web.xml

   <servlet>
    <servlet-name>PDFServlet</servlet-name>
    <servlet-class>pdfconverter.PDFServlet</servlet-class>

  </servlet>

  <servlet-mapping>
    <servlet-name>PDFServlet</servlet-name>
    <url-pattern>/PDFServlet</url-pattern>
  </servlet-mapping>  

PDFServlet.java

public class PDFServlet extends HttpServlet {

public void init(ServletConfig config) throws ServletException{
    super.init(config);
}

public void doGet(HttpServletRequest request, 
        HttpServletResponse response) 
        throws ServletException, IOException{
    doPost(request, response);
}

public void doPost(HttpServletRequest request, 
        HttpServletResponse response) 
        throws ServletException, IOException{

    response.setContentType("application/pdf"); // Code 1
    Document document = new Document();
    try{

        PdfWriter.getInstance(document, 
            response.getOutputStream()); // Code 2
        document.open();

        // Code 3

        PdfPTable table = new PdfPTable(2);
        table.addCell("Firstname");
        table.addCell(request.getParameter("firstname"));
        table.addCell("Middlename");
        table.addCell("4");
        table.addCell("Lastname");
        table.addCell("6");     
        table.addCell("Date of birth");
        table.addCell("6"); 
        table.addCell("Sex");
        table.addCell("6"); 
        table.addCell("Degree");
        table.addCell("6"); 
        table.addCell("Type");
        table.addCell("6"); 
        // Code 4
        document.add(table);        
        document.close(); 
    }catch(DocumentException e){
        e.printStackTrace();
    }
}}

1 个答案:

答案 0 :(得分:0)

为输入标记提供name而不是id

<form id="carrierform" method="post" action="PDFServlet">
<div  class="wrapper">
 <span id="wrappspan">First Name:*</span>
 <input type="text" class="input" name="firstname"/>
</div>
 <div  class="wrapper">
 <span id="wrappspan">Middle Name:</span>
 <input type="text" class="input" name="middlename" />
</div>
 <div  class="wrapper">
 <span id="wrappspan">Last Name:*</span>
 <input type="text" class="input" name="lastname" />
</div>
<div class="wrapper">
      <span id="wrappspan">Date of birth:*</span>
      Day<input name="spinner1"/>
      Month<input name="spinner2"/>
      Year<input name="spinner3"/>
</div> 
<div class="wrapper" >
    <span id="wrappspan">Sex:*</span> 
    <input type="radio" name="sex" value="Male" size="17"/>Male
    <input type="radio" name="sex" value="Female" size="17"/>Female
</div>
<div class="wrapper"> 
    <span id="wrappspan">Degree:*</span> 
     <select>
       <option class="selectWrap" value="1">B-TECH</option>
       <option class="selectWrap" value="2">M-TECH</option>
       <option class="selectWrap" value="3">MS</option>
     </select> 
</div>   
 <div class="wrapper"> 
    <span id="wrappspan">Type:</span> 
     <select name="class">
       <option class="selectWrap" value="1">Full Time</option>
       <option class="selectWrap" value="2">Part Time</option>
       <option class="selectWrap" value="3">Open</option>
     </select> 
</div>    
<div  class="wrapper">
    <span id="wrappspan">Resume:*</span>      
<input id="filebrowse" type="file" name="datafile" size="40"/>
<input type="submit" value="upload" />      
</div>               
<button id="cmd" >generate PDF</button>  
</form>