Initial Commit

This commit is contained in:
Aliberk Sandıkçı 2023-10-26 19:01:31 +03:00
commit 599133ab25
Signed by: asandikci
GPG key ID: 25C67A03B5666BC1
3 changed files with 94 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
output/

5
README.md Normal file
View file

@ -0,0 +1,5 @@
### URL -> IMG -> PDF
Get images from URL(s) and convert it/them to Pdf file
### TODO
- [ ] use pdfcpu to convert all images to pdf (workaround: use pdfArrenger)

88
url-img-pdf.go Normal file
View file

@ -0,0 +1,88 @@
package main
// Download images from web URLs and convert them to a pdf file with one click
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
)
func main() {
fmt.Println("SCRIPT SUCCESFULLY STARTED")
flag := 0
fmt.Println("Is files ready? (1/0) (to just convert images to pdf and skip download section press 1)")
fmt.Scanln(&flag)
var name string
if flag == 0 {
var baseURL, fileFormat string
var startNumber, endNumber int
fmt.Println("Folder Name: (enter for default) ")
fmt.Scanln(&name)
fmt.Println("Base URL: ")
fmt.Scanln(&baseURL)
fmt.Println("Start Number: ")
fmt.Scanln(&startNumber)
fmt.Println("End Number: ")
fmt.Scanln(&endNumber)
fmt.Println("File Format: (enter for default .jpg format) ")
fmt.Scanln(&fileFormat)
if name == "" {
name = "url-img-pdf_FOLDER"
}
if fileFormat == "" {
fileFormat = "jpg"
}
name = "/output/" + name
if err := os.Mkdir(name, os.ModePerm); err != nil {
log.Fatal(err)
}
for i := startNumber; i <= endNumber; i++ {
err := DownloadFile(name+"/img"+strconv.Itoa(i)+"."+fileFormat, baseURL+strconv.Itoa(i))
if err != nil {
panic(err)
}
fmt.Println("Downloaded: " + baseURL + strconv.Itoa(i))
}
}
fmt.Println("installed all images, you can use a tool like pdfarranger for converting (too lazy to implement)")
}
// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory. (NEEDS TEST!)
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}