Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

pdf generation - Issue writing a PDF file from scratch

I'm trying to generate a simple PDF file (text only) programmatically. With most PDF reader the file looks exactly like I want but some PDF readers won't open the file and report it as damaged. I've removed every elements from the file leaving only the first void page to debug the code and found the problem but without any results, it's still recognized as damaged.

This is the raw PDF data:

%PDF-1.4
1 0 obj
<< /Type /Catalog
   /Pages 2 0 R
>>
endobj
2 0 obj
<< /Type /Pages
   /Count 1
   /Kids [ 3 0 R ]
>>
endobj
3 0 obj
<< /Type /Page
   /Parent 2 0 R
   /Resources <<
   >>
   /MediaBox [ 0 0 595 842 ]
>>
endobj
xref
0 4
0000000000 65536 f 
0000000010 00000 n 
0000000067 00000 n 
0000000138 00000 n 
trailer
<< /Size 4
   /Root 1 0 R
>>
startxref
248
%%EOF

Can anyone tell me what's wrong with this PDF?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Per spec /Resources is required although it can be empty. Adding /Resources <<>> to object 2 or 3 should suffice.

EDIT

The PDF below with the updated /Resources entry passes Acrobat's preflight when I run it. Do you get a preflight error message?

%PDF-1.4
1 0 obj
<< /Type /Catalog
   /Pages 2 0 R
>>
endobj
2 0 obj
<< /Type /Pages
   /Count 1
   /Kids [ 3 0 R ]
>>
endobj
3 0 obj
<< /Type /Page
   /Parent 2 0 R
   /MediaBox [ 0 0 595 842 ]
   /Resources <<>>
>>
endobj
xref
0 4
0000000000 65536 f 
0000000010 00000 n 
0000000067 00000 n 
0000000138 00000 n 
trailer
<< /Size 4
   /Root 1 0 R
>>
startxref
243
%%EOF

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...