1

I want to convert html to pdf with itext in my java code,but it seems that the strong tag and em tag don't work.I try to search solutions in the network and tracing my code to find out why,but failed.Here are my java code , my html file and generated pdf snapshot below.

   public static boolean html2pdf(String htmlContent, String path) {
        try (FileOutputStream fos = new FileOutputStream(path);
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(htmlContent.getBytes())) {
            Document document = new Document();
            PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
            document.open();
            XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, byteArrayInputStream,
                    Charset.forName("UTF-8"), new MyFontProvider());
            document.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
static class MyFontProvider extends XMLWorkerFontProvider {
    @Override
    public Font getFont(String fontname, String encoding, float size, int style) {
        Font cnFont = null;
        try {
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            cnFont = new Font(baseFont);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (cnFont == null) {
            cnFont = super.getFont(fontname, encoding, size, style);
        }
        return cnFont;
    }
}

and my dependencies:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>
<dependency>
    <groupId>com.itextpdf.tool</groupId>
    <artifactId>xmlworker</artifactId>
    <version>5.5.13</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>

<!DOCTYPE html>
<html>
 <head>
  <title>title</title>
 </head>
 <body>
  <p >
   <strong>overstrike</strong>
  </p>
  <p>
   <em>incline</em>
  </p>
 </body>
</html>

enter image description here


Solved

I solved this by using itext7.

Cœur
  • 37,241
  • 25
  • 195
  • 267
elaine
  • 11
  • 2
  • Why are you using a *maintenance* release of an old iText version instead of the latest version? I'm inclined to close this question as a duplicate of https://stackoverflow.com/questions/47895935/ – Bruno Lowagie Apr 28 '18 at 09:13

1 Answers1

0

In MyFontProvider, you are ignoring the style parameter if the first attempt to load a font succeeds (which presumably it does).

Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • Thank you, but I'd like to choose the style basing on the html file rather than set a default style for the pdf file. – elaine Apr 28 '18 at 08:16
  • 1
    Yes, if you use the style parameter, that's what you will do. I am sorry I am not an expert in iText, so I couldn't figure out how to do that. – Robin Green Apr 28 '18 at 08:19
  • 1
    Asian font families usually consist of a single *regular* font, no bold, italic or bold-italic font. If you needed a Western font family, you'd provide different fonts (e.g. Arial, Arial Bold, Arial Italic,...) to cater for different styles. With Asian fonts, iText needs to mimic bold and italic. I think you need iText 7 to do that. – Bruno Lowagie Apr 28 '18 at 09:19