Imagemagick: generate raw image data for PDF flate embedding?

后端 未结 1 833
暗喜
暗喜 2021-01-19 15:53

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?

相关标签:
1条回答
  • 2021-01-19 16:53

    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!

    0 讨论(0)
提交回复
热议问题