gen2brain / go-fitz

Golang wrapper for the MuPDF Fitz library
GNU Affero General Public License v3.0
429 stars 97 forks source link

undefined: fitz.Document #118

Closed lilien1010 closed 1 month ago

lilien1010 commented 1 month ago

I am moving all the logic of pdf to a sub module, but facing a error like below.

What could be the protential rootcauses?

xxx/internal/pkg/pdf/fitz

internal/pkg/pdf/fitz/pdf.go:15:45: undefined: fitz.Document internal/pkg/pdf/fitz/pdf.go:16:19: undefined: fitz.New internal/pkg/pdf/fitz/pdf.go:36:40: undefined: fitz.Document make: *** [build-service/xxxx] Error 1

package fitz

import (
    "fmt"
    "image/png"
    "os"
    "path/filepath"
    "strings"

    "github.com/gen2brain/go-fitz"
    "github.com/pkg/errors"
    "github.com/rs/zerolog/log"
)

func AnalyzePdfPages(pdfPath string) (*fitz.Document, []int, error) {
    doc, err := fitz.New(pdfPath)
    if err != nil {
        return nil, nil, errors.Wrap(err, "failed to open PDF file")
    }

    var scannedPages []int
    for i := 0; i < doc.NumPage(); i++ {
        text, err := doc.Text(i)
        if err != nil {
            return nil, nil, fmt.Errorf("failed to get text from page %d: %v", i+1, err) 
        }

        if strings.TrimSpace(text) == "" {
            scannedPages = append(scannedPages, i+1) // Page numbers start from 1
        }
    }
    return doc, scannedPages, nil
}

func ExportScannedPagesImage(doc *fitz.Document, taskID int64, scannedPages []int) ([]string, error) {
    baseDir := os.TempDir()

    var outPaths []string

    for _, pageNum := range scannedPages {
        img, err := doc.Image(pageNum - 1) // Page numbers start from 1, but Image function starts from 0
        if err != nil {
            return nil, fmt.Errorf("failed to get image from page %d: %v", pageNum, err)
        }

        if img == nil {
            fmt.Printf("Warning: Page %d has no image content\n", pageNum)
            continue
        }

        outPath := filepath.Join(baseDir, fmt.Sprintf("%d_scanned_page_%d.png", taskID, pageNum))
        out, err := os.Create(outPath)
        if err != nil {
            return nil, fmt.Errorf("failed to create output file: %v", err)
        }
        defer out.Close()

        err = png.Encode(out, img)
        if err != nil {
            return nil, fmt.Errorf("failed to encode image: %v", err) 
        }

        outPaths = append(outPaths, outPath)
        log.Info().Msgf("Exported scanned page %d to %s\n", pageNum, outPath) 

    }

    return outPaths, nil
}
gen2brain commented 1 month ago

You are trying to build with CGO_ENABLED=0 or you are missing the C compiler.

baopq27 commented 1 month ago

@gen2brain I am also encountering that error while using Docker. Can you help me fix it?

gen2brain commented 1 month ago

Enable cgo with CGO_ENABLED=1 and you will see the actual error.

lilien1010 commented 1 month ago

enabled CGO still seeing errors

# runtime/cgo
linux_syscall.c:67:13: error: call to undeclared function 'setresgid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
linux_syscall.c:67:13: note: did you mean 'setregid'?
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/unistd.h:593:6: note: 'setregid' declared here
linux_syscall.c:73:13: error: call to undeclared function 'setresuid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
linux_syscall.c:73:13: note: did you mean 'setreuid'?
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/unistd.h:595:6: note: 'setreuid' declared here

I think the problem is I am using CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o

lilien1010 commented 1 month ago

https://stackoverflow.com/questions/77293369/dynamic-linked-go-program-when-cross-compile

lilien1010 commented 1 month ago

Fixed by: brew tap SergioBenitez/osxct brew install x86_64-unknown-linux-gnu and build with : CGO_ENABLED=1 GOOS=linux go build -o main ./cmd/xxx/main.go

baopq27 commented 1 month ago

@gen2brain I successfully built it, but when using doc, err := fitz.New("test.pdf")

it throws an error: cannot create context: incompatible header (1.23.7) and library (1.24.2) versions. Which library is 'v1.24.2'? Can you help me?