Html到PDF,<pre> not coming good in PDF

时间:2016-03-02 10:45:41

标签: itext itextpdf html-to-pdf

I am trying to convert html to pdf with iText API(itextpdf.5.5.7.jar, xmlworker 5.5.7.jar), everything is good except the text inside the pre tag.

In html the text inside the pre is good but in PDF the formatting is totally gone and simply coming in normal lines without formatting. I checked the blogs but I did not find correct answer.


Please find the screen shots for HTML and PDF.

HTML screen: HTML screen

PDF screen: PDF screen

this is my code

String pdfFileName="C:\test.pdf";    
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
PdfWriter writer = PdfWriter.getInstance(document,
    new FileOutputStream(pdfFileName));
writer.setInitialLeading(12.5f);
document.open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
CSSResolver cssResolver = XMLWorkerHelper.getInstance()
        .getDefaultCssResolver(true);       
Pipeline<?> pipeline = new CssResolverPipeline(cssResolver,
        new HtmlPipeline(htmlContext, new PdfWriterPipeline(document,
                writer)));      
XMLWorker worker = new XMLWorker(pipeline, true);        
XMLParser p = new XMLParser(worker);         
File input = new File(completeHtmlFilePath);
p.parse(new InputStreamReader(new FileInputStream(input), "UTF-8"));        
document.close();           
return pdfFileNameWithPath; 

0 个答案:

没有答案