Lambda deployement
I tried using pdftotext for extracting text from pdf.
My BE is deployed in AWS lambda. I added this to the dockerfile:
`RUN yum install -y poppler-utils && yum clean all`
The code i am using is (Go):
`func IsTextBasedPDFUsingPoppler(ctx context.Context, fileBytes []byte) (bool, string) {
defer func() {
if r := recover(); r != nil {
logger.Get(ctx).Errorf("Panic in IsTextBasedPDF: %v", r)
}
}()
logger.Get(ctx).Infof("Running pdftotext, pdf_size=%d", len(fileBytes))
// timeout protection - generous timeout for Lambda environment
ctxTimeout, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
env := os.Getenv("ENV")
pdfExtractionLibraryPath := "/usr/bin/pdftotext" // Default Linux path for Lambda
if env == "qa" {
pdfExtractionLibraryPath = "C:\\Program Files\\poppler-25.12.0\\Library\\bin\\pdftotext.exe"
}
// Log the path being used
logger.Get(ctx).Infof("Using pdftotext path: %s (ENV=%s)", pdfExtractionLibraryPath, env)
cmd := exec.CommandContext(
ctxTimeout,
pdfExtractionLibraryPath,
"-enc", "UTF-8",
"-f", "1",
"-l", "5",
"-nopgbrk",
"-", // read PDF from stdin
"-", // write text to stdout
)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// Get stdin pipe and write PDF bytes, then close it
stdin, err := cmd.StdinPipe()
if err != nil {
logger.Get(ctx).Errorf("failed to get stdin pipe: %v", err)
return false, ""
}
// Start the command
if err := cmd.Start(); err != nil {
logger.Get(ctx).Errorf("failed to start pdftotext: %v", err)
return false, ""
}
// Write PDF bytes to stdin in a goroutine and close it
writeErr := make(chan error, 1)
go func() {
defer stdin.Close()
_, err := stdin.Write(fileBytes)
writeErr <- err
}()
logger.Get(ctx).Infof("Waiting for pdftotext to complete...")
// Wait for command to finish
err = cmd.Wait()
// Check if there was a write error
if wErr := <-writeErr; wErr != nil {
logger.Get(ctx).Errorf("failed to write to stdin: %v", wErr)
}
if err != nil {
// Check if the binary exists
if _, statErr := os.Stat(pdfExtractionLibraryPath); os.IsNotExist(statErr) {
logger.Get(ctx).Errorf("pdftotext binary not found at path: %s", pdfExtractionLibraryPath)
}
logger.Get(ctx).Errorf("pdftotext failed: %v stderr=%s stdout=%s", err, stderr.String(), stdout.String())
return false, ""
}
extractedText := strings.Join(strings.Fields(stdout.String()), " ")
logger.Get(ctx).Infof("pdftotext finished")
// simple threshold detection
if len(extractedText) < 50 {
return false, ""
}
return true, extractedText
}`
but the code is not working.
Pls help to debug.
0 条评论