1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > html页面中中文转英文插件 iText 7 的htmlToPdf插件支持转换中文

html页面中中文转英文插件 iText 7 的htmlToPdf插件支持转换中文

时间:2019-08-29 03:47:29

相关推荐

html页面中中文转英文插件 iText 7 的htmlToPdf插件支持转换中文

用官网的示例, 转换出来的pdf中文都不见了,加上ConvertProperties参数后,添加系统字体,就解决了。记得之前解决iText中文问题,老费劲了,现在API使用起来方便多了,不由感叹,世间美好。

public class HtmlToPdfService {

private static final Logger log = org.slf4j.LoggerFactory.getLogger(HtmlToPdfService.class);

public static Path convert(String html) {

Path tempFile = null;

try {

tempFile = Files.createTempFile("html-to-pdf-", ".pdf");

final ConverterProperties converterProperties = new ConverterProperties();

final FontProvider fontProvider = new DefaultFontProvider();

final int ret = fontProvider.addSystemFonts();

log.debug("addSystemFonts ret:{}", ret);

converterProperties.setFontProvider(fontProvider);

HtmlConverter.convertToPdf(html, new FileOutputStream(tempFile.toFile()), converterProperties);

log.trace("Converted PDF = {}", tempFile.toString());

return tempFile;

} catch (IOException e) {

e.printStackTrace();

}

return tempFile;

}

}

附maven配置:

com.itextpdfitext7-core7.1.9pomcom.itextpdfhtml2pdf2.1.7

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。