Skip to content

Instantly share code, notes, and snippets.

@olliephillips
Created March 13, 2023 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olliephillips/05d1b4da7a55423485827066eb793ba2 to your computer and use it in GitHub Desktop.
Save olliephillips/05d1b4da7a55423485827066eb793ba2 to your computer and use it in GitHub Desktop.
PDF split and recombine script
package main
import (
"fmt"
"github.com/pdfcpu/pdfcpu/pkg/api"
_ "github.com/pdfcpu/pdfcpu/pkg/api"
"log"
"os"
"path/filepath"
"strings"
)
func main() {
inputTitle := "gofaster-master.pdf"
choppedTitle := "./tmp/chop.pdf"
blankPage := "page2.pdf"
// remove the cover sheet and second page from the master
err := api.TrimFile(inputTitle, choppedTitle, []string{"3-216"}, nil)
if err != nil {
log.Fatalln("err")
}
// get all nft pdf covers
files, err := os.ReadDir("./nft")
if err != nil {
log.Fatalln(err)
}
nfts := []string{}
for _, file := range files {
ext := filepath.Ext(file.Name())
if ext == ".pdf" {
nfts = append(nfts, fmt.Sprintf("%s/%s", "./nft", file.Name()))
}
}
fmt.Println(nfts)
for _, n := range nfts {
number := strings.TrimLeft(n, "./nft/goatspeed-")
number = strings.TrimRight(number, ".pdf")
err := api.MergeCreateFile([]string{n, blankPage, choppedTitle}, fmt.Sprintf("./out/gofaster-nft-edition-%s.pdf", number), nil)
if err != nil {
log.Fatalln(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment