Я хочу преобразовать html в pdf с itext в моем java-коде, но кажется, что тег strong и тег em не работают. Я пытаюсь найти решения в сеть и отслеживание моего кода, чтобы выяснить, почему, но не удалось. Вот мой код Java, мой HTML-файл и сгенерированный снимок PDF ниже.
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;
}
}
и мои зависимости:
<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](https://i.stack.imgur.com/q7bFH.jpg)
решаемые
Я решил это с помощью itext7.