linux生成pdf中文字体完全不显示,然后添加了一个文件夹放字体simsunb.ttf,结果部分汉字丢失,索性把下图所有的中文字体加上,解决问题。
写法一:
<dependency> <groupId>com.itextpdf</groupId> <artifactId>html2pdf</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.1</version> </dependency> <dependency> <groupId>com.itextpdf.tool</groupId> <artifactId>xmlworker</artifactId> <version>5.5.1</version> </dependency> <!-- 支持中文 --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <!-- 支持css样式渲染 --> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf-itext5</artifactId> <version>9.0.3</version> </dependency>
WriterProperties writerProperties = new WriterProperties(); //Add metadata writerProperties.addXmpMetadata(); ByteArrayOutputStream fos = new ByteArrayOutputStream(); PdfWriter pdfWriter = new PdfWriter(fos, writerProperties); PdfDocument pdfDoc = new PdfDocument(pdfWriter); //PageSize pageSize=new PageSize(842, 595); pdfDoc.addNewPage(); pdfDoc.getCatalog().setLang(new PdfString("UTF-8")); //Set the document to be tagged pdfDoc.setTagged(); pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true)); Document document=new Document(pdfDoc); //document.add(image); ConverterProperties props = new ConverterProperties(); // Font font = new Font(BaseFont.createFont(fontUrl+",1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)); FontProvider fp = new FontProvider(); fp.addStandardPdfFonts(); //fp.addDirectory(outputFile); fp.addDirectory(fontFileUrl);//The noto-nashk font file (.ttf extension) is placed in the resources fp.addSystemFonts(); props.setFontProvider(fp); props.setBaseUri(outputFile); try { HtmlConverter.convertToPdf(new ByteArrayInputStream(HTML.getBytes("UTF-8")), pdfDoc, props); } catch (Exception e) { throw new WebRuntimeException("pdf处理失败:" + e.getMessage()); } byte[] bytes = fos.toByteArray(); pdfDoc.close(); // ByteArrayBody contentBody = new ByteArrayBody(bytes, "temp" + System.currentTimeMillis() + ".pdf"); response.setContentType("application/pdf"); response.setCharacterEncoding("UTF-8"); outputStream = response.getOutputStream(); outputStream.write(bytes); } catch (Exception e) { e.printStackTrace(); }finally { try { if(outputStream!=null){ outputStream.flush(); outputStream.close(); } }catch(IOException ex){ ex.printStackTrace(); } }
写法二;
if (fontname == null) { // 操作系统需要有该字体, 没有则需要安装; 当然也可以将字体放到项目中, 再从项目中读取 fontname = "SimSun"; } String fontPrefix = ""; String os = System.getProperties().getProperty("os.name"); try {//取本地工程内 if (os.startsWith("win") || os.startsWith("Win")) { java.net.URL url = this.getClass().getResource("/fonts/SIMSUN.TTC"); if (url != null) { fontPrefix = url.getPath(); } else { return super.getFont(fontname, encoding, size, style); } } else { logger.info("配置路径为:" + linuxFontUrl); //特定路径 // fontPrefix = (Tools.isNull(linuxFontUrl)?"/usr/share/fonts/SIMSUN.TTC":linuxFontUrl); fontPrefix = linuxFontUrl; } logger.info("路径为:" + fontPrefix); BaseFont baseFont; Font font; baseFont = BaseFont.createFont(fontPrefix + ",1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); font = new Font(baseFont); font.setSize(size); font.setStyle(style); return font; } catch (DocumentException e) { logger.info("pdf生成发生错误:" + e.getMessage()); e.printStackTrace(); } catch (IOException e) { logger.info("pdf生成发生错误2:" + e.getMessage()); e.printStackTrace(); } return super.getFont(fontname, encoding, size, style);