-1

I tried to convert jsp page to save as pdf by using itext.i downloaded itex.jars and included those in to my project.after that what i will do to get result as pdf page?

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
Mythily M
  • 17
  • 1
  • 1
  • 3

4 Answers4

0

Check this link.. http://www.pd4ml.com/examples.htm

Example :

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." );  
    }

The statement jt.doConversion("http://pd4ml.com/sample.htm", "D:/pd4ml.pdf");
instead of "http://pd4ml.com/sample.htm" i have to pass dynamic page url and if page is converted into pdf format so that pdf file should be in same format.

Vignesh Shiv
  • 1,129
  • 7
  • 22
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!");

    }

}
  • 3
    Make a readable, compact and clear answer, take a look to https://stackoverflow.com/help/how-to-answer, and edit your answer – GGO Mar 19 '18 at 08:50
  • Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written – WhatsThePoint Mar 20 '18 at 15:21
  • @GGO While I agree that this isn't a good answer, it *is* still an attempt to answer the question, so it shouldn't be deleted. You're supposed to choose Looks OK in the Low Quality Posts review on code-only answers. See https://meta.stackoverflow.com/q/256359/4284627. You did the right thing in commenting, but you shouldn't have recommended deletion. – Donald Duck Mar 20 '18 at 15:29
0

We are using pd4ml for downloading a content of JSP in PDF form. You can get the jars here.

Keep this code in your JSP after all the imports

<pd4ml:transform 
      inline="false"
      fileName="application.pdf"
      screenWidth="815"
      pageFormat="A4"
      pageOrientation="portrait"
      pageInsets="10,10,10,10,points">
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
  • Hi Arun, I am trying to convert .jsp to pdf. Is there any link to example which you can provide in order to understand how exactly one can do it ? Thanks. – user523956 Jul 21 '20 at 09:29
  • There are some online converters for JSP to PDF, I don't know about the credibility. Otherwise you can look at this https://stackoverflow.com/a/29387324/7218656 – Arun Sudhakaran Jul 21 '20 at 15:07
-1

You need to send the html contents to a Java servlet/controller and save the xHTML to PDF. You'll need to use HtmlPipelineContext and XMLWorker

Have a look here: Converting HTML files to PDF

and here: http://itextpdf.com/examples/iia.php?id=56

Community
  • 1
  • 1
kmandov
  • 3,130
  • 16
  • 15