2025-03-07 17:23:50 +08:00

86 lines
1.8 KiB
Go

package utils
import (
"fmt"
"github.com/gen2brain/go-fitz"
"image/jpeg"
"os"
"path/filepath"
)
// 一些计算
// 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
}