Imagemagick: generate raw image data for PDF flate

2019-02-23 14:21发布

I'm trying to come up with a command-line, source code example of a PDF (see also How to generate plain-text source-code PDF examples that work in a document viewer?), but with embedding an image. This is how far I got - I have a template 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

Then, I generate the image data using convert, and I apply "flate" compression using zlib:

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

Then, I replace the /Length 1418 in the file, and finally replace the token ### using:

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

This file obviously has incorrect xref table, however, opens in evince just fine:

hello2.pdf-evince

... however, it's obvious that the bitmap is not in correct format.

I've tried to also generate, say,

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

... but those are not even generally green (as the original image should be).

 

Does anyone know the correct image format - and the convert command line - to generate a raw image that can be "flated" and included in a pdf?

Many thanks in advance for any answers,
Cheers!

1条回答
啃猪蹄的小仙女
2楼-- · 2019-02-23 14:30

Ok, fixed it; the problem was that one had to specify 8-bit depth in the convert command line; thus the correct invocation is:

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

Then we have:

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

Finally, the hello2.pdf opens in evince and displays the bitmap correctly:

hello2.pdf-evince-OK

 

Btw, I found this because I'm actually trying to debug an image in another document; so I basically did the following:

# 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

Hope this helps someone,
Cheers!

查看更多
登录 后发表回答