How To convert a large table to PDF using flying saucer?

Ankur picture Ankur · Nov 27, 2012 · Viewed 7k times · Source

I have a html with large number of columns(you can find the sample at this link)

Now When I try to convert it to PDF using flying saucer(jar link recompiled to work with iText 2.1.X), the generated PDF has truncated Columns

Is there some way to make Flying saucer to either break the table or to increase the width of the page according to the html content?

This is the code that I am using

String doc = file.toURI().toURL().toString();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc);
String outputFile = "test.pdf";
OutputStream os = new FileOutputStream(outputFile);
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();

Where file is the html which I am trying to convert.

Answer

SRy picture SRy · Dec 1, 2012

Use YAHP library.This is the best library I have worked so far to convert HTML to PDF. This is written on the top of flying saucer which is a big disappointment as compared to it's popularity.It won't even render simple input text boxes.So, I turned to YAHP library which is excellent for your case.

try this code after you get all the jars related to this library.

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.allcolor.yahp.converter.CYaHPConverter;
import org.allcolor.yahp.converter.IHtmlToPdfTransformer;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyHtmlSerializer;
import org.htmlcleaner.TagNode;

public class YahpHtmlToPdf {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) {
        try{
            CleanerProperties props = new CleanerProperties();
            props.setTranslateSpecialEntities(true);
            props.setTransResCharsToNCR(true);
            props.setOmitComments(true);
            TagNode tagNode = new HtmlCleaner(props).clean(new File("C:\\Users\\MyComputer\\Desktop\\aspose.html"));
            String newString=new PrettyHtmlSerializer(props).getAsString(tagNode, "ISO-8859-1");
            CYaHPConverter converter = new CYaHPConverter();
            File fout = new File("C:\\sample\\aspose.pdf");
            FileOutputStream out = new FileOutputStream(fout);
            Map properties = new HashMap();
            List headerFooterList = new ArrayList();
            properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);
            converter.convertToPdf(newString,IHtmlToPdfTransformer.A1P,headerFooterList, "file:///temp/",out,properties);
            out.flush();
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }   

}

enter image description hereThis is screenshot of pdf generated with your html. .You can specify page size like this IHtmlToPdfTransformer.A1P.