-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
260 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,64 @@ | ||
package utility | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"image/jpeg" | ||
"os" | ||
"path/filepath" | ||
// import ( | ||
// "bytes" | ||
// "fmt" | ||
// "image/jpeg" | ||
// "os" | ||
// "path/filepath" | ||
|
||
"github.com/gen2brain/go-fitz" | ||
) | ||
// "github.com/gen2brain/go-fitz" | ||
// ) | ||
|
||
func ExtractFirstPage(pdfBytes []byte) ([]byte, error) { | ||
doc, err := fitz.NewFromMemory(pdfBytes) | ||
if err != nil { | ||
fmt.Println("Error opening PDF:", err) | ||
return []byte{}, err | ||
} | ||
defer doc.Close() | ||
// func ExtractFirstPage(pdfBytes []byte) ([]byte, error) { | ||
// doc, err := fitz.NewFromMemory(pdfBytes) | ||
// if err != nil { | ||
// fmt.Println("Error opening PDF:", err) | ||
// return []byte{}, err | ||
// } | ||
// defer doc.Close() | ||
|
||
outputDir := filepath.Join(".", "output_images") | ||
// outputDir := filepath.Join(".", "output_images") | ||
|
||
if _, err := os.Stat(outputDir); os.IsNotExist(err) { | ||
err := os.Mkdir(outputDir, 0755) | ||
if err != nil { | ||
fmt.Println("Error creating output directory:", err) | ||
return []byte{}, err | ||
// if _, err := os.Stat(outputDir); os.IsNotExist(err) { | ||
// err := os.Mkdir(outputDir, 0755) | ||
// if err != nil { | ||
// fmt.Println("Error creating output directory:", err) | ||
// return []byte{}, err | ||
|
||
} | ||
} | ||
// } | ||
// } | ||
|
||
img, err := doc.Image(0) | ||
if err != nil { | ||
fmt.Println("Error extracting image from page:", err) | ||
return []byte{}, err | ||
} | ||
// img, err := doc.Image(0) | ||
// if err != nil { | ||
// fmt.Println("Error extracting image from page:", err) | ||
// return []byte{}, err | ||
// } | ||
|
||
// baseName := filepath.Base(pdfPath) | ||
// // baseName := filepath.Base(pdfPath) | ||
|
||
// outputFileName := fmt.Sprintf("first_page_%s.jpg", baseName) | ||
// outputFilePath := filepath.Join(outputDir, outputFileName) | ||
// // outputFileName := fmt.Sprintf("first_page_%s.jpg", baseName) | ||
// // outputFilePath := filepath.Join(outputDir, outputFileName) | ||
|
||
// f, err := os.Create(outputFilePath) | ||
// if err != nil { | ||
// fmt.Println("Error creating image file:", err) | ||
// return err | ||
// } | ||
// img. | ||
buf := new(bytes.Buffer) | ||
// bufio. | ||
// err := jpeg.Encode(buf, new_image, nil) | ||
// send_s3 := buf.Bytes() | ||
// bytes. | ||
err = jpeg.Encode(buf, img, &jpeg.Options{Quality: jpeg.DefaultQuality}) | ||
if err != nil { | ||
fmt.Println("Error encoding image to JPEG:", err) | ||
return []byte{}, err | ||
} | ||
// // f, err := os.Create(outputFilePath) | ||
// // if err != nil { | ||
// // fmt.Println("Error creating image file:", err) | ||
// // return err | ||
// // } | ||
// // img. | ||
// buf := new(bytes.Buffer) | ||
// // bufio. | ||
// // err := jpeg.Encode(buf, new_image, nil) | ||
// // send_s3 := buf.Bytes() | ||
// // bytes. | ||
// err = jpeg.Encode(buf, img, &jpeg.Options{Quality: jpeg.DefaultQuality}) | ||
// if err != nil { | ||
// fmt.Println("Error encoding image to JPEG:", err) | ||
// return []byte{}, err | ||
// } | ||
|
||
// f.Close() | ||
// // f.Close() | ||
|
||
// fmt.Printf("PDF first page converted to image successfully: %s\n", pdfPath) | ||
return buf.Bytes(), nil | ||
} | ||
// // fmt.Printf("PDF first page converted to image successfully: %s\n", pdfPath) | ||
// return buf.Bytes(), nil | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters