pdfbox Could not load font file: C:\WINDOWS\FONTS\mstmc.ttf java.io.EOFException

本文介绍了如何解决PDFBox在Windows环境下加载mstmc.ttf字体时遇到的EOFException问题。通过修改源码,创建新的FileSystemFontProvider类来避免加载该字体文件。
摘要由CSDN通过智能技术生成

解决思路就是修改源码,过滤掉mstmc.ttf文件读取,具体操作如下:

1、在自己的项目中新建包结构为:org.apache.pdfbox.pdmodel.font

2、新建FileSystemFontProvider.java文件,将下方代码复制进去即可

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pdfbox.pdmodel.font;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fontbox.FontBoxFont;
import org.apache.fontbox.cff.CFFCIDFont;
import org.apache.fontbox.cff.CFFFont;
import org.apache.fontbox.ttf.NamingTable;
import org.apache.fontbox.ttf.OS2WindowsMetricsTable;
import org.apache.fontbox.ttf.OTFParser;
import org.apache.fontbox.ttf.OpenTypeFont;
import org.apache.fontbox.ttf.TTFParser;
import org.apache.fontbox.ttf.TrueTypeCollection;
import org.apache.fontbox.ttf.TrueTypeCollection.TrueTypeFontProcessor;
import org.apache.fontbox.ttf.TrueTypeFont;
import org.apache.fontbox.type1.Type1Font;
import org.apache.fontbox.util.autodetect.FontFileFinder;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.util.Charsets;

/**
 * A FontProvider which searches for fonts on the local filesystem.
 *
 * @author John Hewson
 */
final class FileSystemFontProvider extends FontProvider {
	private static final Log LOG = LogFactory.getLog(FileSystemFontProvider.class);

	private final List<FSFontInfo> fontInfoList = new ArrayList<FSFontInfo>();
	private final FontCache cache;

	private static class FSFontInfo extends FontInfo {
		private final String postScriptName;
		private final FontFormat format;
		private final CIDSystemInfo cidSystemInfo;
		private final int usWeightClass;
		private final int sFamilyClass;
		private final int ulCodePageRange1;
		private final int ulCodePageRange2;
		private final int macStyle;
		private final PDPanoseClassification panose;
		private final File file;
		private final FileSystemFontProvider parent;

		private FSFontInfo(File file, FontFormat format, String postScriptName, CIDSystemInfo cidSystemInfo,
				int usWeightClass, int sFamilyClass, int ulCodePageRange1, int ulCodePageRange2, int macStyle,
				byte[] panose, FileSystemFontProvider parent) {
			this.file = file;
			this.format = format;
			this.postScriptName = postScriptName;
			this.cidSystemInfo = cidSystemInfo;
			this.usWeightClass = usWeightClass;
			this.sFamilyClass = sFamilyClass;
			this.ulCodePageRange1 = ulCodePageRange1;
			this.ulCodePageRange2 = ulCodePageRange2;
			this.macStyle = macStyle;
			this.panose = panose != null && panose.length >= PDPanoseClassification.LENGTH
					? new PDPanoseClassification(panose)
					: null;
			this.parent = parent;
		}

		@Override
		public String getPostScriptName() {
			return postScriptName;
		}

		@Override
		public FontFormat getFormat() {
			return format;
		}

		@Override
		public CIDSystemInfo getCIDSystemInfo() {
			return cidSystemInfo;
		}

		/**
		 * {@inheritDoc}
		 * <p>
		 * The method returns null if there is there was an error opening the font.
		 * 
		 */
		@Override
		public synchronized FontBoxFont getFont() {
			// synchronized to avoid race condition on cache access,
			// which could result in an unreferenced but open font
			FontBoxFont cached = parent.cache.getFont(this);
			if (cached != null) {
				return cached;
			} else {
				FontBoxFont font;
				switch (format) {
				case PFB:
					font = getType1Font(postScriptName, file);
					break;
				case TTF:
					font = getTrueTypeFont(postScriptName, fil
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值