我正在尝试使用 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);
}
}
谢谢&问候沙达布·阿克拉姆·汗