java - 使用 itext 进行 pdf 转换时遇到问题

标签 java pdf pdf-generation itext

这是我用于生成 PDF 的 Java 类。我正在使用 iText 生成 PDF。

public class pdfgen {
 public void createPdf(String inputFile, String outputFile, boolean isPictureFile)    

 {    
     Rectangle pageSize = new Rectangle(2780, 2525);    
      Document pdfDoc = new Document(pageSize); 
      String pdfFilePath = outputFile;    
      try  {
          FileOutputStream fileOutputStream = new FileOutputStream(pdfFilePath);  
          PdfWriter writer = null;    
          writer = PdfWriter.getInstance(pdfDoc, fileOutputStream);
          writer.open(); 
          pdfDoc.open();
          if (isPictureFile){
              pdfDoc.add(com.itextpdf.text.Image.getInstance(inputFile));
          }
          else{
              URL url=new URL(inputFile);
              URLConnection conn = url.openConnection();
              BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
              String line; while ((line = in.readLine()) !=null ) {
                  System.out.println(line);
              }
              System.out.println(inputFile);
              in.close(); 
              File file = new File(inputFile);
              pdfDoc.add(new  Paragraph(org.apache.commons.io.FileUtils.readFileToString(file)));

          }
          pdfDoc.close();
          writer.close();
      }catch(DocumentException e){
          System.err.println("The error has occured in the document");
      }catch(FileNotFoundException e){
          System.err.println("Your file is not found.");
      }
      catch(Exception e){
          System.err.println("Exception: "+e);
      }
 }

}

这是我的 JSP 文件,我在其中调用上面的类

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.Vector"  %>
<%@page import="com.dalkin.pdfgen" %>
<% Vector result=(Vector)request.getAttribute("val");%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Output </title>
</head>
<body>

<%Vector names; %>
<%if(arrcol.size()!=0){  %>
            <div style="width:1024px;">
            <table cellpadding="5" cellspacing="5">

            <tr>                

            <td>
            <%for(int q=0;q<result.size();q+=3){ %>
            <div style="background:url(sample.gif) no-repeat; height:320px; width:500px; float:left;">
            <input type="text" size="50" value=<%=result.get(q) %>>
            <input type="text" size="50" value=  <%=result.get((q+1))%>>    
            <input type="text" size="50" value=<%=result.get((q+2))%>>              
            </div>

            <%}} %>                 
            </td>
            </tr>

</table>
</div>

<%pdfgen pf = new pdfgen();  
pf.createPdf("http://localhost:8080/New/FirstServlet","D:\\first.pdf",false);%>  

</body>
</html>

当我运行程序时,我收到 FileNotFoundException “http://localhost:8080/New/FirstServlet” 找不到您的文件。 谁能帮助我哪里做错了?

最佳答案

您可以这样调用 PDF 创建方法

pf.createPdf("http://localhost:8080/New/FirstServlet","D:\\first.pdf",false);

在该方法中,您可以使用第一个参数(名为 inputFile),如下所示:

File file = new File(inputFile);

“http://localhost:8080/New/FirstServlet”没有文件,所以

FileUtils.readFileToString(file)

除了你得到的异常之外,肯定会失败。

在执行之前的代码中

URL url=new URL(inputFile);
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

然后使用 line = in.readLine() 迭代这些行。除了打印行之外,您还可以将行附加到某些 StringBuilder 并使用内置的 String

pdfDoc.add(new  Paragraph(...));

关于java - 使用 itext 进行 pdf 转换时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13080859/

相关文章:

java - Jsoup:提取内部标签

javascript - 错误: write EPIPE on sendFile()?

java - 使用 PdfDocument 在 Android 中生成自定义尺寸的 PDF

java - 如何使用iText将内容写入pdf?

python - 为什么 Matplotlib 的 PdfPages 将选项卡打印为框(字体编码)?

java - onClick 按钮导致崩溃

java - 如果特定的外部程序启动,则让 Java 程序自行终止

java - 自定义枚举属性给出错误 String types not allowed

Php Regex - 如何选择是否等于某物

java - Android sqlite 升级时出现重复列