在 linux 上裁剪 pdf 文件时无法正确呈现 Courier 粗体字体
Courier bold font not correctly rendered when cropping a pdf file on linux
我正在使用 pdfbox 2.0.21 从 pdf 中提取页面作为图像并将它们添加到 word 文档中。当我在 windows 环境中生成文件时,文档字体呈现正确,而当在测试环境(centos 7)中 运行 时,它们呈现为下图
我怀疑 pddocument 没有 courier 粗体字体,所以它在渲染图像时默认为某些本机设置。我该如何解决这个问题?我没有看到任何强制或指定字体集的方法。
提取图像的代码如下
PDDocument document = null;
try {
document = PDDocument.load(sourceOfImages);
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); ++page)
{
logger.debug( " page " + page + "of " + document.getNumberOfPages());
ReportImage reportImage = new ReportImage();
try (ByteArrayOutputStream pageImagesOutputStream = new ByteArrayOutputStream())
{
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, DPI, ImageType.RGB);
logger.debug( " start clear metadata area ");
clearImageArea(bim,whiteAreaMetadataX,whiteAreaMetadataY,whiteAreaMetadataW,whiteAreaMetadataH);
logger.debug( " end clear metadata area ");
logger.debug( " creating png ");
final int[] colourMap = { 0x00000000, 0xffffffff, 0xff000000, 0xff353535, 0xff888888, 0xff969696, 0xff237fe9, 0xffff0000 };
IndexColorModel colorModel = new IndexColorModel(8, colourMap.length, colourMap, 0, true, 0, DataBuffer.TYPE_BYTE );
BufferedImage image = new BufferedImage(ReportHelper.getA4W(DPI), ReportHelper.getA4H(DPI), BufferedImage.TYPE_3BYTE_BGR);
image.getGraphics().drawImage(bim, 0, 0, null);
ImageIO.write(image, "PNG", pageImagesOutputStream);
double cmWidth = 21;
double cmHeight = 29.7;
pageImagesOutputStream.flush();
logger.debug( "end creating png ");
reportImage.setImage(pageImagesOutputStream.toByteArray());
reportImage.setWidth(cmWidth);
reportImage.setHeight(cmHeight);
reportImage.getMetadata().getPDFInfoDTO().setFileName(fileName);
reportImage.getMetadata().setPageNumber(page);
images.add(reportImage);
}
catch(Exception ex)
{
logger.error("failed reading images at page" + page, ex);
throw new IOException(ex);
}
logger.debug( " end page " + page);
}
logger.debug( " end file elaboration " + fileName + " size " + images.size());
return images;
}
catch(Exception ex)
{
logger.error("failed reading document", ex);
throw new IOException(ex);
}
已解决在 docker 图像上安装 mscorefonts
我正在使用 pdfbox 2.0.21 从 pdf 中提取页面作为图像并将它们添加到 word 文档中。当我在 windows 环境中生成文件时,文档字体呈现正确,而当在测试环境(centos 7)中 运行 时,它们呈现为下图
我怀疑 pddocument 没有 courier 粗体字体,所以它在渲染图像时默认为某些本机设置。我该如何解决这个问题?我没有看到任何强制或指定字体集的方法。 提取图像的代码如下
PDDocument document = null;
try {
document = PDDocument.load(sourceOfImages);
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); ++page)
{
logger.debug( " page " + page + "of " + document.getNumberOfPages());
ReportImage reportImage = new ReportImage();
try (ByteArrayOutputStream pageImagesOutputStream = new ByteArrayOutputStream())
{
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, DPI, ImageType.RGB);
logger.debug( " start clear metadata area ");
clearImageArea(bim,whiteAreaMetadataX,whiteAreaMetadataY,whiteAreaMetadataW,whiteAreaMetadataH);
logger.debug( " end clear metadata area ");
logger.debug( " creating png ");
final int[] colourMap = { 0x00000000, 0xffffffff, 0xff000000, 0xff353535, 0xff888888, 0xff969696, 0xff237fe9, 0xffff0000 };
IndexColorModel colorModel = new IndexColorModel(8, colourMap.length, colourMap, 0, true, 0, DataBuffer.TYPE_BYTE );
BufferedImage image = new BufferedImage(ReportHelper.getA4W(DPI), ReportHelper.getA4H(DPI), BufferedImage.TYPE_3BYTE_BGR);
image.getGraphics().drawImage(bim, 0, 0, null);
ImageIO.write(image, "PNG", pageImagesOutputStream);
double cmWidth = 21;
double cmHeight = 29.7;
pageImagesOutputStream.flush();
logger.debug( "end creating png ");
reportImage.setImage(pageImagesOutputStream.toByteArray());
reportImage.setWidth(cmWidth);
reportImage.setHeight(cmHeight);
reportImage.getMetadata().getPDFInfoDTO().setFileName(fileName);
reportImage.getMetadata().setPageNumber(page);
images.add(reportImage);
}
catch(Exception ex)
{
logger.error("failed reading images at page" + page, ex);
throw new IOException(ex);
}
logger.debug( " end page " + page);
}
logger.debug( " end file elaboration " + fileName + " size " + images.size());
return images;
}
catch(Exception ex)
{
logger.error("failed reading document", ex);
throw new IOException(ex);
}
已解决在 docker 图像上安装 mscorefonts