package sandbox.xmlworker;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorker;
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.parser.XMLParser;
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;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class D04_ParseHtmlCss {
public static final String SRC = "/home/xxx/workspace/DemoTransformer/src/data/result.html";
public static final String CSS = "/home/xxx/workspace/DemoTransformer/src/data/beyanname.css";
public static final String DEST = "/home/xxx/workspace/DemoTransformer/src/data/resultpdffileson.pdf";
public void createPdf(File file) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
writer.setInitialLeading(12.5f);
// step 3
document.open();
// step 4
// CSS
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(CSS));
cssResolver.addCss(cssFile);
// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.parse(new FileInputStream(SRC));
// step 5
document.close();
}
/**
* Main method
*/
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new D04_ParseHtmlCss().createPdf(new File(DEST));
}
}
此代码来自itext sandbox:http://developers.itextpdf.com/examples/xml-worker-itext5/xml-worker-examples
我也使用itext-pdf5.4.5和xml-worker 5.4.5
但它出现了这个错误,我无法弄清楚如何解决这个问题
Exception in thread "main" com.itextpdf.tool.xml.exceptions.RuntimeWorkerException: Invalid nested tag head found, expected closing tag link.
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:134)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:395)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:70)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:235)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:213)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:174)
at sandbox.xmlworker.D04_ParseHtmlCss.createPdf(D04_ParseHtmlCss.java:59)
at sandbox.xmlworker.D04_ParseHtmlCss.main(D04_ParseHtmlCss.java:71)
html文件的头部是:
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>KDV1</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="beyanname.css">
</head>
我使用itext
从xml和xslt文件生成此代码答案 0 :(得分:0)
您的工具正在使用XML解析器来解析HTML。虽然两者看起来非常相似,they are not exactly the same。您的错误是由非claused <link>
标记引起的,该标记为valid in HTML但不是XML格式。这导致您的解析器抛出异常。因此,对您而言,最简单的解决方案是使用HTML解析器替换XMLParser
,或确保您的HTML文件位于XHTML
XML compliant