Merge Multiple PDF Files in Go

PDF Files are a static format, which makes them ideal for sharing but not for editing. If you have multiple PDF files you want to combine, it’s not a cut and paste situation — and who would want to spend the time to do that if it was? Instead, you can improve the organization of your documents and keep the formatting from each file by using the following API to merge up to ten PDF files in Go.

To run the operation, you simply need to input your files in the order you want them placed into the following code:

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/convert/merge/pdf/multi"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile1",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
file, errFile2 := os.Open("/path/to/file")
defer file.Close()
part2,
errFile2 := writer.CreateFormFile("inputFile2",filepath.Base("/path/to/file"))
_, errFile2 = io.Copy(part2, file)
if errFile2 != nil {
fmt.Println(errFile2)
return
}
file, errFile3 := os.Open("/path/to/file")
defer file.Close()
part3,
errFile3 := writer.CreateFormFile("inputFile3",filepath.Base("/path/to/file"))
_, errFile3 = io.Copy(part3, file)
if errFile3 != nil {
fmt.Println(errFile3)
return
}
file, errFile4 := os.Open("/path/to/file")
defer file.Close()
part4,
errFile4 := writer.CreateFormFile("inputFile4",filepath.Base("/path/to/file"))
_, errFile4 = io.Copy(part4, file)
if errFile4 != nil {
fmt.Println(errFile4)
return
}
file, errFile5 := os.Open("/path/to/file")
defer file.Close()
part5,
errFile5 := writer.CreateFormFile("inputFile5",filepath.Base("/path/to/file"))
_, errFile5 = io.Copy(part5, file)
if errFile5 != nil {
fmt.Println(errFile5)
return
}
file, errFile6 := os.Open("/path/to/file")
defer file.Close()
part6,
errFile6 := writer.CreateFormFile("inputFile6",filepath.Base("/path/to/file"))
_, errFile6 = io.Copy(part6, file)
if errFile6 != nil {
fmt.Println(errFile6)
return
}
file, errFile7 := os.Open("/path/to/file")
defer file.Close()
part7,
errFile7 := writer.CreateFormFile("inputFile7",filepath.Base("/path/to/file"))
_, errFile7 = io.Copy(part7, file)
if errFile7 != nil {
fmt.Println(errFile7)
return
}
file, errFile8 := os.Open("/path/to/file")
defer file.Close()
part8,
errFile8 := writer.CreateFormFile("inputFile8",filepath.Base("/path/to/file"))
_, errFile8 = io.Copy(part8, file)
if errFile8 != nil {
fmt.Println(errFile8)
return
}
file, errFile9 := os.Open("/path/to/file")
defer file.Close()
part9,
errFile9 := writer.CreateFormFile("inputFile9",filepath.Base("/path/to/file"))
_, errFile9 = io.Copy(part9, file)
if errFile9 != nil {
fmt.Println(errFile9)
return
}
file, errFile10 := os.Open("/path/to/file")
defer file.Close()
part10,
errFile10 := writer.CreateFormFile("inputFile10",filepath.Base("/path/to/file"))
_, errFile10 = io.Copy(part10, file)
if errFile10 != nil {
fmt.Println(errFile10)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

The output will capture all your original documents in one neat file. Head to the Cloudmersive website to retrieve your free API key and gain access to 800 calls/month across our entire API library.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store