0

I have problem to display the Arabic Characters from HTML Content in PDF.

this is the code of arabic.html :

<html>
<body style="font-family: Noto Naskh Arabic">
<table>
<tr>
<td dir="rtl">رقم التعميم رقم التعميم</td>
<td dir="rtl">رقم التعميم</td>
</tr>
</table>
</body>
</html>

I try with this java code :

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.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.CssAppliers;
import com.itextpdf.tool.xml.html.CssAppliersImpl;
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;
import java.nio.charset.Charset;


/**
 *
 * @author iText
 */

public class ParseHtml8 {
    public static final String DEST = "f:\\test.pdf";
    public static final String HTML = "f:\\arabic.html";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtml8().createPdf(DEST);
    }

    /**
     * Creates a PDF 
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) 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("f:\\NotoNaskhArabic-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(HTML), Charset.forName("UTF-8"));;
        // step 5
        document.close();
    }
}

the generated PDF does not contain any characters.

the size of the pdf file is : 6KB but it is empty

I used itextpdf-5.5.6.jar,xmlworker-5.5.6.jar

Please help me in this regard.

Nuwan Alawatta
  • 1,812
  • 21
  • 29
franco
  • 1,829
  • 6
  • 42
  • 75
  • Please upgrade from iText 5 + XML Worker to iText 7 + pdfHTML + pdfCalligraph. See the FAQ: https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-7-frequently-asked-questions-about-pdfhtml/how-convert-html-containing-arabichebrew-characters-pdf Arabic support in iText 5 is too limited for what you're trying to achieve. – Bruno Lowagie Jul 06 '18 at 04:31
  • This question duplicates the code of the accepted answer to the question [Arabic characters from html content to pdf using iText](https://stackoverflow.com/questions/30214147). Since that answer was accepted and received 5 upvotes, this current question is off-topic. See the [FAQ](https://stackoverflow.com/help/on-topic) where questions such as *"why isn't this code working?"* are discussed. Furthermore, the person who posted this question posted [a follow-up question two hours after this one](https://stackoverflow.com/questions/51200970). It would have been better to update this question. – Bruno Lowagie Jul 06 '18 at 06:00

0 Answers0