我正在尝试编写一个命令行源代码示例,用于生成包含图像的PDF文件(参见 如何生成在文档查看器中有效的纯文本源代码PDF示例?)。我已经有了一个名为hello.pdf
的模板,但是还需要嵌入图像。
%PDF-1.4
1 0 obj
<< /Type /Catalog
/Outlines 2 0 R
/Pages 3 0 R
>>
endobj
2 0 obj
<< /Type /Outlines
/Count 0
>>
endobj
3 0 obj
<< /Type /Pages
/Kids [ 4 0 R ]
/Count 1
>>
endobj
4 0 obj
<< /Type /Page
/Parent 3 0 R
/MediaBox [ 0 0 612 792 ]
/Contents 5 0 R
/Resources << /ProcSet 6 0 R
/Font << /F1 7 0 R >>
/ProcSet [ /PDF /Text /ImageC ] /XObject << /Im1 8 0 R >>
>>
>>
endobj
5 0 obj
<< /Length 173 >>
stream
BT
/F1 24 Tf
100 100 Td
( Hello World ) Tj
200 200 Td
( Hello Again ) Tj
% width skew-right-up skew-top-right height x y
150 0 0 150 340 130 cm
% 150 150 Td - irrelevant for image
/Im1 Do
ET
endstream
endobj
6 0 obj
[ /PDF /Text ]
endobj
7 0 obj
<< /Type /Font
/Subtype /Type1
/Name /F1
/BaseFont /Helvetica
/Encoding /MacRomanEncoding
>>
endobj
8 0 obj
<<
/BitsPerComponent 8 /ColorSpace /DeviceRGB /DecodeParms
<<
/BitsPerComponent 8 /Colors 3 /Columns 150 /Predictor 10
>>
/Filter [ /FlateDecode ] /Height 150 /Length 1418 /Subtype /Image /Type /XObject /Width 150
>>
stream
###endstream
endobj
9 0 obj
[ /PDF ]
endobj
xref
0 10
0000000000 65535 f
0000000009 00000 n
0000000074 00000 n
0000000120 00000 n
0000000179 00000 n
0000000364 00000 n
0000000466 00000 n
0000000496 00000 n
0000001000 00000 n
0000001100 00000 n
trailer
<< /Size 10
/Root 1 0 R
>>
startxref
625
%%EOF
然后,我使用convert
生成图像数据,并使用zlib
应用"flate"压缩:
convert -size 150x150 gradient:\#4b4-\#bfb test.ppm
du -b test.ppm # 135017 bytes
python -c "import zlib,sys;sys.stdout.write(zlib.compress(sys.stdin.read()))" < test.ppm > test.flate
du -b test.flate # 1418 bytes
然后,我替换文件中的/Length 1418
,最后使用以下方式替换令牌###
:
perl -ne 's/^###/`cat test.flate`/e;print' hello.pdf > hello2.pdf
这个文件的交叉引用表明显不正确,但在
evince
中可以正常打开:
我已经尝试生成:
convert -size 150x150 gradient:\#4b4-\#bfb -endian LSB rgb:test.raw
...但它们甚至不是普遍的绿色(原始图像应该是这样的)。
有人知道正确的图像格式和convert
命令行吗,以生成可以“膨胀”并包含在pdf中的原始图像?
非常感谢您提前的任何答案,
干杯!