关于 linux:Imagemagick: 为 PDF flate 嵌入生成原始图像数据?

Imagemagick: generate raw image data for PDF flate embedding?

我正在尝试提出一个命令行、PDF 源代码示例(另请参阅如何生成在文档查看器中工作的纯文本源代码 PDF 示例?),但嵌入了一个图片。这就是我得到的程度 - 我有一个模板 hello.pdf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
%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" 压缩:

1
2
3
4
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,最后使用:

替换标记###

1
perl -ne 's/^###/`cat test.flate`/e;print' hello.pdf > hello2.pdf

这个文件显然有不正确的外部参照表,但是,在 evince 中打开就好了:

hello2.pdf-evince

... 但是,很明显位图格式不正确。

我也尝试过生成,比如说,

1
convert -size 150x150 gradient:\\#4b4-\\#bfb -endian LSB rgb:test.raw

...但那些甚至不是一般的绿色(原始图像应该是)。

有谁知道正确的图像格式 - 以及 convert 命令行 - 以生成可以"扁平化"并包含在 pdf 中的原始图像?

非常感谢您提供任何答案,
干杯!


好的,修好了;问题是必须在 convert 命令行中指定 8 位深度;因此正确的调用是:

1
convert -depth 8 -size 150x150 gradient:\\#4b4-\\#bfb rgb:test.raw

然后我们有:

1
2
3
4
5
6
7
du -b test.raw # 67500 bytes
python -c"import zlib,sys;sys.stdout.write(zlib.compress(sys.stdin.read()))" < test.raw > test.flate
du -b test.flate # 664 bytes

# replace /Length 664, and then:

perl -ne 's/^###/`cat test.flate`/e;print' hello.pdf > hello2.pdf

最后,hello2.pdfevince 中打开并正确显示位图:

hello2.pdf-evince-OK

顺便说一句,我发现这个是因为我实际上是在尝试调试另一个文档中的图像;所以我基本上做了以下事情:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# extract and save the stream of this image object
qpdf --show-object=23 --raw-stream-data mybadfile.pdf > myraw.file

# get raw binary data - deflate the saved object stream
python -c"import zlib,sys;sys.stdout.write(zlib.decompress(sys.stdin.read()))" < myraw.file > myraw.deflate

identify myraw.deflate
# identify: no decode delegate for this image format `myraw.deflate' @ constitute.c/ReadImage/530.

identify rgb:myraw.deflate
# identify: Must specify image size `myraw.deflate' @ rgb.c/ReadRGBImage/155.

identify -size 588x508 rgb:myraw.deflate
# rgb:myraw.deflate=>myraw.deflate RGB 588x508 588x508+0+0 16-bit TrueColor DirectClass 875KiB 0.020u 0:00.030
# identify: Unexpected end-of-file `myraw.deflate': No such file or directory @ rgb.c/ReadRGBImage/261.

display -size 588x508 rgb:myraw.deflate
# display: Unexpected end-of-file `myraw.deflate': No such file or directory @ rgb.c/ReadRGBImage/261. ### but it shows correctly, except for size?

identify -depth 8 -size 588x508 rgb:myraw.deflate
# rgb:myraw.deflate=>myraw.deflate RGB 588x508 588x508+0+0 8-bit TrueColor DirectClass 875KiB 0.020u 0:00  ## OK

display -depth 8 -size 588x508 rgb:myraw.deflate
# OK; choosing rgba: is already bad - so confirmed 8-bit rgb

希望这对某人有所帮助,
干杯!