如何将jsp页面转换为pdf格式?

问题描述 投票:1回答:4

我试图通过使用itext.i下载的itex.jars将jsp页面保存为pdf,并将其包含在我的项目中。之后我将采取什么措施将结果作为pdf页面?

javascript jsp pdf
4个回答
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文件应该是相同的格式。


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!");

    }

}

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">

-1
投票

您需要将html内容发送到Java servlet / controller并将xHTML保存为PDF。您需要使用HtmlPipelineContext和XMLWorker

看看这里:Converting HTML files to PDF

在这里:http://itextpdf.com/examples/iia.php?id=56

© www.soinside.com 2019 - 2024. All rights reserved.