Brainmaniac
Brainmaniac

Reputation: 2536

Specify default substitution font when converting pdf to image using imagemagick and font is missing

I am using Spatie/pdfToImage that builds on ghost script and imagemagick to on my server:

  1. Take a multiple page pdf from an email using mailgun routing.
  2. Save the pdf in folder /docs_pdf like file.pdf
  3. Use a foreach to loop through each page and save each page as a png to /docs like file_#.png

locally where I use laravel -> valet everything works fine.

On my server using digital ocean through laravel forge the language in a multipaged pdf that is in swedish transforms from normal swedish to a bunch of random letters and signs.

The left is correct (yes, its true. Its Swedish) and the right is wrong: enter image description here

Someone suggested to me that this is probably a matter of the font missing on the server. The fonts used in the pdf:

<</StemV 68/FontName/PSQHMO+FoundrySans-Normal/FontFile2 216 0 R/FontStretch/Normal/FontWeight 400/Flags 32/Descent -240/FontBBox[-40 -240 960 916]/Ascent 916/FontFamily(FoundrySans-Normal)/CapHeight 667/XHeight 465/Type/FontDescriptor/ItalicAngle 0>>
<</StemV 100/FontName/MLHPWU+FoundrySans-Medium/FontFile2 217 0 R/FontStretch/Normal/FontWeight 400/Flags 32/Descent -241/FontBBox[-42 -241 1008 916]/Ascent 916/FontFamily(FoundrySans-Medium)/CapHeight 667/XHeight 470/Type/FontDescriptor/ItalicAngle 0>>
<</StemV 68/FontName/SUEECI+FoundrySans-Normal/FontFile2 218 0 R/FontStretch/Normal/FontWeight 400/Flags 4/Descent -240/FontBBox[-40 -240 960 916]/Ascent 916/FontFamily(FoundrySans-Normal)/CapHeight 667/XHeight 465/Type/FontDescriptor/ItalicAngle 0>>
<</StemV 48/FontName/KIDDUY+FoundrySans-Light/FontFile2 9 0 R/FontStretch/Normal/FontWeight 400/Flags 32/Descent -248/FontBBox[-28 -248 978 924]/Ascent 924/FontFamily(FoundrySans-Light)/CapHeight 667/XHeight 458/Type/FontDescriptor/ItalicAngle 0>>

Here is configuration of fonts in imagemagick and ghostscript: https://www.imagemagick.org/script/resources.php

how can this be solved?

Update:

I have now made a clean install on a new server.

Installed Imagick and spatie/pdfToImage

As suggested by KenS I ran

gs -sDEVICE=png16m -o out%d.png

terminal output

forge@Server:~/app/storage/app/public/files$ gs -sDEVICE=png16m -o test_out%d.png file.pdf
GPL Ghostscript 9.22 (2017-10-04)
Copyright (C) 2017 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 2.
Page 1
Page 2

the document rendered the same = wrong.

I am at a complete loss.. Don't know what next step might be..

Update2:

I also run the convert imagemagick commando and the img rendered the same way also.

So even if I do it with ghostscript solo, imagemagick or spatie/pdfToImage it gives me the same output

Upvotes: 0

Views: 1521

Answers (2)

Brainmaniac
Brainmaniac

Reputation: 2536

Finally got it to work. I want to first give kudos to KenS that really helped me, and without him it would not have worked.

This is what I did:

1 - I removed Ghostscript:

sudo apt-get purge --auto-remove ghostscript

then

wget https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs925/ghostscript-9.25.tar.gz

tar xvf ghostscript-9.25.tar.gz

Enter the unpacked folder and do

./configure

make

make install

then

sudo ln -s /usr/local/bin/gs /usr/bin/gs

On top of the above I did:

sudo add-apt-repository ppa:glasen/freetype2

and then:

sudo apt update && sudo apt install freetype2-demos

Upvotes: 0

KenS
KenS

Reputation: 31141

Well, the current version of Ghostscript (9.25) renders this acceptably for me; that is the text appears to be correct. All the fonts are embedded, so there shouldn't be any problems.

And this means that even if you did replace the default font substitution, it wouldn't help, because Ghostscript shouldn't be using the default font, it will be using the fonts embedded in the PDF file.

Without knowing what version of Ghostscript you are using (I see from a later comment that its 9.25), or the command line that is used to start it, I can't really do a like-for-like comparison. Its hard for me to see how you could be getting such a different result though. That looks like Ghostscript has failed to find the embedded fonts.

Its possible that whatever package you are using has done something 'unfortunate'. The various package maintainers on Linux add their own patches, and sometimes modify the way that Ghostscript is built. Possibly that has broken something.

If you are able to build Ghostscript yourself you could try cloning our Git repository and doing that. You could also try downloading the Linux binaries off our website. They won't work with every Linux distribution (different ABI) but you can try, you might be lucky.

You could also try running Ghostscript directly on the PDF file. Something like:

gs -sDEVICE=png16m -o out%d.png

should produce 2 PNG files, out1.png and out2.png. It will also produce a bunch of stuff on the terminal. That back channel output is valuable information for me so if you can reproduce the problem, I'd like to see that too.

One last thought; its possible to have more than one version of Ghostscript installed simultaneously, its possible that your current setup is using an old version of Ghostscript.

I can't help you with ImageMagick or Spatie, but if you can debug those to the point where you can reproduce the problem with a plain Ghostscript command line then I can look further at it.

Upvotes: 1

Related Questions