Browse Source

back_to_pdf flag implementation

master
Gitea 2 years ago
parent
commit
1d7bb784a9
  1. 2
      src/go.mod
  2. 12
      src/go.sum
  3. 61
      src/main.go

2
src/go.mod

@ -7,3 +7,5 @@ require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9
)
require github.com/jung-kurt/gofpdf v1.16.2 // indirect

12
src/go.sum

@ -1,8 +1,20 @@
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gen2brain/go-fitz v1.19.0 h1:tXuT5dpsxPNn7LS8eGv2uQ04EviyGb/o2AdEr1e4W0M=
github.com/gen2brain/go-fitz v1.19.0/go.mod h1:UZAxMETTDK4UPpuh80HaRpPzgkSibUihXVzwj2ip5oQ=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.16.2 h1:jgbatWHfRlPYiK85qgevsZTHviWXKwB1TTiKdz5PtRc=
github.com/jung-kurt/gofpdf v1.16.2/go.mod h1:1hl7y57EsiPAkLbOwzpzqgx1A30nQCk/YmFV8S2vmK0=
github.com/phpdave11/gofpdi v1.0.7/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 h1:LRtI4W37N+KFebI/qV0OFiLUv4GLOWeEW5hn/KEJvxE=
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

61
src/main.go

@ -13,6 +13,7 @@ import (
"github.com/gen2brain/go-fitz"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"github.com/jung-kurt/gofpdf"
"golang.org/x/image/font/gofont/goregular"
)
@ -25,7 +26,7 @@ var (
extractionDirPath *string = flag.String(
"dst_dir",
"output_images",
"output",
"Specify path to the output directory",
)
@ -46,6 +47,12 @@ var (
false,
"Add bottom space for a note or not",
)
backToPdf *bool = flag.Bool(
"back_to_pdf",
false,
"Convert extracted pages with a note back to pdf",
)
)
func addText(img *image.RGBA, x, y uint, text string, size float64) error {
@ -72,7 +79,7 @@ func addText(img *image.RGBA, x, y uint, text string, size float64) error {
func main() {
flag.Usage = func() {
fmt.Printf(` -add_note_space
fmt.Printf(` -add_note_space bool
Add additional white bottom space below a note or not
-dst_dir string
Specify path to the output directory (default "output_images")
@ -82,6 +89,8 @@ func main() {
Set a note that will be added to the bottom of each extracted image
-src_dir string
Specify path to the directory where each found PDF file's pages will be converted to images (default ".")
-back_to_pdf bool
Convert extracted pages with a note back to pdf
(c) Kasyanov Nikolay Alexeyevich (Unbewohnte)
`)
@ -125,6 +134,19 @@ func main() {
continue
}
if *note == "" && *backToPdf {
fmt.Printf("No note was specified. No need to convert back to PDF !\n")
*backToPdf = false
}
customPDF := gofpdf.New("P", "pt", "A4", "")
customPDF.AddUTF8FontFromBytes("goregular", "", goregular.TTF)
customPDF.SetFont("goregular", "", 15)
err = customPDF.Error()
if err != nil {
fmt.Printf("Could not create a new custom PDF: %s\n", err)
}
for i := 0; i < pdf.NumPage(); i++ {
srcPDFImage, err := pdf.Image(i)
if err != nil {
@ -132,9 +154,8 @@ func main() {
continue
}
outputImageFile, err := os.Create(
filepath.Join(saveDirPath,
strings.TrimSuffix(entryName, ".pdf")+fmt.Sprintf("_%d", i)+".png"))
outputImageFilePath := filepath.Join(saveDirPath, strings.TrimSuffix(entryName, ".pdf")+fmt.Sprintf("_%d", i)+".png")
outputImageFile, err := os.Create(outputImageFilePath)
if err != nil {
fmt.Printf("[%d] Could not create image file for %s page %d: %s\n", count, entryName, i, err)
continue
@ -162,12 +183,40 @@ func main() {
continue
}
png.Encode(outputImageFile, newImage)
err = png.Encode(outputImageFile, newImage)
if err != nil {
fmt.Printf("[%d] Could not encode %s, page %d to png format: %s\n", count, entryName, i, err)
continue
}
outputImageFile.Sync()
if *backToPdf {
// customPDF.AddPage()
customPDF.AddPageFormat("P", gofpdf.SizeType{
Wd: float64(newImage.Bounds().Dx()),
Ht: float64(newImage.Bounds().Dy()),
})
pageW, pageH, _ := customPDF.PageSize(i)
// := customPDF.GetPageSize()
customPDF.Image(outputImageFilePath, 0, 0, pageW, pageH, false, "", 0, "")
}
case false:
png.Encode(outputImageFile, srcPDFImage)
}
outputImageFile.Close()
}
if *note != "" && *backToPdf {
err = customPDF.OutputFileAndClose(
filepath.Join(saveDirPath, strings.TrimSuffix(entryName, ".pdf")+"_with_note.pdf"),
)
if err != nil {
fmt.Printf("Could not convert %s's pages back to pdf with a note: %s\n", entryName, err)
}
}
pdf.Close()
}
}

Loading…
Cancel
Save