Convert a PDF File to PDF/A in Go
Worried about losing the integrity of your formatting or data when sharing a PDF file? If so, it may be a good idea to proactively convert your file to the standardized PDF/A file type to ensure no data or design loss occurs. Unlike PDF files, PDF/A files prohibit certain features that may be incompatible with other systems or for long-term archiving, such as encryption, audio, and video. To simplify the conversion process, you can use the following API to automatically transform an input PDF file to the PDF/A format.
To run the API we simply need to plug in our target file and API key into the below example code:
package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)func main() {url := "https://api.cloudmersive.com/convert/edit/pdf/optimize/pdf-a"
method := "POST"payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
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("conformanceLevel", "<string>")
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))
}
Done! The process will be completed in no time, and your new PDF/A file will be ready to go. To retrieve your API key, visit the Cloudmersive website to register for a free account; this will give you access to 800 calls/month across our extensive API library.