package utils import ( "fmt" "github.com/gen2brain/go-fitz" "image/jpeg" "os" "path/filepath" "time" ) // 一些计算 // ComputeIndividualIncomeTax 计算个人所得税 func ComputeIndividualIncomeTax(income float64) float64 { if income <= 800 { return 0 } if income <= 4000 { income = income - 800 } // 实际纳税金额 if income > 4000 { income = income * 0.8 } // 税率、速算扣除数 var taxRate, quickDeduction float64 if income <= 20000 { taxRate = 0.2 quickDeduction = 0 } else if income <= 50000 { taxRate = 0.3 quickDeduction = 2000 } else { taxRate = 0.4 quickDeduction = 7000 } incomeTax := income*taxRate - quickDeduction 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 } // CalculateAge 计算年龄 func CalculateAge(birthdate string) (int, error) { // 解析出生日期字符串 layout := "2006-01-02 15:04:05" birthTime, err := time.Parse(layout, birthdate) if err != nil { return 0, err } // 获取当前时间 now := time.Now() // 计算年龄 years := now.Year() - birthTime.Year() // 如果今年的生日还没到,年龄减一 if now.Month() < birthTime.Month() || (now.Month() == birthTime.Month() && now.Day() < birthTime.Day()) { years-- } return years, nil }