Skip to content

Commit db88d3c

Browse files
committed
Direct conversion
1 parent 2cb2833 commit db88d3c

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ func main() {
124124
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.
125125
Take special note that you should replace `your-api-secret` with the secret you obtained in item two of the pre-requisites.
126126

127+
128+
### Conversion Without Temporarily Storing Files on the ConvertAPI File Server
129+
By design, the ConvertAPI library uses a temporary file server to store files.
130+
A temporary file server is useful for conversion chaining and generally improves performance.
131+
However, the [Vanilla Go Example](examples/direct/main.go) demonstrates how to efficiently convert files without using the ConvertAPI file server.
132+
133+
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.
134+
Using `multipart` is much more efficient compared to JSON with base64 encoding.
135+
136+
127137
### Issues & Comments
128138
Please leave all comments, bugs, requests, and issues on the Issues page. We'll respond to your request ASAP!
129139

examples/direct/main.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"io"
5+
"mime"
6+
"mime/multipart"
7+
"net/http"
8+
"os"
9+
)
10+
11+
// Example of DOCX to PDF conversion using vanilla go.
12+
// File is converted without temporary storing files on ConverAPI servers.
13+
// No error handling to make an example easier to read.
14+
// No file buffering to memory. To store file to memory in general is a bad idea!
15+
func main() {
16+
pipeReader, pipeWriter := io.Pipe()
17+
multipartWriter := multipart.NewWriter(pipeWriter)
18+
19+
// Adjust URL according your converter and set YOUR_SECRET.
20+
req, _ := http.NewRequest("POST", "https://v2.convertapi.com/convert/docx/to/pdf?secret=YOUR_SECRET", pipeReader)
21+
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
22+
req.Header.Set("Accept", "multipart/mixed")
23+
24+
go func(mpWriter *multipart.Writer) {
25+
defer pipeWriter.Close()
26+
defer multipartWriter.Close()
27+
filePart, _ := mpWriter.CreateFormFile("file", "test.docx")
28+
f, _ := os.Open("assets/test.docx") // Open file that needs to be converted
29+
defer f.Close()
30+
f.WriteTo(filePart)
31+
// If conversion takes multiple source files, it can be added here, as an example above.
32+
}(multipartWriter)
33+
34+
resp, _ := http.DefaultClient.Do(req)
35+
save(resp, "/tmp/result.pdf")
36+
}
37+
38+
func save(resp *http.Response, file string) {
39+
defer resp.Body.Close()
40+
41+
mediaType := resp.Header.Get("Content-Type")
42+
_, params, _ := mime.ParseMediaType(mediaType)
43+
multipartReader := multipart.NewReader(resp.Body, params["boundary"])
44+
45+
part, _ := multipartReader.NextPart()
46+
resFile, _ := os.Create(file)
47+
defer resFile.Close()
48+
io.Copy(resFile, part)
49+
// If conversion returns multiple files, it can be red using multipartReader.NextPart() multiple times.
50+
}

0 commit comments

Comments
 (0)