-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCvtGdocToPdf.go
74 lines (60 loc) · 1.36 KB
/
CvtGdocToPdf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// v1
// v2 start a text debug
// 10/1/22
//
//
package main
import (
"fmt"
"os"
"io"
gdriveApi "google/gdrive/gdriveApi"
)
func main() {
var gd gdriveApi.GdriveApiStruct
numArgs := len(os.Args)
if numArgs < 2 {
fmt.Println("error - no comand line arguments!")
fmt.Println("CvtGdocToPdf usage is:\n CvtGdocToPdf docId\n")
os.Exit(1)
}
docId := os.Args[1]
fmt.Println("Doc Id: ", docId)
err := gd.Init()
if err != nil {
fmt.Println("Init error: ", err)
os.Exit(1)
}
srv := gd.Svc
// docId := "1pdI_GFPR--q88V3WNKogcPfqa5VFOpzDZASo4alCKrE"
fil, err := srv.Files.Get(docId).Do()
if err != nil {
fmt.Println("Unable to find file!", err)
os.Exit(1)
}
filnam := fil.Name
fmt.Println("file name: ", filnam)
if len(filnam) <1 {
fmt.Println("not a valid file name")
os.Exit(1)
}
mimeType := "application/pdf"
res, err := srv.Files.Export(docId, mimeType).Context(gd.Ctx).Download()
if err != nil {
fmt.Println("Unable to download document into app: ", err)
os.Exit(1)
}
outfil, err := gd.CreTxtOutFile(filnam, "pdf")
if err != nil {
fmt.Println("error main -- cannot open out file: ", err)
os.Exit(1)
}
_, err = io.Copy(outfil, res.Body)
if err != nil {
fmt.Println("error main -- cannot convert gdoc file: ", err)
os.Exit(1)
}
outfil.Close()
fmt.Println("Success!")
os.Exit(0)
}