通过设置自定义字体目录,加载字体
opts.setCustomFontFolders(new String[]{fileProperties.getCustomFontFolder()});
fileProperties.getCustomFontFolder() 是一个文件目录路径,这个目录里面有两个字体,一个字体是11M, 另一个是 27K.
下面是调用代码
public synchronized File doCadToPdf(InputStream inputStream, String outputFilePath) throws IOException {
File outputFile = new File(outputFilePath);
LoadOptions opts = new LoadOptions();
opts.setSpecifiedEncoding(CodePages.SimpChinese);
opts.setCustomFontFolders(new String[]{fileProperties.getCustomFontFolder()});
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
cadRasterizationOptions.setBackgroundColor(Color.getWhite());
cadRasterizationOptions.setPageWidth(fileProperties.getNewImageHeight());
cadRasterizationOptions.setPageHeight(fileProperties.getNewImageWeight() + 105F);
cadRasterizationOptions.setAutomaticLayoutsScaling(true);
cadRasterizationOptions.setNoScaling(false);
cadRasterizationOptions.setDrawType(1);
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
try (Image cadImage = Image.load(inputStream, opts);
OutputStream stream = new FileOutputStream(outputFile)) {
cadImage.save(stream, pdfOptions);
return outputFile;
}
}
案例来说,字体只需要加载一次缓存起来,后面就不用重复加载了,也不会导致内存溢出才对,是我哪里调用错了吗?
下面的图片,是内存溢出后dump文件的分析