Search code examples
pdfimagemagickghostscript

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


Solution

  • 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