linux生成pdf中文字体完全不显示,然后添加了一个文件夹放字体simsunb.ttf,结果部分汉字丢失,索性把下图所有的中文字体加上,解决问题。

写法一:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>kernel</artifactId>
    <version>7.1.0</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>font-asian</artifactId>
    <version>7.1.0</version>
    <scope>test</scope>
</dependency>
public void HtmlToPdf(HttpServletRequest request, HttpServletResponse response,List<String> htmllist) throws Exception {
    ServletOutputStream outputStream=null;
    try {
        String outputFile = "../util/" + Tools.getTimeNo() + ".pdf";
        WriterProperties writerProperties = new WriterProperties();
        //Add metadata
        writerProperties.addXmpMetadata();
        ByteArrayOutputStream fos = new ByteArrayOutputStream();
        PdfWriter pdfWriter = new PdfWriter(fos, writerProperties);


        PdfDocument pdfDoc = new PdfDocument(pdfWriter);

        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(new AreaBreak(AreaBreakType.NEXT_PAGE));
        //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);
        PdfMerger merger = new PdfMerger(pdfDoc);

        //use foreach to create pdf and merge the pdf which converted yet
        for(int i=0;i<htmllist.size();i++){
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfDocument temp = new PdfDocument(new PdfWriter(baos));
            HtmlConverter.convertToPdf(htmllist.get(i), temp, props);
            temp = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())));
            merger.merge(temp, 1, temp.getNumberOfPages());
            temp.close();

        }
        pdfDoc.close();


        response.setContentType("application/pdf");
        response.setCharacterEncoding("UTF-8");
        outputStream = response.getOutputStream();
        outputStream.write(fos.toByteArray());

    } 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);

Logo

更多推荐