Home > Back-end >  Converting a PDF to PNG with Go (via Ghostscript)
Converting a PDF to PNG with Go (via Ghostscript)

Time:06-18

I want to convert a pdf file by calling Ghostscript via Go's exec.Command().

I am on Windows 10, Go Version 1.14.14 and Ghostscript 9.56.1.

The project structure looks like this:

- testapp.exe
- gs/
  |- gsdll64.dll
  |- gsdll64.lib
  |- gswin64c.exe
  |- in/
     |- test.pdf
  |- out/

This is the code:

func() {
    fmt.Println("Starting PDF Import...")

    cmdParams := []string{
        //"-q",
        //"-dQUIET",
        "-dSAFER",
        "-dBATCH",
        "-dNOPAUSE",
        "-dNOPROMPT",
        "-dMaxBitmap=500000000",
        "-dAlignToPixels=0",
        "-dGridFitTT=2",
        "-sDEVICE=png16m",
        "-dTextAlphaBits=4",
        "-dGraphicsAlphaBits=4",
        "-r150x150",

        "-o \".\\gs\\out\\test.png\"",
        ".\\gs\\in\\test.pdf",
    }

    cmd := exec.Command(".\\gs\\gswin64c.exe", cmdParams...)
    fmt.Println("Import command is: "   cmd.String())

    s, importErr := cmd.Output()
    result := string(s)
    fmt.Println(result)

    if importErr != nil {
        fmt.Println("Could not import PDF file, reason: "   importErr.Error())
    } else {
        fmt.Println("Import successful!")
    }

    fmt.Println("Import process finished!")
}

And the output I get looks like this:

Import command is: .\gs\gswin64c.exe -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r150x150 -o ".\gs\out\test.png" .\gs\in\test.pdf
GPL Ghostscript 9.56.1 (2022-04-04)
Copyright (C) 2022 Artifex Software, Inc.  All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Processing pages 1 through 84.
Page 1
   **** Error: Page drawing error occured.
               Could not draw this page at all, page will be missing in the output.
Page 2
   **** Error: Page drawing error occured.
               Could not draw this page at all, page will be missing in the output.

//...

Page 84
   **** Error: Page drawing error occured.
               Could not draw this page at all, page will be missing in the output.

Import successful!
Import process finished!

Obviously, something is wrong here.

The weird thing, though, is that when I copy and paste the command line from the output (i.e. .\gs\gswin64c.exe -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r150x150 -o ".\gs\out\test.png" .\gs\in\test.pdf) directly to a cmd or powershell window, the conversion works fine (though only one file will be created as there is no --%d (or similar) suffix on the output filename(s), yet).

I did also look at this question, and from the way the call to exec.Command() is created, I cannot really see a difference!?

Does anyone

  • have an idea what the issue is,
  • how to fix it
  • and why the behaviour is different when I paste the call directly to cmd or powershell?

CodePudding user response:

The problem is here:

        "-o \".\\gs\\out\\test.png\"",

This is passing two arguments as one. Instead:

    "-o",
    ".\\gs\\out\\test.png",
    ...
  • Related