I am using iText
to convert html to pdf and when I try to register a fontProvider
I get EOFException
, here is the relevant code:
XMLWorkerFontProvider fontProvider =new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register("resources/fonts/NotoSansHebrew-Regular.ttf"); //this row throws the error
I had placed NotoSansHebrew-Regular.ttf
under resources, like iText-rtl.
Here is the full code:
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();
}
main:
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");
}
And the stack:
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)
Looks like the path to your font "resources/fonts/NotoSansHebrew-Regular.ttf
is invalid.
Try to use absolute path.