PDF 框 1.8.10。从 load() 方法生成 PDDocument 时出错
PDFBOX 1.8.10. Error in generating PDDocument from load() method
我正在使用 PDFBOX 1.8.10。
如果我将 PDF 文件加载到字节数组中,它会工作 -
File file = new File(args[0]);
FileInputStream fis = new FileInputStream(file); //Normal PDF File
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
}
} catch (IOException ex) {
ex.printStackTrace();
}
byte[] bytes = bos.toByteArray();
CheckIsPDF(bytes);
pdf = PDDocument.load(new ByteArrayInputStream(bytes)); //**No exception here**
但是如果相同的文件存储在数据库中并且如果我尝试通过上面的代码读取它,我会得到以下异常 -
"java.io.IOException: Error: End-of-File, expected line"
.
这是从数据库读取并填充 PDF 的代码-
List<byte[]> forms; //this gets populated from database. The data stored in DB is HEX.
for(byte[] file : forms){
try{
int var=file.length;
pdDocument = PDDocument.load(new ByteArrayInputStream(file)); //**Exception**
fieldLists = PDFFormUtils.printFields( pdDocument );
}
catch(Exception e){
e.printStackTrace();
}
}
如评论中所述,问题的原因是 blob 的内容不是 PDF。 blob 内容是:
43 3a 5c 4d 42 43 50 4f 53 5c 52 65 6e 74 2e 70 64 66
pdf 以“%PDF”开头,因此在十六进制中会是
25 50 44 46
您提到的十六进制序列转换为
C:\MBCPOS\Rent.pdf
这意味着有人将文件名而不是文件内容保存到 blob 中。
我正在使用 PDFBOX 1.8.10。
如果我将 PDF 文件加载到字节数组中,它会工作 -
File file = new File(args[0]);
FileInputStream fis = new FileInputStream(file); //Normal PDF File
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
}
} catch (IOException ex) {
ex.printStackTrace();
}
byte[] bytes = bos.toByteArray();
CheckIsPDF(bytes);
pdf = PDDocument.load(new ByteArrayInputStream(bytes)); //**No exception here**
但是如果相同的文件存储在数据库中并且如果我尝试通过上面的代码读取它,我会得到以下异常 -
"java.io.IOException: Error: End-of-File, expected line"
.
这是从数据库读取并填充 PDF 的代码-
List<byte[]> forms; //this gets populated from database. The data stored in DB is HEX.
for(byte[] file : forms){
try{
int var=file.length;
pdDocument = PDDocument.load(new ByteArrayInputStream(file)); //**Exception**
fieldLists = PDFFormUtils.printFields( pdDocument );
}
catch(Exception e){
e.printStackTrace();
}
}
如评论中所述,问题的原因是 blob 的内容不是 PDF。 blob 内容是:
43 3a 5c 4d 42 43 50 4f 53 5c 52 65 6e 74 2e 70 64 66
pdf 以“%PDF”开头,因此在十六进制中会是
25 50 44 46
您提到的十六进制序列转换为
C:\MBCPOS\Rent.pdf
这意味着有人将文件名而不是文件内容保存到 blob 中。