0

I tried everything but not able to find any solution. I used iText, flying-saucer for converting HTML to PDF, but not able to do so.

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 Pdf {
    public static void main(String[] args) throws DocumentException, IOException {
         String File_To_Convert = "WebContent/index.html";
         String url = new File(File_To_Convert).toURI().toURL().toString();
         System.out.println(""+url);
         String HTML_TO_PDF = "ConvertedFile.pdf";
         OutputStream os = new FileOutputStream(HTML_TO_PDF); 


         ITextRenderer renderer = new ITextRenderer();
         renderer.setDocument(url);      
         renderer.layout();
         renderer.createPDF(os);       
         os.close();
    }
}

Error while running this piece of code:

 Flying Saucer: No configuration files found in classpath using URL: resources/conf/xhtmlrenderer.conf, resorting to hard-coded fallback properties.
Sébastien Le Callonnec
  • 26,254
  • 8
  • 67
  • 80
  • plz show what have you tried.. – Akash Rajbanshi May 22 '15 at 05:18
  • public class Pdf { public static void main(String[] args) throws DocumentException, IOException { String File_To_Convert = "WebContent/index.html"; String url = new File(File_To_Convert).toURI().toURL().toString(); System.out.println(""+url); String HTML_TO_PDF = "ConvertedFile.pdf"; OutputStream os = new FileOutputStream(HTML_TO_PDF); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(url); renderer.layout(); renderer.createPDF(os); os.close(); } } – user2681809 May 22 '15 at 05:47
  • WARNING: Flying Saucer: No configuration files found in classpath using URL: resources/conf/xhtmlrenderer.conf, resorting to hard-coded fallback properties. – user2681809 May 22 '15 at 05:47
  • @user2681809 you can add this to your question. Your code will be more readable – Mr Tsjolder from codidact May 22 '15 at 05:48

1 Answers1

4

I use ITextRenderer. I just support xhtml to pdf. That's why your need to convert html to xhtml at first.

xhtml to pdf

    String path_xhtml = "C:\example.xhtml";
    String path_pdf = "C:\example.pdf";
    String url = new File(path_xhtml).toURI().toURL().toString();
    OutputStream os = new FileOutputStream(path_pdf);
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(url);
    renderer.layout();
    renderer.createPDF(os);

html to xhtml by org.w3c.tidy.Tidy

     FileInputStream fis = new FileInputStream("C:\example.html");
     FileOutputStream fos = new FileOutputStream("C:\example.xhtml");   
     Tidy tidy = new Tidy();
     tidy.setXHTML(true);
     Document d = tidy.parseDOM(fis, fos);

update

Image on html file

There might be image on html file. The above code not enought to render image. Please reference Using Flying Saucer to Render Images to PDF In Memory

Pino
  • 7,468
  • 6
  • 50
  • 69
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131