我试图使用itext将jsp输出转换为pdf格式 使用itext和我在java中有很多知识我刚刚开始 编程请帮我将jsp输出转换为pdf
我曾尝试过一些例子,但它将jsp代码转换为pdf但不是jsp out put 这是我的代码
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);
}
}
感谢&安培;问候 shadab akram khan
答案 0 :(得分: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);