1

我正在尝试使用 iTextPDF xmlWorker 工具在 Android 中将 HTML 转换为 PDF。我可以得到PDF文件。但有些字符在 PDF 创建后消失了。

我的代码:

private void pdfCreate (String filePath) {

        try {

            String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
            fileNameAndPath = filePath + File.separator + timeStamp + ".pdf";


            String d1 = "<html><head></head><body>İstanbul, Ankara, İzmir, Çanakkale</body></html>";

            OutputStream myFile = new FileOutputStream(new File(fileNameAndPath));
            Document document = new Document();

            document.addCreationDate();
            document.setPageSize(PageSize.A4);
            document.setMargins(36, 36, 36, 36);
            document.setMarginMirroring(true);


            PdfWriter writer = PdfWriter.getInstance(document, myFile);
            document.open();

            XMLWorkerHelper worker = XMLWorkerHelper.getInstance();

            InputStream is;

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                is = new ByteArrayInputStream(d1.getBytes(StandardCharsets.UTF_8));
            } else
                is = new ByteArrayInputStream(d1.getBytes("UTF-8"));

            String FONT = "assets/fonts/arial.ttf";
            XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
            fontImp.register(FONT);

            worker.parseXHtml(writer, document, is, Charset.forName("UTF-8"), fontImp);

            document.close();
            myFile.close();



    }

结果:

“İ”、“ı” 消失。我尝试了 stringbuilder 和其他一些方法。

PDF 查看

4

1 回答 1

2

我终于找到了解决方案。刚刚添加了body style font-family,如外部字体名称。

最后的代码

private void pdfCreate (String filePath) {

        try {

            String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
            fileNameAndPath = filePath + File.separator + timeStamp + ".pdf";

    String d1 = "<html><head></head><body style=\"font-family:arial\">İstanbul, Ankara, İzmir, Çanakkale</body></html>";


            OutputStream myFile = new FileOutputStream(new File(fileNameAndPath));
            Document document = new Document();

            document.addCreationDate();
            document.setPageSize(PageSize.A4);
            document.setMargins(36, 36, 36, 36);
            document.setMarginMirroring(true);


            PdfWriter writer = PdfWriter.getInstance(document, myFile);
            document.open();

            XMLWorkerHelper worker = XMLWorkerHelper.getInstance();

            InputStream is;

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                is = new ByteArrayInputStream(d1.getBytes(StandardCharsets.UTF_8));
            } else
                is = new ByteArrayInputStream(d1.getBytes("UTF-8"));

            String FONT = "assets/fonts/arial.ttf";
            XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
            fontImp.register(FONT);

            worker.parseXHtml(writer, document, is, Charset.forName("UTF-8"), fontImp);

            document.close();
            myFile.close();



    }
于 2016-06-23T16:37:12.050 回答