繁体   English   中英

使用iText将HTML表单转换为PDF

[英]Converting HTML form to PDF using iText

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

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();
    }
}}

输入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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM