hzeller / timg

A terminal image and video viewer.
GNU General Public License v2.0
1.91k stars 73 forks source link

Cannot read pdf in alpine container #126

Closed ErikXu closed 8 months ago

ErikXu commented 8 months ago

I am trying to read a pdf in alpine container. but I got a error message below:

execvp failed, errno = 2 (No such file or directory)
Magick: "gs" "-q" "-dBATCH" "-dSAFER" "-dMaxBitmap=50000000" "-dNOPAUSE" "-sDEVICE=ppmraw" "-dTextAlphaBits=4" "-dGraphicsAlphaBits=4" "-r72x72" "-sOutputFile=/tmp/gmLQKViM" "--" "/tmp/gmnCPGIr" "-c" "quit".

Dockerfile:

FROM alpine:3.19

RUN apk add build-base
RUN apk add timg

Build command:

docker build -t timg:latest -f ./Dockerfile .

Read pdf command:

docker run --rm -it \
-v ${PWD}/:/workspace/ \
timg:latest \
timg /workspace/test.pdf

Whole output:

[root@VM-0-15-centos timg]# docker run --rm -it -v ${PWD}/:/workspace/ timg:latest timg /workspace/test.pdf
execvp failed, errno = 2 (No such file or directory)
Magick: "gs" "-q" "-dBATCH" "-dSAFER" "-dMaxBitmap=50000000" "-dNOPAUSE" "-sDEVICE=ppmraw" "-dTextAlphaBits=4" "-dGraphicsAlphaBits=4" "-r72x72" "-sOutputFile=/tmp/gmLQKViM" "--" "/tmp/gmnCPGIr" "-c" "quit".
hzeller commented 8 months ago

Internally, the image processing invokes ghostscript to interpret PostScript and PDF (that is the gs call). So you probably need to add that to your container.

ErikXu commented 8 months ago

Cool, I really appreciate your advice. After adding ghostscript, The pdf can be read now. The final Dockerfile is

FROM alpine:3.19

RUN apk add build-base
RUN apk add ghostscript
RUN apk add timg

But the resolution seems unacceptable, it might due to the quality of pdf, right? Or any command parameter to improve?

timg -W --auto-crop /workspace/test.pdf

image

hzeller commented 8 months ago

Make sure you run timg in a terminal that can show high-resolution pictures (Maybe you need to tell it with -pi or -pk ).

Having said that, the rendering of PostScript and PDF is still not optimal as the graphicsmagick library I am using for that only requests a limited resolution when converting the file. I plan to see if I can get better results by directly using the ghostscript API, but haven't had time for that yet.

ErikXu commented 8 months ago

Cool. Thanks!

hzeller commented 8 months ago

I started adding a dedicated library to render PDFs (poppler)

You can compile it yourself if you have libpoppler-glib-dev installed and compile timg with -DWITH_POPPLER=On It allows to upscale with -U and -W and provides a higher resolution as the standard rendering.

(still work in progress, e.g. --auto-crop won't work yet).

hzeller commented 7 months ago

The new release now contains the improved PDF rendering https://github.com/hzeller/timg/releases/tag/v1.6.0

ErikXu commented 7 months ago

Really appreciate for your effort, but it seems not so much improvement in my side.

Dockerfile (base image changed and version v1.6.0 was installed):

FROM alpine:edge

RUN apk add build-base
RUN apk add ghostscript
RUN apk add timg

Result: image

I guess still the problem of the quality of pdf.

Is it possible to provide a good-looking pdf for testing?

Thanks!

hzeller commented 7 months ago

what is the terminal you're using ? Is it one of the ones that can output high-resolution images (such as Konsole, wezterm, kitty...) ?

It looks like the output of -pq ('quarter' blocks, see timg --help for explanations). That never can be good quality as the image is emitted as textblocks.

Can you run timg with --title="%wx%h %D" --verbose when looking at your example PDF so that we can see the original resolution and the image decoder it uses in the title (top of page), and other relevant information in the bottom of the output ?

ErikXu commented 7 months ago

I use MobaXterm as the terminal.

And the result of adding --title="%wx%h %D" --verbose is

 docker run --rm -it \
> -v ${PWD}/:/workspace/ \
> timg:latest \
> timg --title="%wx%h %D" --verbose -W --auto-crop /workspace/Resume.pdf
414x651 pdf

-------- pdf content --------

Terminal cells: 133x38  cell-pixels: -1x-2
Note: Terminal does not return ws_xpixel and ws_ypixel in TIOCGWINSZ ioctl or "\033[16t" query.
        ->Aspect ratio might be off.
        ->File a feature request with the terminal emulator program you use
Active Geometry: 131x36
Effective pixelation: Using quarter block.
Background color for transparency 'auto'; effective RGB #000000
1 file (1 successful); 275.6 KiB written (4312.7 KiB/s) 2 frames
Environment variables
 TIMG_PIXELATION           (not set)
 TIMG_DEFAULT_TITLE        (not set)
 TIMG_ALLOW_FRAME_SKIP     (not set)
 TIMG_USE_UPPER_BLOCK      (not set)
 TIMG_FONT_WIDTH_CORRECT   (not set)
hzeller commented 7 months ago

It only can do quarter block output there (see effective pixelation). Also looks like this terminal has other limitations such as not providing terminal pixel size information.

You can try giving -pi or -ps to the timg invocation to see if the terminal can do higher resolution.

I recommend using a modern terminal such as wezterm.