Skip to content

Commit e92c9d4

Browse files
committed
Stream example
1 parent 02fcb9e commit e92c9d4

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

examples/streams/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
convertapi "github.com/ConvertAPI/convertapi-go/pkg"
6+
"github.com/ConvertAPI/convertapi-go/pkg/config"
7+
"github.com/ConvertAPI/convertapi-go/pkg/param"
8+
"io"
9+
"log"
10+
"os"
11+
"strings"
12+
)
13+
14+
func main() {
15+
config.Default = config.NewDefault(os.Getenv("CONVERTAPI_SECRET")) // Get your secret at https://www.convertapi.com/a
16+
17+
fmt.Println("Converting HTML from the stream to TXT")
18+
19+
reader := strings.NewReader("<!DOCTYPE html><html><head><title>My First Heading</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>\n")
20+
21+
htmlRes := convertapi.ConvDef("html", "txt",
22+
// Reading source file body from the io.Reader
23+
param.NewReader("file", reader, "page.html", nil),
24+
)
25+
26+
fmt.Println("TXT file content:")
27+
28+
// Copy the content of the reader to stdout.
29+
if _, err := io.Copy(os.Stdout, htmlRes); err != nil {
30+
log.Fatalf("failed to copy data: %s", err)
31+
}
32+
}

examples/web/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
func main() {
1212
config.Default = config.NewDefault(os.Getenv("CONVERTAPI_SECRET")) // Get your secret at https://www.convertapi.com/a
1313

14-
fmt.Println("Converting remote PPTX to PDF")
15-
pptxRes := convertapi.ConvDef("web", "pdf",
14+
fmt.Println("Converting WEB page to PDF")
15+
webRes := convertapi.ConvDef("web", "pdf",
1616
param.NewString("url", "https://en.wikipedia.org/wiki/Data_conversion"),
1717
param.NewString("filename", "web-example"),
1818
)
1919

20-
if files, errs := pptxRes.ToPath("/tmp"); errs == nil {
20+
if files, errs := webRes.ToPath("/tmp"); errs == nil {
2121
fmt.Println("PDF file saved to: ", files[0].Name())
2222
} else {
2323
fmt.Println(errs)

0 commit comments

Comments
 (0)