โปรแกรมนี้ใช้ไลบรารี gofpdf เพื่อสร้างเอกสาร PDF ใหม่ เพิ่มหน้าเข้าไป และวาดภาพบนหน้านั้น จากนั้นจะบันทึกเอกสาร PDF ด้วยชื่อเดียวกับไฟล์รูปภาพ แต่มีนามสกุล .pdf

package main

import (
 "fmt"
 "image"
 "os"

 "github.com/jung-kurt/gofpdf"
)

func main() {
 // Open the image file
 file, _ := os.Open("image.jpg")
 defer file.Close()

 // Decode the image
 img, _, _ := image.Decode(file)

 // Create a new PDF document
 pdf := gofpdf.New("P", "mm", "A4", "")

 // Add a page to the document
 pdf.AddPage()

 // Draw the image on the page
 pdf.Image(img, 10, 10, 0, 0, false, "", 0, "")

 // Save the PDF document
 pdf.OutputFileAndClose("image.pdf")

 fmt.Println("Successfully converted image.jpg to image.pdf")
}

โปรดทราบว่าโค้ดตัวอย่างนี้ไม่ได้จัดการข้อผิดพลาด สำหรับโค้ดที่พร้อมใช้งานจริง คุณจะต้องจัดการกับข้อผิดพลาดด้วยวิธีที่เหมาะสม