新增了 退还用户优惠卷、新增了处理用户处方图片
This commit is contained in:
@@ -1,5 +1,13 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gen2brain/go-fitz"
|
||||
"image/jpeg"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// 一些计算
|
||||
|
||||
// ComputeIndividualIncomeTax 计算个人所得税
|
||||
@@ -35,3 +43,43 @@ func ComputeIndividualIncomeTax(income float64) float64 {
|
||||
|
||||
return incomeTax
|
||||
}
|
||||
|
||||
// ConvertPDFToImages converts a PDF file to images and saves them in the specified output directory.
|
||||
func ConvertPDFToImages(pdfPath string, outputDir string, filename string) error {
|
||||
// Open the PDF file
|
||||
doc, err := fitz.New(pdfPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open PDF file: %v", err)
|
||||
}
|
||||
defer doc.Close()
|
||||
|
||||
// Ensure the output directory exists
|
||||
if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("failed to create output directory: %v", err)
|
||||
}
|
||||
|
||||
// Iterate over each page in the PDF
|
||||
for i := 0; i < doc.NumPage(); i++ {
|
||||
// Render the page to an image
|
||||
img, err := doc.Image(i)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to render page %d: %v", i, err)
|
||||
}
|
||||
|
||||
// Create the output file
|
||||
outputFile := filepath.Join(outputDir, filename)
|
||||
file, err := os.Create(outputFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create output file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Encode the image as JPEG and save it to the file
|
||||
opts := &jpeg.Options{Quality: 80}
|
||||
if err := jpeg.Encode(file, img, opts); err != nil {
|
||||
return fmt.Errorf("failed to encode image: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user