Converting a PDF to JPG (using ImageMagick)

· 421 words · 2 minutes read

In the example below we use the gographics/imagick package as a wrapper to the C library for ImageMagick to convert our PDF into a JPG. The processes goes as follows: We use the package to load in our test file which we then process by setting the resolution, compression levels and alpha channel settings then we save the final output file. Because the library is built on C, it’s important we call the Terminate and Destroy functions appropriately to keep our memory usage in check.

This is a useful process for creating thumbnails or converting the files to show in the web browser. It is limited to the first page of the PDF at the moment though.

Prerequisites for running under Ubuntu 18.04:

1
sudo apt install libmagic-dev libmagickwand-dev

The code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main

import (
    "log"
    "gopkg.in/gographics/imagick.v2/imagick"
)

func main() {

    pdfName := "ref.pdf"
    imageName := "test.jpg"

    if err := ConvertPdfToJpg(pdfName, imageName); err != nil {
        log.Fatal(err)
    }
}

// ConvertPdfToJpg will take a filename of a pdf file and convert the file into an 
// image which will be saved back to the same location. It will save the image as a 
// high resolution jpg file with minimal compression.
func ConvertPdfToJpg(pdfName string, imageName string) error {

    // Setup
    imagick.Initialize()
    defer imagick.Terminate()

    mw := imagick.NewMagickWand()
    defer mw.Destroy()

    // Must be *before* ReadImageFile
    // Make sure our image is high quality
    if err := mw.SetResolution(300, 300); err != nil {
        return err
    }

    // Load the image file into imagick
    if err := mw.ReadImage(pdfName); err != nil {
        return err
    }

    // Must be *after* ReadImageFile
    // Flatten image and remove alpha channel, to prevent alpha turning black in jpg
    if err := mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_FLATTEN); err != nil {
        return err
    }

    // Set any compression (100 = max quality)
    if err := mw.SetCompressionQuality(95); err != nil {
        return err
    }

    // Select only first page of pdf
    mw.SetIteratorIndex(0)

    // Convert into JPG
    if err := mw.SetFormat("jpg"); err != nil {
        return err
    }

    // Save File
    return mw.WriteImage(imageName)
}

If you see an error like below, take a look at this guide.

1
ERROR_POLICY: not authorized `TestPdf.pdf' @ error/constitute.c/ReadImage/412

Image of Author Edd Turtle

Author:  Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code. He loves coding, but also enjoys cycling and camping in his spare time.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below - Thanks for reading!