我试图通过使用itext.i下载的itex.jars将jsp页面保存为pdf,并将其包含在我的项目中。之后我将采取什么措施将结果作为pdf页面?
答案 0 :(得分:0)
检查此链接.. http://www.pd4ml.com/examples.htm
示例:
public static void main(String[] args) {
try {
PdfViewerStarter jt = new PdfViewerStarter();
jt.doConversion("http://pd4ml.com/sample.htm", "D:/pd4ml.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
public void doConversion( String url, String outputPath )
throws InvalidParameterException, MalformedURLException, IOException {
File output = new File(outputPath);
java.io.FileOutputStream fos = new java.io.FileOutputStream(output);
PD4ML pd4ml = new PD4ML();
pd4ml.setHtmlWidth(userSpaceWidth);
pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));
pd4ml.useTTF("c:/windows/fonts", true);
pd4ml.render(new URL(url), fos);
fos.close();
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(output);
} else {
System.out.println("Awt Desktop is not supported!");
}
System.out.println( outputPath + "\ndone." );
}
声明jt.doConversion(" http://pd4ml.com/sample.htm"," D:/pd4ml.pdf");
而不是" http://pd4ml.com/sample.htm"我必须传递动态页面网址,如果页面转换为pdf格式,以便pdf文件格式相同。
答案 1 :(得分:0)
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
public class GenratePdf {
public static void generatePDF(String inputHtmlPath, String outputPdfPath)
{
try {
String url = new File(inputHtmlPath).toURI().toURL().toString();
System.out.println("URL: " + url);
OutputStream out = new FileOutputStream(outputPdfPath);
//Flying Saucer part
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(out);
out.close();
}
catch (DocumentException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
String inputFile = "D:\\mailPages\\pdfTest.jsp";
String outputFile = "D:/mailPages/testpdf.pdf";
generatePDF(inputFile, outputFile);
System.out.println("Done!");
}
}
答案 2 :(得分:0)
我们正在使用pd4ml
以PDF格式下载JSP
的内容。您可以得到罐子here。
所有导入后,将此代码保留在JSP
中
<pd4ml:transform
inline="false"
fileName="application.pdf"
screenWidth="815"
pageFormat="A4"
pageOrientation="portrait"
pageInsets="10,10,10,10,points">
答案 3 :(得分:-1)
您需要将html内容发送到Java servlet / controller并将xHTML保存为PDF。 您需要使用HtmlPipelineContext和XMLWorker