【问题标题】:Converting HTML form to PDF using iText使用 iText 将 HTML 表单转换为 PDF
【发布时间】:2014-01-14 11:04:23
【问题描述】:

我尝试使用“生成 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();
    }
}}

【问题讨论】:

  • @Andrew Thompson ..你能帮我吗?
  • 一个人知道如何编辑问题,并不意味着他们知道答案!我出于一个原因编辑了这篇文章。一个整齐的帖子更喜欢引起注意和答案。不要误会我的意思,我并不是说这是保证的答案,而只是更有可能。但是给你一个提示,停止使用像'u这样的无意义拼写'和'请'。这些不是我们发送的短信,而且如今大多数手机和平板电脑都具有“滑动”功能和自动更正功能。 'l33t speak' 已经死了,我会高兴地在它的坟墓上跳舞。这真的激怒了我。 :-/

标签: java html forms pdf itext


【解决方案1】:

输入标签使用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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-04
    相关资源
    最近更新 更多