go libreoffice word 转pdf

一、main.go 关键代码

完整代码

Go 复制代码
package main

import (
	"fmt"
	"github.com/jmoiron/sqlx"
	"github.com/tealeg/xlsx"
	"log"
	"os/exec"
	"path/filepath"
)
import _ "github.com/go-sql-driver/mysql"

import "net/http"
import "encoding/json"
import "io/ioutil"
import "github.com/google/uuid"

type Student struct {
	Name string
	Sex  string
}

type ResponseData struct {
	Status  string `json:"status"`
	Message string `json:"message"`
}

func (student Student) show() {
	fmt.Printf("Name:%s Sex:%s\r\n", student.Name, student.Sex)
}
func uploadFile(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	r.ParseMultipartForm(32 << 20) // 设置最大内存为32MB
	file, handler, err := r.FormFile("file")
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	defer file.Close()

	data, err := ioutil.ReadAll(file)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	ext := filepath.Ext(handler.Filename)
	// 这里可以将文件数据保存到服务器
	err2 := ioutil.WriteFile(uuid.NewString()+ext, data, 0644)
	if err2 != nil {
		return
	}

	response := ResponseData{
		Status:  "success",
		Message: "Hello, this is a JSON response",
	}
	jsonResponse, err3 := json.Marshal(response)
	if err3 != nil {
		http.Error(w, err3.Error(), http.StatusInternalServerError)
		return
	}
	w.Write(jsonResponse)
}

func convertToPDF(inputFile, outputFile string) {
	cmd := exec.Command("libreoffice", "--headless", "--convert-to", "pdf", inputFile, "--outdir", outputFile)
	err := cmd.Run()
	if err != nil {
		log.Fatal("转换失败:", err)
	}
	log.Println("转换成功!")
}

func main() {

	inputFile := "/root/doc1.docx"
	outputFile := "/root/output/"
	convertToPDF(inputFile, outputFile)

	http.HandleFunc("/upload", uploadFile)
	http.ListenAndServe("0.0.0.0:5000", nil)
}
func saveexcel() {
	excelPath := "students.xlsx"
	xlFile, err := xlsx.OpenFile(excelPath)
	if err != nil {
		fmt.Printf("Error opening Excel file: %s\n", err)
		return
	}
	sheet := xlFile.Sheets[0]
	var students []Student
	i := 0
	for _, row := range sheet.Rows {
		name := row.Cells[0].String()
		sex := row.Cells[1].String()
		fmt.Printf("name:%s sex:%s \r\n", name, sex)
		students = append(students, Student{Name: name, Sex: sex})
		i++
	}
	for _, student := range students {
		student.show()
	}

	fmt.Println("数据库操作")
	dsn := "root:123456@tcp(192.168.31.132:3306)/demo"
	db, err := sqlx.Open("mysql", dsn)
	if err != nil {
		log.Fatal(err)
	}

	for i, student := range students {
		if i == 0 {
			continue
		}
		db.NamedExec("insert into students(Name,Sex) values(:name,:sex)", student)
	}

	var studentMaps []*Student
	err = db.Select(&studentMaps, `select name,sex from students where name=?`, "张三")
	if err != nil {
		log.Fatal(err)
	}
	for _, student := range studentMaps {
		student.show()
	}
}

二、运行效果:

相关推荐
weixin_3975740911 天前
PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战
大数据·人工智能·pdf
Metaphor69211 天前
使用 Python 将 PDF 转换为 HTML
python·pdf·html
2601_9618451511 天前
粉笔行测5000题电子版|pdf|解析
pdf·新媒体运营·github·个人开发·内容运营·规格说明书·极限编程
Sour11 天前
PDF翻译卡住不动怎么办?扫描件、OCR 和大文件排查清单
前端·pdf·ocr
狂奔solar11 天前
OpenDataLoader-PDF 做 PDF 解析可视化调试器
pdf·rag 预处理
chatexcel11 天前
ChatExcel Max使用教程:图片、PDF、网页与复杂Excel的一站式数据分析
数据分析·pdf·excel
绘梨衣54711 天前
PDF表格解析知识总结
开发语言·python·pdf
qq_5469372711 天前
Excel批量转PDF_Word_图片,支持自动合并报表,效率翻倍。
pdf·word·excel
zyplayer-doc12 天前
企业知识库安全与权限管理完全指南:从加密到审计的六层防护
人工智能·安全·pdf·编辑器·创业创新
易鹤鹤.12 天前
pdf标注高亮
pdf