Skip to content

Commit b4383be

Browse files
Add files via upload
1 parent 9f03bfd commit b4383be

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

uses_cases/merge/mergeDocuments.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"path"
6+
7+
asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
8+
)
9+
10+
func mergeDocuments(pdf_api *asposepdfcloud.PdfApiService, output_name string, remote_folder string) {
11+
names := []string{PDF_DOCUMENT, PDF_DOCUMENT_2, PDF_DOCUMENT_3}
12+
13+
mergeDocuments := asposepdfcloud.MergeDocuments{
14+
List: []string{},
15+
}
16+
17+
for _, name := range names {
18+
uploadFile(pdf_api, name)
19+
mergeDocuments.List = append(mergeDocuments.List, path.Join(remote_folder, name))
20+
}
21+
22+
args := map[string]interface{}{
23+
"folder": remote_folder,
24+
}
25+
26+
_, httpResponse, err := pdf_api.PutMergeDocuments(output_name, mergeDocuments, args)
27+
28+
if err != nil {
29+
fmt.Println(err.Error())
30+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
31+
fmt.Println("Unexpected error!")
32+
} else {
33+
downloadFile(pdf_api, output_name)
34+
}
35+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
"path/filepath"
8+
9+
asposepdfcloud "github.com/aspose-pdf-cloud/aspose-pdf-cloud-go/v25"
10+
)
11+
12+
// Set file names and folders
13+
const (
14+
REMOTE_FOLDER = "Merge_Temp_Pdf_Cloud"
15+
LOCAL_FOLDER = "c:\\Samples"
16+
PDF_DOCUMENT = "sample.pdf"
17+
PDF_DOCUMENT_2 = "sample_2.pdf"
18+
PDF_DOCUMENT_3 = "sample_3.pdf"
19+
PDF_OUTPUT = "output_sample.pdf"
20+
)
21+
22+
// Initialize credentials and Rest API service
23+
func initPdfApi() *asposepdfcloud.PdfApiService {
24+
AppSID := "*****"
25+
AppKey := "*****"
26+
27+
pdfApi := asposepdfcloud.NewPdfApiService(AppSID, AppKey, "")
28+
return pdfApi
29+
}
30+
31+
// Upload local file to the remote folder with check errors
32+
func uploadFile(pdf_api *asposepdfcloud.PdfApiService, name string) {
33+
args := map[string]interface{}{
34+
"folder": REMOTE_FOLDER,
35+
}
36+
file, err := os.Open(filepath.Join(LOCAL_FOLDER, name))
37+
if err != nil {
38+
fmt.Println(err.Error())
39+
} else {
40+
result, httpResponse, err := pdf_api.UploadFile(path.Join(REMOTE_FOLDER, name), file, args)
41+
if err != nil {
42+
fmt.Println(err.Error())
43+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
44+
fmt.Println("Unexpected error!")
45+
} else {
46+
fmt.Println(result)
47+
}
48+
}
49+
}
50+
51+
// Download file from remote folder and save it locally with check errors
52+
func downloadFile(pdf_api *asposepdfcloud.PdfApiService, name string) {
53+
args := map[string]interface{}{
54+
"folder": REMOTE_FOLDER,
55+
}
56+
result_data, httpResponse, err := pdf_api.DownloadFile(path.Join(REMOTE_FOLDER, name), args)
57+
if err != nil {
58+
fmt.Println(err.Error())
59+
} else if httpResponse.StatusCode < 200 || httpResponse.StatusCode > 299 {
60+
fmt.Println("Unexpected error!")
61+
} else {
62+
fmt.Println("File '" + name + "'successfully downloaded.")
63+
fileName := path.Join(LOCAL_FOLDER, PDF_OUTPUT)
64+
f, _ := os.Create(fileName)
65+
_, _ = f.Write(result_data)
66+
}
67+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
func main() {
4+
pdfApi := initPdfApi()
5+
mergeDocuments(pdfApi, PDF_DOCUMENT, REMOTE_FOLDER)
6+
7+
}

0 commit comments

Comments
 (0)