I am discovering OpenHtmlToPdf for converting html into pdf. Well it is working with my custom fonts, but when I give multiple span tags for styling it fails. In this case it is not loading the proper fonts....
<p>invalid format</p>
<span style="font-family:'Courier Prime'">
<span style="color:yellow">
<span style="font-size:36pt">
Hello World!
</span>
</span> </span>
<p>working format</p>
<span style="font-family:'Courier
Prime';color:yellow;font-size:36pt">Hello World!</span>
enter image description here
Code: This is the relevant part of the spring-boot service ...
@Service
public class HtmltoPdfServiceImpl implements HtmlToPdfService {
@Autowired
private PdfRendererBuilder pdfRendererBuilder;
@Override
public byte[] topdf(HtmlToPdfModel htmlmap) throws HtmlToPdfException {
byte[] pdf;
String html = htmlmap.getHtmlbody();
try (ByteArrayOutputStream os = new ByteArrayOutputStream()){
pdfRendererBuilder.withHtmlContent(html, "");
pdfRendererBuilder.toStream(os);
pdfRendererBuilder.run();
pdf = os.toByteArray();
} catch (IOException e) {
throw new HtmlToPdfException("...");
}
return pdf;
}}
@Configuration
@Slf4j
public class PdfRendererBuilderConfig {
@Bean
public PdfRendererBuilder pdfRendereBuilderBean() throws IOException {
Integer x = 210;
Integer y = 297;
Path fontDirectory = Paths.get("/fonts");
PdfRendererBuilder pdfRendererBuilder = new PdfRendererBuilder();
pdfRendererBuilder.useDefaultPageSize(x, y, BaseRendererBuilder.PageSizeUnits.MM);
pdfRendererBuilder.useFastMode();
List<CSSFont> fonts = AutoFont.findFontsInDirectory(fontDirectory);
fonts.forEach(s->log.info(s.familyCssEscaped()+" loaded ..."));
AutoFont.toCSSEscapedFontFamily(fonts);
AutoFont.toBuilder(pdfRendererBuilder, fonts);
return pdfRendererBuilder;
}}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…