Skip to content

Commit 32e1984

Browse files
committed
First release
1 parent 85e5538 commit 32e1984

File tree

7 files changed

+384
-76
lines changed

7 files changed

+384
-76
lines changed

LICENSE.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ConvertAPI
2+
https://www.convertapi.com
3+
(c) 2011-2018 Baltsoft
4+
5+
ConvertAPI-CLI
6+
https://github.com/ConvertAPI/convertapi-cli
7+
Copyright (c) 2018 Tomas Rutkauskas
8+
9+
The MIT License
10+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
# ConvertAPI CLI Client
2+
3+
## Convert your files with our command line file conversion utility
4+
5+
The ConvertAPI helps converting various file formats.
6+
Creating PDF and Images from various sources like Word, Excel, Powerpoint, images, web pages or raw HTML codes.
7+
Merge, Encrypt, Split, Repair and Decrypt PDF files.
8+
And many others files manipulations.
9+
In just few minutes you can integrate it into your application or shell script and use it easily.
10+
11+
The ConvertAPI CLI makes it easier to use the Convert API from your shell without having to build your own HTTP calls.
12+
You can get your free secret at https://www.convertapi.com/a
13+
14+
## Installation
15+
16+
Download compressed CLI executable
17+
18+
* Linux: [convertapi_lin.zip](http://github)
19+
* Linux ARM64: [convertapi_lin_arm.zip](http://github)
20+
* Darwin (MacOS): [convertapi_mac.zip](http://github)
21+
* Windows: [convertapi_win.zip](http://github)
22+
23+
(this utility also can be built from source code for many other CPU and OS)
24+
25+
Unzip executable
26+
27+
```shell
28+
unzip convertapi_*.zip
29+
```
30+
31+
And you are done.
32+
Optionally you can move executable file to more appropriate place and make utility accessible for all local users. On Linux would be:
33+
34+
```shell
35+
sudo mv convertapi /usr/local/bin
36+
```
37+
38+
## Usage
39+
40+
### Before you start
41+
42+
In order to use this CLI utility you must create your free trial account on https://www.convertapi.com site.
43+
After sign up process you will get your secret at https://www.convertapi.com/a .
44+
Secret will be used in every CLI utility run.
45+
46+
### Most basic file conversion
47+
48+
Before we go in to details, short usage example how to convert DOCX file to PDF.
49+
50+
```shell
51+
convertapi --iformat=docx --oformat=pdf --params="file:@/path/to/test.docx" --out="@/path/to/resultdir" --secret=<YOUR_SECRET_HERE>
52+
```
53+
54+
### Arguments documentation
55+
56+
#### --iformat
57+
Input file format (file name extension).
58+
All supported input formats can be found [here](https://www.convertapi.com).
59+
_Example:_
60+
61+
```shell
62+
--iformat=docx
63+
```
64+
65+
#### --oformat
66+
Output file format (file name extension).
67+
All supported output formats can be found [here](https://www.convertapi.com).
68+
_Example:_
69+
70+
```shell
71+
--oformat=pdf
72+
```
73+
74+
#### --params
75+
Conversion parameter list.
76+
All supported conversion parameters can be found [here](https://www.convertapi.com).
77+
Parameter name and value is separated by `:` colon.
78+
Parameters are separated by `,` comma.
79+
_Example:_
80+
81+
```shell
82+
--params="param1:value1, param2:value2, param3:value3"
83+
```
84+
85+
##### File parameter
86+
87+
Special case is file parameter.
88+
Value can have prefix and can be provided as an array with `;` semicolon separator.
89+
Prefix can be one of those:
90+
91+
###### no prefix
92+
93+
No prefix means that value is http or https scheme URL to file.
94+
_Example:_
95+
96+
```shell
97+
--params="file:https://cdn.convertapi.com/cara/testfiles/presentation.pptx"
98+
```
99+
100+
###### @
101+
102+
Parameter values starting from `@` are treated as paths to local files or directories.
103+
_Example:_
104+
105+
```shell
106+
--params="file:@/path/to/myfile.doc"
107+
```
108+
109+
###### <
110+
111+
Parameters with `<` values are substituted with data taken from `STDIN`.
112+
As raw value can only be URL so in this case data received from `STDIN` should be URL or URL array separated by `\n`
113+
_Example:_
114+
115+
```shell
116+
--params="file:<"
117+
```
118+
119+
###### @<
120+
121+
Parameters with `@<` values are substituted with data taken from `STDIN`.
122+
Data received from `STDIN` should be local path to file or directory.
123+
Also can be an array of paths separated by `\n`.
124+
_Example:_
125+
126+
```shell
127+
--params="file:@<"
128+
```
129+
130+
###### <<
131+
132+
Parameters with `<<` values are substituted with data taken from `STDIN`.
133+
Data received from `STDIN` should be file content.
134+
_Example:_
135+
136+
```shell
137+
--params="file:<<"
138+
```
139+
140+
##### Array parameter
141+
142+
If parameter name is suffixed with `[]` parameter gets treated as an array parameter.
143+
Mainly array parameters are used when one conversion needs to accept multiple files (e.g. pdf merge or zip compression).
144+
145+
```shell
146+
--params="files[]:@/path/to/dir"
147+
```
148+
149+
#### --out
150+
151+
Argument defines how conversion result should be outputted.
152+
Values can be one of those:
153+
154+
##### url
155+
156+
This is default output method. It prints to `STDOUT` URL or array of URLs that point to converted files.
157+
This method should be used for conversion chaining.
158+
159+
##### @
160+
161+
Value prefixed with `@` is treated as local file or directory path where converted file will be stored.
162+
_Example:_
163+
164+
```shell
165+
--out="@/path/to/result.pdf"
166+
```
167+
or
168+
```shell
169+
--out="@/path/to/resultdir"
170+
```
171+
172+
##### stdout
173+
174+
Conversion result will be outputted to `STDOUT`.
175+
If result consists of multiple files, second and other files will be outputted to file descriptors starting from 3.
176+
177+
```shell
178+
--out="stdout"
179+
```
180+
181+
#### --secret
182+
183+
ConvertAPI user secret.
184+
Get your secret at https://www.convertapi.com/a
185+
186+
#### --version
187+
188+
Outputs CLI utility version information and exits.
189+
190+
#### --help
191+
192+
Displays short usage information.
193+
194+
195+
### Examples
196+
197+
Convert local DOCX file to PDF A3 page size saving result to `/path/to/resultdir`
198+
```shell
199+
convertapi --iformat=docx --oformat=pdf --params="file:@/path/to/test.docx, pagesize:a3" --out="@/path/to/resultdir" --secret=<YOUR_SECRET_HERE>
200+
```
201+
202+
Merge all PDF files that are in `/path/to/dir` directory and save it locally
203+
```shell
204+
convertapi --iformat=pdf --oformat=merge --params="files[]:@/path/to/dir" --out="@/path/to/resultdir" --secret=<YOUR_SECRET_HERE>
205+
```
206+
207+
Convert remote PPTX file to PDF saving result to `/path/to/result.pdf`
208+
```shell
209+
convertapi --iformat=pptx --oformat=pdf --params="file:https://example.com/myfile.pptx" --out="@/path/to/result.pdf" --secret=<YOUR_SECRET_HERE>
210+
```
211+
212+
Convert from DOCX to JPG and ZIP result JPG files
213+
```shell
214+
convertapi --iformat=docx --oformat=jpg --params="file:@/path/to/test.docx" --secret=<YOUR_SECRET_HERE> \
215+
| convertapi --iformat=jpg --oformat=zip --params="files[]:<" --out="@/path/to/result.zip" --secret=<YOUR_SECRET_HERE>
216+
```
217+
218+
Convert DOCX to PDF and save result in remote server over SSH
219+
```shell
220+
convertapi --iformat=docx --oformat=pdf --params="file:@/path/to/test.docx" --out=stdout --secret=<YOUR_SECRET_HERE> \
221+
| ssh user@myserver "cat >/tmp/my.pdf"
222+
```
223+
224+
Get PDF file from remote server, convert it to JPG and save result locally
225+
```shell
226+
ssh user@server "cat /tmp/my.pdf" \
227+
| convertapi --iformat=pdf --oformat=jpg --params="file:<<" --out=@/path/to/resultdir --secret=<YOUR_SECRET_HERE>
228+
```
229+
230+
Do PDF->JPG and DOCX->JPG conversions in parallel and ZIP result JPG files
231+
```shell
232+
( \
233+
convertapi --iformat=pdf --oformat=jpg --params="file:/path/to/dir" --secret=<YOUR_SECRET_HERE> \
234+
& convertapi --iformat=docx --oformat=jpg --params="file:@/path/to/dir" --secret=<YOUR_SECRET_HERE> \
235+
) | convertapi --iformat=jpg --oformat=zip --params="files[]:<" --out=@/path/to/resultdir --secret=<YOUR_SECRET_HERE>
236+
```
237+
238+
Merge PDFs files from various locations: remote SSH server, local file, local directory, remote HTTP server.
239+
Save result file to remote SSH server. All this done without writing to disk.
240+
```shell
241+
ssh user@server1 "cat /tmp/my.pdf" \
242+
| convertapi --iformat=pdf --oformat=merge --params="files[]:<<;@/path/to/test.pdf;@/path/to/dir;https://example.com/my.pdf" --out=stdout --secret=<YOUR_SECRET_HERE> \
243+
| ssh user@myserver2 "cat >/tmp/my.pdf"
244+
```
245+
246+
### Issues &amp; Comments
247+
Please leave all comments, bugs, requests, and issues on the Issues page. We'll respond to your request ASAP!
248+
249+
### License
250+
The ConvertAPI CLI is licensed under the [MIT](http://www.opensource.org/licenses/mit-license.php "Read more about the MIT license form") license.
251+
Refer to the [LICENSE](https://github.com/ConvertAPI/convertapi-cli/blob/master/LICENSE) file for more information.

build

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#/bin/bash
2+
3+
GOOS=linux GOARCH=amd64 go build -o bin/convertapi
4+
zip -q -j -m -9 bin/convertapi_lin.zip bin/convertapi
5+
6+
GOOS=linux GOARCH=arm go build -o bin/convertapi
7+
zip -q -j -m -9 bin/convertapi_lin_arm.zip bin/convertapi
8+
9+
GOOS=windows GOARCH=amd64 go build -o bin/convertapi.exe
10+
zip -q -j -m -9 bin/convertapi_win.zip bin/convertapi.exe
11+
12+
GOOS=darwin GOARCH=amd64 go build -o bin/convertapi
13+
zip -q -j -m -9 bin/convertapi_mac.zip bin/convertapi

convert.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"github.com/ConvertAPI/convertapi-go"
5+
"github.com/ConvertAPI/convertapi-go/config"
6+
"github.com/ConvertAPI/convertapi-go/param"
7+
"sync"
8+
)
9+
10+
func convert(inFormat string, outFormat string, paramsets [][]param.IParam, out string) {
11+
config.Default.HttpClient = newHttpClient()
12+
wg := &sync.WaitGroup{}
13+
// debug(paramsets)
14+
for _, set := range paramsets {
15+
// debug("Uploading ", set)
16+
if err := prepare(set); err != nil {
17+
printError(err, 1)
18+
}
19+
// debug("Converting ", set)
20+
21+
wg.Add(1)
22+
go func(set []param.IParam) {
23+
defer wg.Done()
24+
res := convertapi.Convert(inFormat, outFormat, set, nil)
25+
if files, err := res.Files(); err == nil {
26+
// debug("Downloading ", set)
27+
for _, file := range files {
28+
output(file, out)
29+
}
30+
} else {
31+
printError(err, 1)
32+
}
33+
}(set)
34+
}
35+
wg.Wait()
36+
}

httpclient.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"runtime"
7+
"strings"
8+
)
9+
10+
type transport struct {
11+
http.RoundTripper
12+
}
13+
14+
func (this *transport) RoundTrip(req *http.Request) (*http.Response, error) {
15+
agent := fmt.Sprintf("ConvertAPI-CLI/%d (%s)", Version, strings.Title(runtime.GOOS))
16+
req.Header.Add("User-Agent", agent)
17+
return this.RoundTripper.RoundTrip(req)
18+
}
19+
20+
func newHttpClient() *http.Client {
21+
return &http.Client{Transport: &transport{http.DefaultTransport}}
22+
}

0 commit comments

Comments
 (0)