Search code examples
itextflying-saucer

Arabic problems with converting html to PDF using ITextRenderer


When I use ITextRenderer converting html to PDF.this is my code

ByteArrayOutputStream out = new ByteArrayOutputStream();

ITextRenderer renderer = new ITextRenderer();
String inputFile = "C://Users//Administrator//Desktop//aaa2.html";
String url = new File(inputFile).toURI().toURL().toString();
renderer.setDocument(url);
renderer.getSharedContext().setReplacedElementFactory(
        new B64ImgReplacedElementFactory());
    // 解决阿拉伯语问题
ITextFontResolver fontResolver = renderer.getFontResolver();
try {
    fontResolver.addFont("C://Users//Administrator//Desktop//arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} catch (DocumentException e) {
    e.printStackTrace();
}

renderer.layout();
OutputStream outputStream = new FileOutputStream("C://Users//Administrator//Desktop//HTMLasPDF.pdf");
renderer.createPDF(outputStream, true);
/*PdfWriter writer = renderer.getWriter();

writer.open();
writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
OutputStream outputStream2 = new FileOutputStream(  "C://Users//Administrator//Desktop//HTMLasPDFcopy.txt");
renderer.createPDF(outputStream2);*/
renderer.finishPDF();
out.flush();
out.close();

Actual PDF Result: enter image description here

Expected PDF Result: enter image description here

How to make arabic ligature?


Solution

  • I change the way to convert pdf by using wkhtmltopdf.