gpt4 book ai didi

java - 在 java 中使用 itext 生成 html 到 pdf

转载 作者:太空宇宙 更新时间:2023-11-04 11:38:10 25 4
gpt4 key购买 nike

描述:
输出:pdf文件
输入 : index.css, bootstrap.min.css, index.html

问题:如果我在没有 bootsrap 的情况下使用 index.css 文件,它工作正常,但是当我使用 bootstrap 时,它会抛出异常。

代码在这里:

package test.test1;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.codec.Charsets;

import com.google.common.io.CharStreams;
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.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;

public class Table {



File oFile = new File("c:\\test\\1.pdf");
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer =null;

public Table() throws IOException, DocumentException {
oFile.createNewFile();
writer=PdfWriter.getInstance(document,new FileOutputStream(oFile));


InputStream htmlpathtest = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("index.html");
String htmlstring = CharStreams.toString(new InputStreamReader(htmlpathtest, Charsets.UTF_8));

InputStream is = new ByteArrayInputStream(htmlstring.getBytes());

ByteArrayOutputStream baos = new ByteArrayOutputStream();

PdfWriter writer = PdfWriter.getInstance(document, baos);

writer.setInitialLeading(12.5f);

document.open();

HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);

htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

// CSS
CSSResolver cssResolver = new StyleAttrCSSResolver();
InputStream csspathtest = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("css\\index.css");

InputStream csspathtest1 = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("css\\bootstrap.min.css");

CssFile cssfiletest = XMLWorkerHelper.getCSS(csspathtest);
cssResolver.addCss(cssfiletest);
cssResolver.addCss(XMLWorkerHelper.getCSS(csspathtest1));


Pipeline<?> pipeline = new CssResolverPipeline(cssResolver,
new HtmlPipeline(htmlContext, new PdfWriterPipeline(
document, writer)));

XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(worker);
p.parse(is);
document.close();
}
public static void main(String[] args) throws IOException, DocumentException { new Table();}
}

异常:

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:188) at test.test1.Table.(Table.java:95) at test.test1.Table.main(Table.java:104)

最佳答案

itext HTML 渲染器对 AFAIK CSS 的支持非常有限。我最近使用了一个更完整的 HTML 到 PDF 库,称为 flying-saucer支持 CSS2 和一些 CSS3 功能。 flying-saucer 使用 itext 作为后端。您应该试一试 - 它可能不支持所有 Bootstrap 功能,但可能仍能满足您的要求。

关于java - 在 java 中使用 itext 生成 html 到 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31578226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com