码
package test.test1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.Pipeline;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
public class Table {
File oFile = new File("c:\\test\\1.pdf");
public static Document document = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer =null;
public static void main(String[] args) throws IOException, DocumentException {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\test\\1.pdf"));
document.open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
CSSResolver cssResolver = new StyleAttrCSSResolver();
InputStream cssIS = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("css\\index.css");
CssFile cssfile = XMLWorkerHelper.getCSS(cssIS);
InputStream cssIS1 = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("css\\bootstrap.css");
CssFile cssfile1 = XMLWorkerHelper.getCSS(cssIS1);
cssResolver.addCss(cssfile);
cssResolver.addCss(cssfile1);
Pipeline<?> pipeline =
new CssResolverPipeline(cssResolver,
new HtmlPipeline(htmlContext,
new PdfWriterPipeline(document, writer)));
XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("resources\\index.html"),
new FileInputStream("resources\\css\\bootstrap.css"));
document.close();
System.out.println( "PDF Created!" );
}
}
说明 我正在尝试使用java中的itext库生成html到pdf文件。
输入: index.html,bootstrap.css,index.css
输出: pdf文件
例外:
Exception in thread "main" java.lang.NumberFormatException: For input string: "100%"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
at java.lang.Float.parseFloat(Float.java:422)
at com.itextpdf.tool.xml.css.FontSizeTranslator.getFontSize(FontSizeTranslator.java:186)
at com.itextpdf.tool.xml.css.FontSizeTranslator.translateFontSize(FontSizeTranslator.java:165)
at com.itextpdf.tool.xml.html.AbstractTagProcessor.startElement(AbstractTagProcessor.java:120)
at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.open(HtmlPipeline.java:105)
at com.itextpdf.tool.xml.XMLWorker.startElement(XMLWorker.java:103)
at com.itextpdf.tool.xml.parser.XMLParser.startElement(XMLParser.java:372)
at com.itextpdf.tool.xml.parser.state.TagEncounteredState.process(TagEncounteredState.java:104)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:237)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:215)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:176)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:222)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:226)
at test.test1.Table.main(Table.java:160)
所以在这里我认为字体在这里遇到问题,请告诉我我到底做了什么?
答案 0 :(得分:2)
itext的XML工作者不支持大多数CSS。
见这里:http://demo.itextsupport.com/xmlworker/itextdoc/CSS-conformance-list.htm
由于bootstrap包含非常高级的CSS,这几乎注定要失败......
与bootstrap相比,您需要使用非常有限的样式编写自己的CSS文件