注册提供程序的Java会抛出EOFException

时间:2017-06-21 13:33:41

标签: java itext eofexception

我正在使用iText将html转换为pdf,当我尝试注册fontProvider时,我得到EOFException,这是相关代码:

XMLWorkerFontProvider fontProvider =new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register("resources/fonts/NotoSansHebrew-Regular.ttf"); //this row throws the error

我已将NotoSansHebrew-Regular.ttf放在资源下,例如iText-rtl

以下是完整代码:

public void createPdf(String file, String htmlString) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer =
            PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        // Styles
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        XMLWorkerFontProvider fontProvider =
            new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontProvider.register("resources/fonts/NotoSansHebrew-Regular.ttf");
        CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
        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(htmlString), Charset.forName("UTF-8"));
        // step 5
        document.close();
    }

主:

public static void main(String[] args) throws IOException, DocumentException {
    Converter test = new Converter();
    test.createPdf("C://Users//t207322//Desktop//digitalCheck//test.pdf", "C://Users//t207322//Desktop//digitalCheck//test.html");
}

堆栈:

Exception in thread "main" ExceptionConverter: java.io.EOFException
    at com.itextpdf.text.pdf.RandomAccessFileOrArray.readInt(RandomAccessFileOrArray.java:453)
    at com.itextpdf.text.pdf.TrueTypeFont.process(TrueTypeFont.java:819)
    at com.itextpdf.text.pdf.TrueTypeFont.<init>(TrueTypeFont.java:498)
    at com.itextpdf.text.pdf.BaseFont.getAllFontNames(BaseFont.java:1232)
    at com.itextpdf.text.FontFactoryImp.register(FontFactoryImp.java:451)
    at com.itextpdf.text.FontFactoryImp.register(FontFactoryImp.java:438)
    at pdf.utils.Converter.createPdf(Converter.java:110)
    at pdf.utils.Converter.main(Converter.java:218)

1 个答案:

答案 0 :(得分:1)

您的字体"resources/fonts/NotoSansHebrew-Regular.ttf的路径看起来无效。 尝试使用绝对路径。