Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace poppler with inline code #11

Open
gedw99 opened this issue Aug 26, 2022 · 0 comments
Open

replace poppler with inline code #11

gedw99 opened this issue Aug 26, 2022 · 0 comments

Comments

@gedw99
Copy link

gedw99 commented Aug 26, 2022

We can replace

cmd, _ := exec.Command("pdftoppm", "-png", PDFPath, folderName+"/png_gen").Output()
with this if we want. ....

package main

import (
	"flag"
	"fmt"
	"image/jpeg"
	"os"
	"path/filepath"

	"github.com/gen2brain/go-fitz"
)

func main() {
	// flags for source dir (pdf) and output dir (png,etc)

	sourceFile := flag.String("source", ".", "source file pdf")
	targetDir := flag.String("target", ".", "target dir")

	flag.Parse()

	fmt.Println("sourceFile:", *sourceFile)
	fmt.Println("targetDir:", *targetDir)

	doc, err := fitz.New(*sourceFile)
	if err != nil {
		panic(err)
	}

	defer doc.Close()
	/*
		// output to runtime dir
		currentDir, err := os.Getwd()
		//tmpDir, err := ioutil.TempDir(os.TempDir(), "fitz")
		if err != nil {
			panic(err)
		}

		// concat out dir
		tmpDir := filepath.Join(currentDir, "out")
		err = os.MkdirAll(tmpDir, os.ModePerm)
	*/
	err = os.MkdirAll(*targetDir, os.ModePerm)

	if err != nil {
		panic(err)
	}

	// Extract pages as images
	for n := 0; n < doc.NumPage(); n++ {
		img, err := doc.Image(n)
		if err != nil {
			panic(err)
		}

		f, err := os.Create(filepath.Join(*targetDir, fmt.Sprintf("test%03d.jpg", n)))
		if err != nil {
			panic(err)
		}

		err = jpeg.Encode(f, img, &jpeg.Options{jpeg.DefaultQuality})
		if err != nil {
			panic(err)
		}

		f.Close()
	}

	// Extract pages as text
	for n := 0; n < doc.NumPage(); n++ {
		text, err := doc.Text(n)
		if err != nil {
			panic(err)
		}

		f, err := os.Create(filepath.Join(*targetDir, fmt.Sprintf("test%03d.txt", n)))
		if err != nil {
			panic(err)
		}

		_, err = f.WriteString(text)
		if err != nil {
			panic(err)
		}

		f.Close()
	}

	// Extract pages as html
	for n := 0; n < doc.NumPage(); n++ {
		html, err := doc.HTML(n, true)
		if err != nil {
			panic(err)
		}

		f, err := os.Create(filepath.Join(*targetDir, fmt.Sprintf("test%03d.html", n)))
		if err != nil {
			panic(err)
		}

		_, err = f.WriteString(html)
		if err != nil {
			panic(err)
		}

		f.Close()
	}
}

This will build for all OS because the libs are included for all os at https://github.com/gen2brain/go-fitz/tree/master/libs

works for me on Mac. Maybe test on windows, and linux.

it would replace poppler which is very heavy IMHO and make the golang binary fully contained to a single file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant