commit 599133ab257b78a7f52ecc54a21013c304ae228b Author: asandikci Date: Thu Oct 26 19:01:31 2023 +0300 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b1960e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +output/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6bd1d1f --- /dev/null +++ b/README.md @@ -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) \ No newline at end of file diff --git a/url-img-pdf.go b/url-img-pdf.go new file mode 100644 index 0000000..cd812ec --- /dev/null +++ b/url-img-pdf.go @@ -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 +}