From db88d3c0905b6b4a2985acc52679203f54f3837c Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Wed, 5 Mar 2025 17:35:12 +0200 Subject: [PATCH 1/5] Direct conversion --- README.md | 10 +++++++++ examples/direct/main.go | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 examples/direct/main.go diff --git a/README.md b/README.md index 9c19d49..16c8139 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,16 @@ func main() { This is the bare-minimum to convert a file using the ConvertAPI client, but you can do a great deal more with the ConvertAPI Go library. Take special note that you should replace `your-api-secret` with the secret you obtained in item two of the pre-requisites. + +### Conversion Without Temporarily Storing Files on the ConvertAPI File Server +By design, the ConvertAPI library uses a temporary file server to store files. +A temporary file server is useful for conversion chaining and generally improves performance. +However, the [Vanilla Go Example](examples/direct/main.go) demonstrates how to efficiently convert files without using the ConvertAPI file server. + +In this example, both the request and response use the `multipart` content type. This approach allows you to convert multiple files or return conversion results as multiple files. +Using `multipart` is much more efficient compared to JSON with base64 encoding. + + ### Issues & Comments Please leave all comments, bugs, requests, and issues on the Issues page. We'll respond to your request ASAP! diff --git a/examples/direct/main.go b/examples/direct/main.go new file mode 100644 index 0000000..f426b84 --- /dev/null +++ b/examples/direct/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "io" + "mime" + "mime/multipart" + "net/http" + "os" +) + +// Example of DOCX to PDF conversion using vanilla go. +// File is converted without temporary storing files on ConverAPI servers. +// No error handling to make an example easier to read. +// No file buffering to memory. To store file to memory in general is a bad idea! +func main() { + pipeReader, pipeWriter := io.Pipe() + multipartWriter := multipart.NewWriter(pipeWriter) + + // Adjust URL according your converter and set YOUR_SECRET. + req, _ := http.NewRequest("POST", "https://v2.convertapi.com/convert/docx/to/pdf?secret=YOUR_SECRET", pipeReader) + req.Header.Set("Content-Type", multipartWriter.FormDataContentType()) + req.Header.Set("Accept", "multipart/mixed") + + go func(mpWriter *multipart.Writer) { + defer pipeWriter.Close() + defer multipartWriter.Close() + filePart, _ := mpWriter.CreateFormFile("file", "test.docx") + f, _ := os.Open("assets/test.docx") // Open file that needs to be converted + defer f.Close() + f.WriteTo(filePart) + // If conversion takes multiple source files, it can be added here, as an example above. + }(multipartWriter) + + resp, _ := http.DefaultClient.Do(req) + save(resp, "/tmp/result.pdf") +} + +func save(resp *http.Response, file string) { + defer resp.Body.Close() + + mediaType := resp.Header.Get("Content-Type") + _, params, _ := mime.ParseMediaType(mediaType) + multipartReader := multipart.NewReader(resp.Body, params["boundary"]) + + part, _ := multipartReader.NextPart() + resFile, _ := os.Create(file) + defer resFile.Close() + io.Copy(resFile, part) + // If conversion returns multiple files, it can be red using multipartReader.NextPart() multiple times. +} From 5ddc4e1360f65b2cfd2fd6bae0469cf6a2950895 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Tue, 3 Jun 2025 10:53:00 +0300 Subject: [PATCH 2/5] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 9617a03..4997ced 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/ConvertAPI/convertapi-go +module github.com/ConvertAPI/convertapi-library-go go 1.22.2 From 94b8981df8ee7a2ca91881fb1dba843b0e2824b9 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Tue, 3 Jun 2025 11:20:51 +0300 Subject: [PATCH 3/5] Remove all references to Secret and ApiKey --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 16c8139..75fdda6 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ConvertAPI helps in converting various file formats. Creating PDF and Images from various sources like Word, Excel, Powerpoint, images, web pages or raw HTML codes. Merge, Encrypt, Split, Repair and Decrypt PDF files and many other file manipulations. You can integrate it into your application in just a few minutes and use it easily. The ConvertAPI-Go library makes it easier to use the Convert API from your Go projects without having to build your own API calls. -You can get your free API secret at https://www.convertapi.com/a +You can get your free API token at https://www.convertapi.com/a/authentication ## Installation @@ -21,10 +21,10 @@ go get github.com/ConvertAPI/convertapi-go ### Configuration -You can get your secret at https://www.convertapi.com/a +You can get your free API token at https://www.convertapi.com/a/authentication ```go -config.Default = config.NewDefault(os.Getenv("your-api-secret-or-token")) +config.Default = config.NewDefault(os.Getenv("api-token")) ``` ### File conversion @@ -111,7 +111,7 @@ import ( ) func main() { - config.Default = config.NewDefault(os.Getenv("your-api-secret-or-token")) + config.Default = config.NewDefault(os.Getenv("api-token")) if file, errs := convertapi.ConvertPath("test.docx", "/tmp/result.pdf"); errs == nil { fmt.Println("PDF file saved to: ", file.Name()) @@ -122,7 +122,7 @@ func main() { ``` This is the bare-minimum to convert a file using the ConvertAPI client, but you can do a great deal more with the ConvertAPI Go library. -Take special note that you should replace `your-api-secret` with the secret you obtained in item two of the pre-requisites. +Take special note that you should replace `api-token` with the token you obtained in item two of the pre-requisites. ### Conversion Without Temporarily Storing Files on the ConvertAPI File Server From 17de20da5382e69d70da7edf8a212a7f8924aaac Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Tue, 3 Jun 2025 11:21:55 +0300 Subject: [PATCH 4/5] BREAKING CHANGE: #16 `User.SecondsLeft` replaced with `ConversionsTotal` and `ConversionsConsumed` --- go.mod | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 4997ced..73b35e5 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/ConvertAPI/convertapi-library-go +module github.com/ConvertAPI/convertapi-go go 1.22.2 @@ -8,4 +8,4 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect -) +) \ No newline at end of file From 290b2d2fd4d36894954ba6e07aac468dd1b69d35 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Tue, 3 Jun 2025 11:31:41 +0300 Subject: [PATCH 5/5] Remove all references to Secret and ApiKey --- examples/advanced/main.go | 4 ++-- examples/chaining/main.go | 4 ++-- examples/direct/main.go | 6 +++--- examples/parallel/main.go | 4 ++-- examples/remote/main.go | 4 ++-- examples/simple/main.go | 4 ++-- examples/splitmerge/main.go | 4 ++-- examples/streams/main.go | 4 ++-- examples/thumbnail/main.go | 4 ++-- examples/user/main.go | 4 ++-- examples/web/main.go | 4 ++-- 11 files changed, 23 insertions(+), 23 deletions(-) diff --git a/examples/advanced/main.go b/examples/advanced/main.go index cf5a83c..2afa6ca 100644 --- a/examples/advanced/main.go +++ b/examples/advanced/main.go @@ -11,7 +11,7 @@ import ( ) func main() { - secret := os.Getenv("CONVERTAPI_SECRET") + token := os.Getenv("API_TOKEN") // Using convertapi.com server in Europe domain, _ := url.Parse("https://eu-v2.convertapi.com") @@ -21,7 +21,7 @@ func main() { transport := &http.Transport{Proxy: http.ProxyURL(proxy)} // Setting this configuration as default - config.Default = config.New(secret, domain, transport) + config.Default = config.New(token, domain, transport) fmt.Println("Converting remote PPTX to PDF") fileParam := param.NewString("file", "https://cdn.convertapi.com/cara/testfiles/presentation.pptx") diff --git a/examples/chaining/main.go b/examples/chaining/main.go index da6b00c..867ca40 100644 --- a/examples/chaining/main.go +++ b/examples/chaining/main.go @@ -10,7 +10,7 @@ import ( ) func main() { - config.Default = config.NewDefault(os.Getenv("CONVERTAPI_SECRET")) // Get your secret at https://www.convertapi.com/a + config.Default = config.NewDefault(os.Getenv("API_TOKEN")) // Get your token at https://www.convertapi.com/a/authentication fmt.Println("Converting PDF to JPG and compressing result files with ZIP") @@ -35,4 +35,4 @@ func main() { } else { fmt.Println(errs) } -} +} \ No newline at end of file diff --git a/examples/direct/main.go b/examples/direct/main.go index f426b84..1eb7171 100644 --- a/examples/direct/main.go +++ b/examples/direct/main.go @@ -16,8 +16,8 @@ func main() { pipeReader, pipeWriter := io.Pipe() multipartWriter := multipart.NewWriter(pipeWriter) - // Adjust URL according your converter and set YOUR_SECRET. - req, _ := http.NewRequest("POST", "https://v2.convertapi.com/convert/docx/to/pdf?secret=YOUR_SECRET", pipeReader) + // Adjust URL according your converter and set API_TOKEN. + req, _ := http.NewRequest("POST", "https://v2.convertapi.com/convert/docx/to/pdf?secret=API_TOKEN", pipeReader) req.Header.Set("Content-Type", multipartWriter.FormDataContentType()) req.Header.Set("Accept", "multipart/mixed") @@ -47,4 +47,4 @@ func save(resp *http.Response, file string) { defer resFile.Close() io.Copy(resFile, part) // If conversion returns multiple files, it can be red using multipartReader.NextPart() multiple times. -} +} \ No newline at end of file diff --git a/examples/parallel/main.go b/examples/parallel/main.go index 2e1110f..c38345b 100644 --- a/examples/parallel/main.go +++ b/examples/parallel/main.go @@ -9,7 +9,7 @@ import ( ) func main() { - config.Default = config.NewDefault(os.Getenv("CONVERTAPI_SECRET")) // Get your secret at https://www.convertapi.com/a + config.Default = config.NewDefault(os.Getenv("API_TOKEN")) // Get your token at https://www.convertapi.com/a/authentication fmt.Println("Converting DOCX to PDF and JPG in parallel using same source file") fileParam := param.NewPath("file", "assets/test.docx", nil) @@ -34,4 +34,4 @@ func save(res *convertapi.Result) (finish chan struct{}) { } }() return -} +} \ No newline at end of file diff --git a/examples/remote/main.go b/examples/remote/main.go index fce557e..7907a27 100644 --- a/examples/remote/main.go +++ b/examples/remote/main.go @@ -9,7 +9,7 @@ import ( ) func main() { - config.Default = config.NewDefault(os.Getenv("CONVERTAPI_SECRET")) // Get your secret at https://www.convertapi.com/a + config.Default = config.NewDefault(os.Getenv("API_TOKEN")) // Get your token at https://www.convertapi.com/a/authentication fmt.Println("Converting remote PPTX to PDF") pptxRes := convertapi.ConvDef("pptx", "pdf", @@ -20,4 +20,4 @@ func main() { } else { fmt.Println(errs) } -} +} \ No newline at end of file diff --git a/examples/simple/main.go b/examples/simple/main.go index d0c9f9b..2c483a1 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -8,10 +8,10 @@ import ( ) func main() { - config.Default = config.NewDefault(os.Getenv("CONVERTAPI_SECRET")) // Get your secret at https://www.convertapi.com/a + config.Default = config.NewDefault(os.Getenv("API_TOKEN")) // Get your token at https://www.convertapi.com/a/authentication if file, errs := convertapi.ConvertPath("assets/test.docx", "/tmp/result.pdf"); errs == nil { fmt.Println("PDF file saved to: ", file.Name()) } else { fmt.Println(errs) } -} +} \ No newline at end of file diff --git a/examples/splitmerge/main.go b/examples/splitmerge/main.go index 33f5d40..9ca0a10 100644 --- a/examples/splitmerge/main.go +++ b/examples/splitmerge/main.go @@ -9,7 +9,7 @@ import ( ) func main() { - config.Default = config.NewDefault(os.Getenv("CONVERTAPI_SECRET")) // Get your secret at https://www.convertapi.com/a + config.Default = config.NewDefault(os.Getenv("API_TOKEN")) // Get your token at https://www.convertapi.com/a/authentication fmt.Println("Creating PDF with the first and the last pages") splitRes := convertapi.ConvDef("pdf", "split", @@ -26,4 +26,4 @@ func main() { } else { fmt.Println(errs) } -} +} \ No newline at end of file diff --git a/examples/streams/main.go b/examples/streams/main.go index 5be6b6a..f592d7a 100644 --- a/examples/streams/main.go +++ b/examples/streams/main.go @@ -12,7 +12,7 @@ import ( ) func main() { - config.Default = config.NewDefault(os.Getenv("CONVERTAPI_SECRET")) // Get your secret at https://www.convertapi.com/a + config.Default = config.NewDefault(os.Getenv("API_TOKEN")) // Get your token at https://www.convertapi.com/a/authentication fmt.Println("Converting HTML from the stream to TXT") @@ -29,4 +29,4 @@ func main() { if _, err := io.Copy(os.Stdout, htmlRes); err != nil { log.Fatalf("failed to copy data: %s", err) } -} +} \ No newline at end of file diff --git a/examples/thumbnail/main.go b/examples/thumbnail/main.go index 42a7719..15da9d7 100644 --- a/examples/thumbnail/main.go +++ b/examples/thumbnail/main.go @@ -9,7 +9,7 @@ import ( ) func main() { - config.Default = config.NewDefault(os.Getenv("CONVERTAPI_SECRET")) // Get your secret at https://www.convertapi.com/a + config.Default = config.NewDefault(os.Getenv("API_TOKEN")) // Get your token at https://www.convertapi.com/a/authentication fmt.Println("Creating PDF thumbnail") extractRes := convertapi.ConvDef("pdf", "jpg", @@ -30,4 +30,4 @@ func main() { fmt.Println(errs) } -} +} \ No newline at end of file diff --git a/examples/user/main.go b/examples/user/main.go index b6dd8be..e274bfc 100644 --- a/examples/user/main.go +++ b/examples/user/main.go @@ -8,7 +8,7 @@ import ( ) func main() { - config.Default = config.NewDefault(os.Getenv("CONVERTAPI_SECRET")) // Get your secret at https://www.convertapi.com/a + config.Default = config.NewDefault(os.Getenv("API_TOKEN")) // Get your token at https://www.convertapi.com/a/authentication if user, err := convertapi.UserInfo(nil); err == nil { fmt.Println("User information: ") @@ -16,4 +16,4 @@ func main() { } else { fmt.Println(err) } -} +} \ No newline at end of file diff --git a/examples/web/main.go b/examples/web/main.go index 332080a..9fba812 100644 --- a/examples/web/main.go +++ b/examples/web/main.go @@ -9,7 +9,7 @@ import ( ) func main() { - config.Default = config.NewDefault(os.Getenv("CONVERTAPI_SECRET")) // Get your secret at https://www.convertapi.com/a + config.Default = config.NewDefault(os.Getenv("API_TOKEN")) // Get your token at https://www.convertapi.com/a/authentication fmt.Println("Converting WEB page to PDF") webRes := convertapi.ConvDef("web", "pdf", @@ -22,4 +22,4 @@ func main() { } else { fmt.Println(errs) } -} +} \ No newline at end of file