I'm making a Spring Boot application. I want to generate PDF from HTML code:
String htmlString = "<!DOCTYPE html>\n" +
"<html lang=\"ru\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\"/>\n" +
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"/>\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>\n" +
"</head>\n" +
"<body>\n" +
" <h3>ПРЕДСТАВЛЕНИЕ</h3>\n" +
"</body>\n" +
"</html>";
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String path = FileSystemView.getFileSystemView().getDefaultDirectory().getPath() + "/A.pdf";
OutputStream outputStream = new FileOutputStream(path);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(htmlString);
renderer.layout();
renderer.createPDF(outputStream);
byteArrayOutputStream.writeTo(outputStream);
As you can see there is a h3
tag with cyrillic symbols. The problem is that after conversion and saving the symbols are not presented in PDF (it's simply empty, because there is nothing more in html code to be visible). Other symbols are being displayed properly btw.
For html-to-pdf conversion i use:
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.0.1</version>
</dependency>
I suppose there is a problem with charset, fonts etc. How can I fix it?