3

I am currently rewriting my "old" Python-based Chatbots into golang. One issue I came across was that I can´t convert a PDF into jpeg. Which I´d like to do so I can send it into a chat easily.

Already tried this with bimg (did not work at all) and imagemagick (gopkg.in/gographics/imagick.v3/imagick) as provided in this answer to another thread: "https://stackoverflow.com/a/47520596/7502507"

imagick.Initialize()

defer imagick.Terminate()

mw := imagick.NewMagickWand()

defer mw.Destroy()

mw.ReadImage(pdf)

mw.SetIteratorIndex(0) // This being the page offset

mw.SetImageFormat("jpg")

mw.WriteImage(image)

It does not produce a jpg at all, it just gives me the error

ERROR_POLICY: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408

How can I make this work? I can´t seem to edit the security settings for this.

Any help is appreciated!

Jan Stein
  • 31
  • 4
  • Perhaps it could be something solved with elevated privileges? – AMDarwech May 06 '19 at 20:03
  • Apparently it is due toa bug in Ghostscript, you have to modify you `policy.xml` or update the Ghostscript package (a fix has been published) : https://stackoverflow.com/questions/52998331/imagemagick-security-policy-pdf-blocking-conversion – Ugo T. May 06 '19 at 20:05

1 Answers1

2

I've been through the same issue and din't find any pure Golang solution.

Not a pure Golang solution, but this works flawlessly which uses cgo internally. Underlying library is Pdfium which is a C++ library, developed by Google and used in Chrome.

After a lot of research, I discovered that Pdfium was the best in class in terms of speed, robustness, license requirements and reliability.

To improve overall performance of the operation, I've ended up with this encoder as Golang's image encoders aren't geared towards performance and are slow.

Viggi
  • 109
  • 1
  • 6