-1

我正在尝试使用 itext 将 jsp 输出转换为 pdf 格式

我已经尝试了一些示例,但是它将jsp代码转换为pdf而不是jsp输出在这里是我的代码

 import com.lowagie.text.Document;  
 import com.lowagie.text.Paragraph;  
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;  
 import java.io.File;  
 import java.io.FileOutputStream;  
 public class PDFConversion  
 {  

  private void createPdf(String inputFile, String outputFile, boolean isPictureFile)  
 {  

      Rectangle pageSize = new Rectangle(2780, 2525);  
     Document pdfDocument = new Document(pageSize);  
      String pdfFilePath = "C:\\Users\\hp\\Desktop\\jsp_to_pdf.pdf";  
      try  
      {  
          FileOutputStream fileOutputStream = new FileOutputStream(pdfFilePath);  
         //PdfWriter writer = null;  
         PdfWriter writer = PdfWriter.getInstance(pdfDocument, fileOutputStream);  
          writer.open();  
          pdfDocument.open();  

           if (isPictureFile)  
          {  
           pdfDocument.add(com.lowagie.text.Image.getInstance("C:\\Users\\hp\\Documents\\NetBeansProjects\\pdf_print\\web\\example1.jsp"));  
          }  

          else  
          {  
          File file = new File("C:\\Users\\hp\\Documents\\NetBeansProjects\\pdf_print\\web\\example1.jsp");  
          pdfDocument.add(new Paragraph(org.apache.commons.io.FileUtils.readFileToString(file)));  
          }  
          pdfDocument.close();  
          writer.close();  
      }  
      catch (Exception exception)  
      {  
       System.out.println("Document Exception!" + exception);  
      }  
   }  
   public static void main(String args[])  

   {  

    PDFConversion pdfConversion = new PDFConversion();  






     pdfConversion.createPdf("example1.jsp","jsp_to_pdf.pdf", false);  

    }  

 } 

谢谢&问候沙达布·阿克拉姆·汗

4

1 回答 1

0

如果要提供下载 PDF 的功能,获取内容为byte[],然后执行以下操作:

response.setHeader("Content-Disposition", "attachment; filename=\"application.pdf\"");
        response.setHeader("Pragma", "public");
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Cache-Control", "public");
        response.setHeader("Content-Description", "File Transfer");
        response.setContentType("application/pdf");
        response.getOutputStream().write(pdfBytes);
于 2012-10-07T06:54:30.310 回答