ImageMagick, PDFs, and third-party fonts

If you use ImageMagick to convert PDFs, you’ll know it’s as simple as

convert file.pdf file.jpg

However, if you’re using a third-party non-GhostScript-sanctioned font, this won’t work terribly well and fail with a rather cryptic GhostScript error such as:

ERROR: /invalidfileaccess in –file–
Operand stack:
–dict:5/5(L)– F2 10.0 –dict:6/6(L)– –dict:6/6(L)– STSongStd-Light-Acro-UniGB-UCS2-H –dict:10/12(ro)(G)– –nostringval– –dict:7/7(L)– –dict:7/7(L)– Adobe-GB1 CIDFont Adobe-GB1 Adobe-GB1 –nostringval– (/usr/share/fonts/chinese/TrueType/uming.ttf) (r)
Execution stack:
%interp_exit .runexec2 –nostringval– –nostringval– –nostringval– 2 %stopped_push –nostringval– –nostringval– –nostringval– false 1 %stopped_push 1 3 %oparray_pop 1 3 %oparray_pop 1 3 %oparray_pop –nostringval– –nostringval– 2 1 6 –nostringval– %for_pos_int_continue –nostringval– –nostringval– –nostringval– –nostringval– 1 %stopped_push –nostringval– –nostringval– –nostringval– %array_continue –nostringval– false 1 %stopped_push –nostringval– %loop_continue –nostringval– –nostringval– –nostringval– –nostringval– –nostringval– %array_continue –nostringval– –nostringval– –nostringval– –nostringval– %loop_continue –nostringval– 12 9 %oparray_pop –nostringval– –nostringval– –nostringval– –nostringval– –nostringval–
Dictionary stack:
–dict:1127/1686(ro)(G)– –dict:0/20(G)– –dict:107/200(L)– –dict:107/200(L)– –dict:104/127(ro)(G)– –dict:241/347(ro)(G)– –dict:20/24(L)– –dict:4/6(L)– –dict:24/31(L)– –dict:38/50(ro)(G)–
Current allocation mode is local
Last OS error: 2
ESP Ghostscript 815.02: Unrecoverable error, exit code 1
convert: Postscript delegate failed `/home/kale/poop.pdf’.
convert: missing an image filename `/home/kale/poop.pnm’.

In this instance, the PDF poop.pdf contains a Chinese font which Ghostscript knows about but won’t let me use since I installed it after-the-fact from RPM (chinese-fonts.noarch). GhostScript by default is run from ImageMagick with the -dSAFER flag which is a mildly paranoid flag that prevents GS from using files outside of its root (/usr/share/ghostscript, usually). To work around this error, rather than dealing with GS’s confusing and arcane font directory configuration, I recommend simply amending ImageMagick’s delegates definition for the PDF filetype:

/usr/lib64/ImageMagick-6.2.8/config/delegates.xml:

<delegate decode=”pdf” encode=”ps” mode=”bi” command='”gs” -q -dBATCH -dSAFER -dMaxBitmap=500000000 -dNOPAUSE -dAlignToPixels=0 -dEPSCrop -sDEVICE=”pswrite” -sOutputFile=”%o” -f”%i”‘ />

to

<delegate decode=”pdf” encode=”ps” mode=”bi” command='”gs” -q -dBATCH -dMaxBitmap=500000000 -dNOPAUSE -dAlignToPixels=0 -dEPSCrop -sDEVICE=”pswrite” -sOutputFile=”%o” -f”%i”‘ />

(remove the -dSAFER flag)

Henceforth, `convert poop.pdf poop.jpg’ will work with third-party fonts without issue.

One Comments

  • Tiago Fonseca

    January 25, 2011

    Just wanted to thank you for this post.
    With my ImageMagick it was using -dPARANOIDSAFER but the fix was the same, just remove it.

Leave a Reply