Image to PDF Conversion

Onkar
2 min readMay 11, 2021

Hi Guys, a few days back I was trying to convert an image document into PDF document for uploading on a professional website portal. As I browsed on web for a converter for the same, I observed that most of the Image to PDF conversion websites were blocked as per the security policy of my organisation.

During the process I also realized that, online image to pdf conversion leads to leakage of your personal data on web. Therefore, I thought that such conversion is possible through Golang programming language and open source module available in it securing your personal data.

Following Open Source modules were utilized to execute this process of conversion:

a) “github.com/nfnt/resize” ;and

b) “github.com/jung-kurt/gofpdf”

Below is the process to convert an Image document to PDF document -

I. To compress the Image use the below code -

package mainimport (
“fmt”
“os”
“image/jpeg”
“github.com/nfnt/resize”
)
func main() {f, err := os.Open(“image.jpg”)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()img, _ := jpeg.Decode(f)m := resize.Resize(1000, 0, img , resize.Lanczos3)out, _ := os.Create(“compre_image.jpg”)jpeg.Encode(out, m, nil)}

Decoding and Encoding of image is done with the help of “image/jpeg” package. This is a default package available with Golang.

Resize package includes “resize method” that converts image using interpolation function.

Various interpolation functions are available as listed below -

  1. NearestNeighbor: Nearest-neighbor interpolation
    2. Bilinear: Bilinear interpolation
    3. Bicubic: Bicubic interpolation
    4. MitchellNetravali: Mitchell-Netravali interpolation
    5. Lanczos2: Lanczos resampling with a=2
    6. Lanczos3: Lanczos resampling with a=3

You can know more about it at https://github.com/nfnt/resize

II. Once, the JPG image is compressed, convert it, in PDF format with below code-

import (
“github.com/jung-kurt/gofpdf”
)
func main() {
args := os.Args[1:]
if len(args) != 2 {
fmt.Println(str)
return
}
err := genratePdf(“hello.pdf”, args)
if err != nil {
panic(err)
}
}func genratePdf(filename string, args []string) error {

pdf := gofpdf.New(“P”, “mm”, “A3”, “”)
pdf.AddPage()
pdf.SetFont(“Arial”, “B”, 16)
// CellFormat(width, height, text, border, position after, align, fill, link, linkStr)
pdf.CellFormat(190, 7, args[1], “0”, 0, “CM”, false, 0, “”)
// ImageOptions(src, x, y, width, height, flow, options, link, linkStr)
pdf.ImageOptions(
args[0],
50, 20,
150, 200,
false,
gofpdf.ImageOptions{ImageType: “JPG”, ReadDpi: true},
0,
“”,
)
return pdf.OutputFileAndClose(filename)
}

As a result, gofpdf.New will generate PDF as per specified attributes. Here, in ImageOptions “x” , “y”, “width” and “height” are important parameters. Based on these values, your image will get adjusted in the PDF file.

You may calculate the image property on following URL.

“ https://uproer.com/articles/image-size-calculator-px-in/”

You may refer the entire code at “https://github.com/omankame/image-pdf/tree/master”

I am very grateful to Mr. Jan Schlicht, Mr. Kurt Jung and all other Open Source contributors who contributed to develop the above modules.

--

--

Onkar

RHEL Linux , K8s and Docker Administrator, Go lang Programmer, AWS, Terraform and open source enthusiastic