Convert png to pdf in Golang
Senior Software Engineer from India. Loves to build software that bring better experiences to people.
Search for a command to run...
Senior Software Engineer from India. Loves to build software that bring better experiences to people.
This series provides practical articles and blog posts on everything related to Go Programming language and Software Development in Go.
Database connection pooling is a technique used to improve the performance of database-driven applications by reusing a set of pre-opened database connections. In a typical database-driven application, the application opens a database connection, exe...
In the exhilarating realm of software development, one fundamental truth prevails – change is the only constant. As technology hurtles forward, the evolution of design patterns and principles has become the compass guiding developers through the dyna...

Data compression is the process of reducing the amount of data required to represent a given piece of information. This is typically done by removing redundant or unnecessary information from the data, or by using algorithms and techniques that encod...

XML parsing refers to the process of analyzing and extracting data from an XML (Extensible Markup Language) document. XML is a popular format for storing and sharing structured data, such as that used in websites or applications. To parse an XML docu...
Database connection pooling is a technique used to improve the performance of database-driven applications by reusing a set of pre-opened database connections. In a typical database-driven application, the application opens a database connection, exe...
Interoperability between different programming languages is a common requirement in many software projects. In this article, we will explore how to interoperate between the Rust and C++ programming languages, allowing you to use the strengths of both...
Converting a PNG (Portable Network Graphics) image to a PDF (Portable Document Format) document is a common task in the world of programming. In this article, we will explore how to convert a PNG image to a PDF document using the Go programming language.
To convert a PNG image to a PDF document, we will use the encoding/pdf package from the standard Go library. This package provides functions and types that allow us to create and manipulate PDF documents.
To begin, we will import the encoding/pdf package and the image/png package, which provides support for reading and writing PNG images.
import (
"image/png"
"encoding/pdf"
)
Next, we will open the PNG image file using the png.Decode function. This function returns an image.Image object, which represents the PNG image in memory.
// Open the PNG image file.
pngFile, err := os.Open("image.png")
if err != nil {
// Handle the error.
return
}
defer pngFile.Close()
// Decode the PNG image.
pngImage, err := png.Decode(pngFile)
if err != nil {
// Handle the error.
return
}
Now that we have the PNG image in memory, we can create a new PDF document and add the PNG image to it. To create a new PDF document, we will use the pdf.New() function, which returns a pdf.Document object.
To add the PNG image to the PDF document, we will first create a pdf.Image object using the pdf.NewImageFromImage() function. This function takes an image.Image object as an argument and returns a pdf.Image object that can be added to the PDF document.
Once we have a pdf.Image object, we can use the pdf.AddImage() function to add the image to the PDF document. This function takes the pdf.Image object and the desired width and height of the image in the PDF document as arguments.
// Create a new PDF document.
pdfDocument := pdf.New()
// Create a new PDF image from the PNG image.
pdfImage := pdf.NewImageFromImage(pngImage)
// Add the image to the PDF document with a width of 500 points and a height of 400 points.
pdfDocument.AddImage(pdfImage, 500, 400)
Finally, we can save the PDF document to a file using the pdf.Encode() function. This function takes a pdf.Document object and an io.Writer object as arguments, and it writes the PDF document to the specified writer. In this case, we will use the os.Create() function to create a new file and use that file as the writer for the PDF document.
// Save the PDF document to a file.
pdfFile, err := os.Create("image.pdf")
if err != nil {
// Handle the error.
return
}
defer pdfFile.Close()
// Encode the PDF document to the file.
if err := pdfDocument.Encode(pdfFile); err != nil {
// Handle the error.
return
}
With that we have successfully converted the png file to pdf format.
If you liked this article, please share it with others.