1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834 |
- package services
- import (
- "baliance.com/gooxml/color"
- "baliance.com/gooxml/document"
- "baliance.com/gooxml/measurement"
- "baliance.com/gooxml/schema/soo/ofc/sharedTypes"
- "baliance.com/gooxml/schema/soo/wml"
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- wkhtml "github.com/SebastiaanKlippert/go-wkhtmltopdf"
- "github.com/shopspring/decimal"
- contractReq "hongze/hongze_mobile_admin/models/request/contract"
- "hongze/hongze_mobile_admin/models/tables/contract"
- "hongze/hongze_mobile_admin/models/tables/contract_service_detail"
- "hongze/hongze_mobile_admin/models/tables/contract_service_template"
- "hongze/hongze_mobile_admin/models/tables/contract_template"
- "hongze/hongze_mobile_admin/utils"
- "html/template"
- "os"
- "os/exec"
- "path"
- "reflect"
- "strconv"
- "strings"
- "time"
- )
- type TableData struct {
- List []TableRow `json:"table" description:"列数据"`
- }
- type TableRow struct {
- RowList []TableCel `json:"row" description:"列数据"`
- }
- type TableCel struct {
- Value string `json:"value" description:"展示的数据"`
- ColumnSpan int `json:"column_span" description:"需要合同的列数量"`
- RowSpan int `json:"row_span" description:"需要合同的行数量"`
- IsMerged bool `json:"is_merged" description:"是否需要上下行合并"`
- IsFirstMerged bool `json:"is_first_merged" description:"是否是第一次合并上下行"`
- Background string `json:"background" description:"背景色"`
- IsBold bool `json:"is_bold" description:"是否加粗显示"`
- TextAlign string `json:"text_align" description:"对齐方式"`
- FontSize float64 `json:"font_size" description:"字体大小"`
- WidthPercent float64 `json:"width_percent" description:"单元格宽度占整个表格的百分比"`
- }
- func getColorConf(background string) (foreground color.Color) {
- switch background {
- case "slate_gray":
- foreground = color.SlateGray
- case "light_slate_gray":
- foreground = color.LightSlateGray
- case "light_gray":
- foreground = color.LightGray
- case "gray":
- foreground = color.Gray
- case "gray_1":
- foreground = color.RGB(uint8(215), uint8(215), uint8(215))
- case "gray_2":
- foreground = color.RGB(uint8(241), uint8(241), uint8(241))
- case "dim_gray":
- foreground = color.DimGray
- case "dark_slate_gray":
- foreground = color.DarkSlateGray
- default:
- foreground = color.LightGray
- }
- return
- }
- func getTextAlignConf(textAlign string) (align wml.ST_Jc) {
- switch textAlign {
- case "left":
- align = wml.ST_JcLeft
- case "center":
- align = wml.ST_JcCenter
- case "right":
- align = wml.ST_JcRight
- case "both":
- align = wml.ST_JcBoth
- default:
- align = wml.ST_JcLeft
- }
- return
- }
- func GenerateWord(contractDetail *contract.ContractDetail) (err error) {
- wordTemplatePath := getWordPath(contractDetail.TemplateId)
- if wordTemplatePath == "" {
- err = errors.New("找不到对应的合同模板")
- return
- }
- doc, err := document.Open(wordTemplatePath)
- if err != nil {
- fmt.Println("error opening document: %s", err)
- return
- }
- paragraphs := []document.Paragraph{}
- for _, p := range doc.Paragraphs() {
- paragraphs = append(paragraphs, p)
- }
-
-
-
- for _, sdt := range doc.StructuredDocumentTags() {
- for _, p := range sdt.Paragraphs() {
- paragraphs = append(paragraphs, p)
- }
- }
- doc.AddParagraph()
- for _, p := range paragraphs {
- for _, r := range p.Runs() {
- switch r.Text() {
- case "{{address}}":
-
-
- r.ClearContent()
- address := getContractAddress(contractDetail)
- r.AddText(address)
-
-
-
-
-
-
-
-
- case "{{postcode}}":
- r.ClearContent()
- r.AddText(contractDetail.Postcode)
- case "{{phone}}":
- r.ClearContent()
- r.AddText(contractDetail.Phone)
- case "{{fax}}":
- r.ClearContent()
- r.AddText(contractDetail.Fax)
- case "{{remark}}":
- r.ClearContent()
- remark := contractDetail.Remark
- if remark == "" {
- remark = "无"
- }
- r.AddText(remark)
- case "{{start_date}}":
- r.ClearContent()
- r.AddText(contractDetail.StartDate.Format("2006 年 01 月 02 日"))
- case "{{end_date}}":
- r.ClearContent()
- r.AddText(contractDetail.EndDate.Format("2006 年 01 月 02 日"))
- case "{{num_year}}":
- r.ClearContent()
-
- newDecimal := decimal.NewFromFloat(contractDetail.EndDate.Sub(contractDetail.StartDate).Hours())
-
- newDecimal2 := decimal.NewFromInt(24 * 365)
-
- numYearDecimal := newDecimal.Div(newDecimal2).Round(1)
-
- minDecimal := decimal.NewFromFloat(0.1)
-
- if numYearDecimal.LessThan(minDecimal) {
- numYearDecimal = minDecimal
- }
-
-
-
-
-
- r.AddText(numYearDecimal.String())
- case "{{price}}":
- r.ClearContent()
- priceStr := ""
-
-
- newDecimal := decimal.NewFromFloat(contractDetail.OriginalPrice)
- originalPrice := newDecimal.String()
- priceStr += "小写:" + originalPrice + ","
-
- originalCnyPrice, cnyErr := utils.ConvertNumToCny(contractDetail.OriginalPrice)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- priceStr += "大写:" + originalCnyPrice
-
- if contractDetail.OriginalPrice != contractDetail.Price {
-
- newDecimal := decimal.NewFromFloat(contractDetail.Price)
- price := newDecimal.String()
- priceStr += ",经甲乙双方友好协商,优惠至:" + price + "元,"
-
- cnyPrice, cnyErr := utils.ConvertNumToCny(contractDetail.Price)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- priceStr += "大写:" + cnyPrice
- }
- r.AddText(priceStr)
- case "{{pay_remark}}":
- r.ClearContent()
- r.AddText(contractDetail.PayRemark)
- case "{{company_name}}":
- r.ClearContent()
- r.AddText(contractDetail.CompanyName)
-
- case "{{services}}":
- r.ClearContent()
-
- nowParagraph := p
- for i := len(contractDetail.Service) - 1; i >= 0; i-- {
-
- var tableDataList TableData
-
- tableTitle := ""
- item := contractDetail.Service[i]
-
- if item.HasDetail == "是" && len(item.DetailList) > 0 {
-
- tableRowList := make([]TableRow, 0)
-
- for j := 0; j < len(item.DetailList); j++ {
-
- isBold := false
- backgrandColor := ""
- fontSize := 10.0
-
- if j == 0 {
- isBold = true
- backgrandColor = "gray_2"
- fontSize = 12.0
- }
-
- tmpCellList, colErr := getColList(item.DetailList[j])
- if colErr != nil {
- err = colErr
- return
- }
-
- tableCelList := make([]TableCel, 0)
- lenCell := len(tmpCellList)
- for k := 0; k < len(tmpCellList); k++ {
-
-
- newDecimal := decimal.NewFromFloat(100)
-
- newDecimal2 := decimal.NewFromInt(int64(lenCell))
-
- widthPercent, _ := newDecimal.Div(newDecimal2).Round(3).Float64()
-
-
-
-
-
- tableCel := TableCel{
- Value: tmpCellList[k],
- TextAlign: "center",
-
-
- Background: backgrandColor,
- IsBold: isBold,
- FontSize: fontSize,
- WidthPercent: widthPercent,
- }
- tableCelList = append(tableCelList, tableCel)
- }
-
- tableRow := TableRow{
- RowList: tableCelList,
- }
- tableRowList = append(tableRowList, tableRow)
- }
-
- tableDataList.List = tableRowList
- tableTitle = "依照《弘则研究FICC客户服务列表2021》中 小套餐 的服务内容,详细如下:"
- } else {
-
- contractServiceTemplate, tmpErr := contract_service_template.GetContractServiceTemplateById(item.ServiceTemplateId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
- jsonStr := contractServiceTemplate.TableValue
- err = json.Unmarshal([]byte(jsonStr), &tableDataList)
- if err != nil {
- return
- }
-
- tableTitle = contractServiceTemplate.Remark
- }
-
- tmpParagraph, tmpErr := addTable(tableTitle, tableDataList, doc, nowParagraph)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
-
-
- nowParagraph = tmpParagraph
- }
- default:
-
- }
- }
- }
- doc.SaveToFile(fmt.Sprint("./static/word/系统生成合同", contractDetail.ContractId, ".docx"))
- return
- }
- func addTable(title string, tableDataList TableData, doc *document.Document, paragraph document.Paragraph) (nowParagraph document.Paragraph, err error) {
-
-
- nowParagraph = doc.InsertParagraphBefore(paragraph)
- nowRun := nowParagraph.AddRun()
- nowRun.AddBreak()
-
-
-
-
-
-
-
-
-
-
- {
- table := doc.InsertTableAfter(nowParagraph)
-
- table.Properties().SetWidth(6.5 * measurement.Inch)
-
-
-
- borders := table.Properties().Borders()
-
- borders.SetAll(wml.ST_BorderSingle, color.Auto, measurement.Zero)
-
- rowList := tableDataList.List
-
- rowIsMeged := make(map[int]bool)
-
- for i := 0; i < len(rowList); i++ {
-
- row := table.AddRow()
-
- row.Properties().SetHeight(30*measurement.Point, wml.ST_HeightRuleAtLeast)
-
- rowDataList := rowList[i].RowList
- if rowDataList != nil {
- for j := 0; j < len(rowDataList); j++ {
-
- var isMeged bool
- isMeged, ok := rowIsMeged[j]
- if !ok {
- rowIsMeged[j] = false
- isMeged = false
- }
- cell := row.AddCell()
- cellPara := cell.AddParagraph()
- run := cellPara.AddRun()
-
- cellData := rowDataList[j]
-
- if cellData.ColumnSpan > 0 {
-
- cell.Properties().SetColumnSpan(cellData.ColumnSpan)
-
- }
-
- if cellData.IsMerged {
-
- rowIsMeged[j] = true
-
- var mergeVal wml.ST_Merge
- if isMeged {
- mergeVal = wml.ST_MergeContinue
- } else {
- mergeVal = wml.ST_MergeRestart
- }
- cell.Properties().SetVerticalMerge(mergeVal)
- } else {
-
- rowIsMeged[j] = false
- }
-
- if cellData.Background != "" {
- cell.Properties().SetShading(wml.ST_ShdSolid, getColorConf(cellData.Background), color.Auto)
- }
-
- cell.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter)
-
- if cellData.WidthPercent > 0 {
- cell.Properties().SetWidthPercent(cellData.WidthPercent)
- }
-
- if cellData.TextAlign != "" {
- cellPara.Properties().SetAlignment(getTextAlignConf(cellData.TextAlign))
-
- }
-
-
- run.Properties().SetBold(cellData.IsBold)
-
- fontSize := 10.0
- if cellData.FontSize > 0 {
- fontSize = cellData.FontSize
- }
- run.Properties().SetSize(measurement.Distance(fontSize * measurement.Point))
-
- cellPara.Properties().Spacing().SetLineSpacing(measurement.Distance(1.4*fontSize*measurement.Point), wml.ST_LineSpacingRuleAuto)
-
- cellPara.Properties().Spacing().SetBefore(measurement.Distance(0.9 * fontSize * measurement.Point))
-
- cellPara.Properties().Spacing().SetAfter(measurement.Distance(0.5 * fontSize * measurement.Point))
-
- run.Properties().SetFontFamily("宋体")
-
- run.Properties().SetVerticalAlignment(sharedTypes.ST_VerticalAlignRunBaseline)
-
- if cellData.Value != "" {
- strSlice := strings.Split(cellData.Value, "<br/>")
- for s := 0; s < len(strSlice); s++ {
- if s > 0 {
- run.AddBreak()
- }
- run.AddText(strSlice[s])
- }
- } else {
- run.AddText("")
- }
- }
- }
- }
- }
- return
- }
- func getWordPath(templateId int) string {
- var path string
- switch templateId {
- case 1:
- path = "./static/word/template_1.docx"
- case 2:
- path = "./static/word/template_2.docx"
- }
- return path
- }
- type html2pdfData struct {
- CompanyName string `description:"甲方名称"`
- ContractCode string `description:"合同编号"`
- Address string `description:"甲方地址"`
- PostcodeDisplay string `description:"甲方邮编;是否展示"`
- Postcode string `description:"甲方邮编"`
- PhoneDisplay string `description:"甲方电话;是否展示"`
- Phone string `description:"甲方电话"`
- FaxDisplay string `description:"传真;是否展示"`
- Fax string `description:"传真"`
- RemarkDisplay string `description:"备注;是否展示"`
- Remark string `description:"备注"`
- PayRemark string `description:"支付备注"`
- StartDate string `description:"合同开始日期"`
- EndDate string `description:"合同结束日期"`
- NumYear string `description:"合同有效期"`
- Price string `description:"支付金额"`
- TableHtml string `description:"表格数据"`
- }
- func GetHtmlByContractDetail(contractDetail *contract.ContractDetail, htmlType string) (contractHtml string, err error) {
- contractTemplate, err := contract_template.GetContractTemplateByTemplateId(contractDetail.TemplateId)
- if err != nil {
- return
- }
- htmlTpl := contractTemplate.Html
- if htmlType == "pdf" {
- htmlTpl = contractTemplate.PdfHtml
- }
- myTpl := template.Must(template.New("contract").Parse(htmlTpl))
-
- address := getContractAddress(contractDetail)
- data := html2pdfData{
- CompanyName: contractDetail.CompanyName,
- ContractCode: contractDetail.ContractCode,
- Address: address,
- PostcodeDisplay: "block",
- Postcode: contractDetail.Postcode,
- PhoneDisplay: "block",
- Phone: contractDetail.Phone,
- FaxDisplay: "block",
- Fax: contractDetail.Fax,
- RemarkDisplay: "block",
- Remark: contractDetail.Remark,
- PayRemark: contractDetail.PayRemark,
- StartDate: contractDetail.StartDate.Format("2006年01月02日"),
- EndDate: contractDetail.EndDate.Format("2006年01月02日"),
- }
- if data.Postcode == "" {
- data.Postcode = "无"
- data.PostcodeDisplay = "none"
- }
- if data.Fax == "" {
- data.Fax = "无"
- data.FaxDisplay = "none"
- }
- if data.Phone == "" {
- data.Phone = "无"
- data.PhoneDisplay = "none"
- }
- if data.PayRemark == "" {
- data.PayRemark = "无"
- }
- if data.Remark == "" {
- data.Remark = "无"
- data.RemarkDisplay = "none"
- }
-
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- tmpPrintContent, tmpErr := utils.CalculationDate(contractDetail.StartDate, contractDetail.EndDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- data.NumYear = tmpPrintContent
- }
-
- {
- priceStr := ""
-
-
- newDecimal := decimal.NewFromFloat(contractDetail.OriginalPrice)
- originalPrice := newDecimal.String()
- priceStr += "小写:" + originalPrice + "元,"
-
- originalCnyPrice, cnyErr := utils.ConvertNumToCny(contractDetail.OriginalPrice)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- priceStr += "大写:" + originalCnyPrice
-
- if contractDetail.OriginalPrice != contractDetail.Price {
-
- newDecimal := decimal.NewFromFloat(contractDetail.Price)
- price := newDecimal.String()
- priceStr += ",经甲乙双方友好协商,优惠至:" + price + "元,"
-
- cnyPrice, cnyErr := utils.ConvertNumToCny(contractDetail.Price)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- priceStr += "大写:" + cnyPrice
- }
- data.Price = priceStr
- }
- buf := new(bytes.Buffer)
- tplErr := myTpl.Execute(buf, data)
- if tplErr != nil {
- err = tplErr
- return
- }
- contractHtml = buf.String()
-
- {
- tableStr := ""
- tableDataSlice := make([]TableData, 0)
- tableTitleSlice := make([]string, 0)
- title := ""
- if contractDetail.ProductId == 1 {
- title = "依照《【弘则研究】FICC客户客户服务列表2021》中 "
- } else {
- title = "依照《【弘则研究】私募客户客户服务列表2021》中 "
- }
- for i := 0; i < len(contractDetail.Service); i++ {
-
- var tableDataList TableData
- item := contractDetail.Service[i]
-
- tableTitleSlice = append(tableTitleSlice, item.Title)
-
- if item.HasDetail == "是" && len(item.DetailList) > 0 {
-
- tableRowList := make([]TableRow, 0)
-
- for j := 0; j < len(item.DetailList); j++ {
-
- isBold := false
- backgrandColor := ""
- fontSize := 13.0
-
- if j == 0 {
- isBold = true
- backgrandColor = "gray_2"
- fontSize = 13.0
- }
-
- tmpCellList, colErr := getColList(item.DetailList[j])
- if colErr != nil {
- err = colErr
- return
- }
-
- tableCelList := make([]TableCel, 0)
- lenCell := len(tmpCellList)
- for k := 0; k < len(tmpCellList); k++ {
-
- widthPercent := 30.0
- if k > 0 {
-
-
- newDecimal := decimal.NewFromFloat(70)
-
- newDecimal2 := decimal.NewFromInt(int64(lenCell) - 1)
-
- tmpWidthPercent, _ := newDecimal.Div(newDecimal2).Round(3).Float64()
-
-
-
-
- widthPercent = tmpWidthPercent
-
- }
- tableCel := TableCel{
- Value: tmpCellList[k],
- TextAlign: "center",
-
-
- Background: backgrandColor,
- IsBold: isBold,
- FontSize: fontSize,
- WidthPercent: widthPercent,
- }
- tableCelList = append(tableCelList, tableCel)
- }
-
- tableRow := TableRow{
- RowList: tableCelList,
- }
- tableRowList = append(tableRowList, tableRow)
- }
-
- tableDataList.List = tableRowList
- } else {
-
- contractServiceTemplate, tmpErr := contract_service_template.GetContractServiceTemplateById(item.ServiceTemplateId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
- jsonStr := contractServiceTemplate.TableValue
- tmpEerr := json.Unmarshal([]byte(jsonStr), &tableDataList)
- if tmpEerr != nil {
- err = tmpEerr
- return
- }
- }
- tableDataSlice = append(tableDataSlice, tableDataList)
- }
- titleStr := strings.Join(tableTitleSlice, "、")
- title += titleStr + "的服务内容,详细如下:"
- if htmlType == "pdf" {
- tableStr += `<p style="">` + title + `</p>`
- } else {
- tableStr = `<p style="font-size: 13pt; line-height: 40px">` + title + `</p>`
- }
- for _, tableDataList := range tableDataSlice {
-
- if htmlType == "pdf" {
- tableStr += getTableStrByPdf(tableDataList)
- } else {
- tableStr += getTableStr(tableDataList)
- }
- }
- data.TableHtml = tableStr
- }
-
- contractHtml = strings.Replace(contractHtml, `\{\{\{TableHtml\}\}\}`, data.TableHtml, -1)
- return
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func getTableStr(tableDataList TableData) (tableStr string) {
-
- tableStr += `<table style="width: 100%;border-collapse: collapse;font-size: 13pt;margin-bottom:30px;page-break-inside: avoid !important;"><tbody>`
- rowList := tableDataList.List
- for i := 0; i < len(rowList); i++ {
-
- tableStr += `<tr style="`
- tableStr += `page-break-before: always;page-break-after: always;page-break-inside: avoid !important;`
-
- tableStr += `">`
-
-
-
-
-
-
- rowDataList := rowList[i].RowList
- cellStr := ""
- if rowDataList != nil {
- for j := 0; j < len(rowDataList); j++ {
-
-
-
-
- tdStr := `<td `
-
- styleStr := `style="`
- styleStr += `border:1px solid #808181;padding: 15px 10px;line-height: 1.5;`
-
- cellOtherStr := ` valign="middle" `
-
- cellData := rowDataList[j]
-
- if cellData.ColumnSpan > 0 {
-
- cellOtherStr += ` colspan="` + strconv.Itoa(cellData.ColumnSpan) + `" `
- }
-
- if cellData.IsMerged {
- if cellData.IsFirstMerged {
- cellOtherStr += ` rowspan="` + strconv.Itoa(cellData.RowSpan) + `" `
- } else {
-
- continue
- }
- }
-
- if cellData.Background != "" {
- styleStr += `background-color: #F0F2F5;`
- }
-
- if cellData.WidthPercent > 0 {
- widthDecimal := decimal.NewFromFloat(cellData.WidthPercent)
- cellOtherStr += ` width="` + widthDecimal.String() + `%" `
- }
-
- if cellData.TextAlign != "" {
- cellOtherStr += ` align="` + cellData.TextAlign + `" `
- }
-
-
- if cellData.IsBold {
- styleStr += `font-weight:bold;`
- }
-
- fontSize := 10.0
- if cellData.FontSize > 0 {
- fontSize = cellData.FontSize
- }
- fontDecimal := decimal.NewFromFloat(fontSize)
- styleStr += `font-size: ` + fontDecimal.String() + `pt;`
- bodyStr := cellData.Value
- styleStr += `" `
- cellStr += tdStr + styleStr + cellOtherStr + `>` + bodyStr + `</td>`
- }
- }
- tableStr += cellStr + `</tr>`
- }
- tableStr += `</tbody></table>`
- return
- }
- func getTableStrByPdf(tableDataList TableData) (tableStr string) {
-
- tableStr += `<table style="width: 100%;border-collapse: collapse;margin-top:10pt;page-break-inside: avoid !important;"><tbody>`
- rowList := tableDataList.List
- for i := 0; i < len(rowList); i++ {
-
- tableStr += `<tr style="`
- tableStr += `page-break-before: always;page-break-after: always;page-break-inside: avoid !important;`
-
- tableStr += `">`
-
-
-
-
-
-
- rowDataList := rowList[i].RowList
- cellStr := ""
- if rowDataList != nil {
- for j := 0; j < len(rowDataList); j++ {
-
-
-
-
- tdStr := `<td `
-
- styleStr := `style="`
- styleStr += `border:1px solid #808181;padding:4pt 10pt;`
-
- cellOtherStr := ` valign="middle" `
-
- cellData := rowDataList[j]
-
- if cellData.ColumnSpan > 0 {
-
- cellOtherStr += ` colspan="` + strconv.Itoa(cellData.ColumnSpan) + `" `
- }
-
- if cellData.IsMerged {
- if cellData.IsFirstMerged {
- cellOtherStr += ` rowspan="` + strconv.Itoa(cellData.RowSpan) + `" `
- } else {
-
- continue
- }
- }
-
- if cellData.Background != "" {
- styleStr += `background-color: #F0F2F5;`
- }
-
- if cellData.WidthPercent > 0 {
- widthDecimal := decimal.NewFromFloat(cellData.WidthPercent)
- cellOtherStr += ` width="` + widthDecimal.String() + `%" `
- }
-
- if cellData.TextAlign != "" {
- cellOtherStr += ` align="` + cellData.TextAlign + `" `
- }
-
-
- if cellData.IsBold {
- styleStr += `font-weight:bold;`
- }
-
- fontSize := 10.0
- if cellData.FontSize > 0 {
- fontSize = cellData.FontSize
- }
- fontDecimal := decimal.NewFromFloat(fontSize)
- styleStr += `font-size: ` + fontDecimal.String() + `pt;`
- bodyStr := cellData.Value
- styleStr += `" `
- cellStr += tdStr + styleStr + cellOtherStr + `>` + bodyStr + `</td>`
- }
- }
- tableStr += cellStr + `</tr>`
- }
- tableStr += `</tbody></table>`
- return
- }
- func Html2Pdf(htmlStr, pdfPath string) (err error) {
- pdfg, err := wkhtml.NewPDFGenerator()
- if err != nil {
- fmt.Println("err:", err)
- return
- }
-
- page := wkhtml.NewPageReader(strings.NewReader(htmlStr))
-
-
-
-
-
-
-
-
- page.FooterFontSize.Set(8)
- page.FooterRight.Set("[page]")
- page.FooterSpacing.Set(4)
-
-
-
-
-
-
-
- page.EnableTocBackLinks.Set(true)
-
- pdfg.AddPage(page)
-
-
-
-
-
- err = pdfg.Create()
- if err != nil {
- return
- }
- err = pdfg.WriteFile(pdfPath)
- return
- }
- func getColList(item *contract_service_detail.ContractServiceDetail) (cellList []string, err error) {
- cellList = make([]string, 0)
- var serviceDetailReq contractReq.AddContractServiceDetailReq
- tmpItem := *item
- t := reflect.TypeOf(tmpItem)
- v := reflect.ValueOf(tmpItem)
- for k := 0; k < t.NumField(); k++ {
-
- tmpName := t.Field(k).Name
- if strings.Contains(tmpName, "Col") {
-
- tmpValue := v.Field(k).String()
-
- if tmpValue != "" {
- err = json.Unmarshal([]byte(tmpValue), &serviceDetailReq)
- if err != nil {
- return
- } else {
- cellList = append(cellList, serviceDetailReq.Value)
- }
- }
- }
- }
- return
- }
- type WordElement struct {
- ElementType string `json:"element_type" description:"元素类型"`
- ElementName string `json:"element_name" description:"元素名称"`
- RelationName string `json:"relation_name" description:"关联元素名称"`
- Content string `json:"content" description:"元素内容"`
- Background string `json:"background" description:"背景色"`
- IsBold bool `json:"is_bold" description:"是否加粗显示"`
- TextAlign string `json:"text_align" description:"对齐方式"`
- FontSize float64 `json:"font_size" description:"字体大小"`
- ElementList []WordElement `json:"list" description:"子元素"`
- }
- func GenerateWordV2(contractDetail *contract.ContractDetail, wordPath string) (err error) {
- contractTemplate, err := contract_template.GetContractTemplateByTemplateId(contractDetail.TemplateId)
- if err != nil {
- return
- }
- jsonStr := contractTemplate.WordConfig
- var contractData []WordElement
- err = json.Unmarshal([]byte(jsonStr), &contractData)
- if err != nil {
- fmt.Println("json字符串解析失败,ERR:", err)
- return
- }
- doc := document.New()
-
- cp := doc.CoreProperties
-
- cp.SetTitle("弘则弥道(上海)投资咨询有限公司 & 研究服务合同")
- cp.SetAuthor("弘则弥道(上海)投资咨询有限公司")
- cp.SetCategory("合同")
-
- cp.SetLastModifiedBy("弘则弥道(上海)投资咨询有限公司")
- cp.SetCreated(time.Now())
- cp.SetModified(time.Now())
- cp.SetDescription("弘则弥道(上海)投资咨询有限公司 研究服务合同")
- cp.SetLanguage("中文")
- for _, data := range contractData {
- fontSize := data.FontSize
- if fontSize <= 0 {
- fontSize = 15
- }
- printContent := ``
- if data.ElementName == "services" {
- tableTitleSlice := make([]string, 0)
- title := ""
- if contractDetail.ProductId == 1 {
- title = "依照《【弘则研究】FICC客户客户服务列表2021》中"
- } else {
- title = "依照《【弘则研究】私募客户客户服务列表2021》中"
- }
- TableDataListSlice := make([]TableData, 0)
-
- for i := 0; i < len(contractDetail.Service); i++ {
-
- var tableDataList TableData
- item := contractDetail.Service[i]
-
- tableTitleSlice = append(tableTitleSlice, item.Title)
-
- if item.HasDetail == "是" && len(item.DetailList) > 0 {
-
- tableRowList := make([]TableRow, 0)
-
- for j := 0; j < len(item.DetailList); j++ {
-
- isBold := false
- backgrandColor := ""
- fontSize := 10.0
-
- if j == 0 {
- isBold = true
- backgrandColor = "gray_2"
- fontSize = 12.0
- }
-
- tmpCellList, colErr := getColList(item.DetailList[j])
- if colErr != nil {
- err = colErr
- return
- }
-
- tableCelList := make([]TableCel, 0)
- lenCell := len(tmpCellList)
- for k := 0; k < len(tmpCellList); k++ {
-
-
- newDecimal := decimal.NewFromFloat(100)
-
- newDecimal2 := decimal.NewFromInt(int64(lenCell))
-
- widthPercent, _ := newDecimal.Div(newDecimal2).Round(3).Float64()
-
-
-
-
-
- tableCel := TableCel{
- Value: tmpCellList[k],
- TextAlign: "center",
-
-
- Background: backgrandColor,
- IsBold: isBold,
- FontSize: fontSize,
- WidthPercent: widthPercent,
- }
- tableCelList = append(tableCelList, tableCel)
- }
-
- tableRow := TableRow{
- RowList: tableCelList,
- }
- tableRowList = append(tableRowList, tableRow)
- }
-
- tableDataList.List = tableRowList
- } else {
-
- contractServiceTemplate, tmpErr := contract_service_template.GetContractServiceTemplateById(item.ServiceTemplateId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
- jsonStr := contractServiceTemplate.TableValue
- err = json.Unmarshal([]byte(jsonStr), &tableDataList)
- if err != nil {
- return
- }
- }
-
- TableDataListSlice = append(TableDataListSlice, tableDataList)
- }
-
- titleStr := strings.Join(tableTitleSlice, "、")
- title += titleStr + "的服务内容,详细如下:"
-
- headerPar := doc.AddParagraph()
- headerParPro := headerPar.Properties()
-
- textAlign := getTextAlignConf(data.TextAlign)
- headerParPro.SetAlignment(textAlign)
-
-
-
- headerRun := headerPar.AddRun()
- headerRunPro := headerRun.Properties()
- headerRunPro.SetBold(data.IsBold)
- headerRunPro.SetSize(measurement.Distance(fontSize * measurement.Point))
- headerRunPro.SetFontFamily("宋体")
-
- headerRun.AddText(title)
-
- for _, tableDataList := range TableDataListSlice {
- tmpErr := addTableV2(tableDataList, doc)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- continue
- } else {
- isPrint, tmpPrintContent, tmpErr := getPrintData(data, contractDetail)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if isPrint == false {
- continue
- }
- printContent = tmpPrintContent
- }
-
- printContentRune := []rune(printContent)
- strLen := len(printContentRune)
- addTabNum := 0
- printContentList := make([]map[int]string, 0)
- firstLen := 17
- secondLen := 14
- if data.ElementType == "column" {
- if strLen > firstLen {
- maxLine := ((strLen - firstLen) / secondLen) + 1
- for i := 0; i < maxLine; i++ {
- printContentMap := make(map[int]string)
- startIndex := secondLen*i + firstLen
- endIndex := secondLen*(i+1) + firstLen
- if endIndex > strLen {
- endIndex = strLen
- }
- tmpPrintContent := string(printContentRune[startIndex:endIndex])
- printContentMap[0] = tmpPrintContent
- printContentList = append(printContentList, printContentMap)
- }
- printContent = string(printContentRune[:firstLen])
- strLen = firstLen
-
-
-
- } else {
- addTabNum = firstLen - strLen
- }
- addTabNum += 3
- }
-
- headerPar := doc.AddParagraph()
- headerParPro := headerPar.Properties()
- headerParPro.Spacing().SetLineSpacing(measurement.Distance(1.5*fontSize*measurement.Point), wml.ST_LineSpacingRuleAuto)
-
- textAlign := getTextAlignConf(data.TextAlign)
- headerParPro.SetAlignment(textAlign)
-
-
-
- headerRun := headerPar.AddRun()
- headerRunPro := headerRun.Properties()
- headerRunPro.SetBold(data.IsBold)
- headerRunPro.SetSize(measurement.Distance(fontSize * measurement.Point))
- headerRunPro.SetFontFamily("宋体")
-
- headerRun.AddText(printContent)
- for _, text := range data.ElementList {
- if text.ElementName == "services" {
- tableTitleSlice := make([]string, 0)
- title := ""
- if contractDetail.ProductId == 1 {
- title = "依照《【弘则研究】FICC客户客户服务列表2021》中 "
- } else {
- title = "依照《【弘则研究】私募客户客户服务列表2021》中 "
- }
- TableDataListSlice := make([]TableData, 0)
- for i := len(contractDetail.Service) - 1; i >= 0; i-- {
-
- var tableDataList TableData
- item := contractDetail.Service[i]
-
- tableTitleSlice = append(tableTitleSlice, item.Title)
-
- if item.HasDetail == "是" && len(item.DetailList) > 0 {
-
- tableRowList := make([]TableRow, 0)
-
- for j := 0; j < len(item.DetailList); j++ {
-
- isBold := false
- backgrandColor := ""
- fontSize := 10.0
-
- if j == 0 {
- isBold = true
- backgrandColor = "gray_2"
- fontSize = 12.0
- }
-
- tmpCellList, colErr := getColList(item.DetailList[j])
- if colErr != nil {
- err = colErr
- return
- }
-
- tableCelList := make([]TableCel, 0)
- lenCell := len(tmpCellList)
- for k := 0; k < len(tmpCellList); k++ {
-
-
- newDecimal := decimal.NewFromFloat(100)
-
- newDecimal2 := decimal.NewFromInt(int64(lenCell))
-
- widthPercent, _ := newDecimal.Div(newDecimal2).Round(3).Float64()
-
-
-
-
-
- tableCel := TableCel{
- Value: tmpCellList[k],
- TextAlign: "center",
-
-
- Background: backgrandColor,
- IsBold: isBold,
- FontSize: fontSize,
- WidthPercent: widthPercent,
- }
- tableCelList = append(tableCelList, tableCel)
- }
-
- tableRow := TableRow{
- RowList: tableCelList,
- }
- tableRowList = append(tableRowList, tableRow)
- }
-
- tableDataList.List = tableRowList
- } else {
-
- contractServiceTemplate, tmpErr := contract_service_template.GetContractServiceTemplateById(item.ServiceTemplateId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
- jsonStr := contractServiceTemplate.TableValue
- err = json.Unmarshal([]byte(jsonStr), &tableDataList)
- if err != nil {
- return
- }
- }
-
- TableDataListSlice = append(TableDataListSlice, tableDataList)
- }
-
- titleStr := strings.Join(tableTitleSlice, "、")
- title += titleStr + "的服务内容,详细如下:"
- headerRun := headerPar.AddRun()
- headerRun.AddBreak()
- headerRunPro := headerRun.Properties()
- headerRunPro.SetBold(text.IsBold)
- headerRunPro.SetSize(measurement.Distance(fontSize * measurement.Point))
- headerRunPro.SetFontFamily("宋体")
-
- headerRun.AddText(title)
-
- for _, tableDataList := range TableDataListSlice {
- tmpErr := addTableV2(tableDataList, doc)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- } else {
- isPrint, printContent, tmpErr := getPrintData(text, contractDetail)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if isPrint == false {
- continue
- }
- if data.ElementType == "column" {
- for j := 0; j < addTabNum; j++ {
-
- tabStr := " "
- printContent = tabStr + printContent
- }
- }
- fontSize := text.FontSize
- if fontSize <= 0 {
- fontSize = 15
- }
- headerRun2 := headerPar.AddRun()
- headerRunPro2 := headerRun2.Properties()
- headerRunPro2.SetBold(text.IsBold)
- headerRunPro2.SetSize(measurement.Distance(fontSize * measurement.Point))
- headerRunPro2.SetFontFamily("宋体")
-
- headerRun2.AddText(printContent)
- }
- }
- for _, printMap := range printContentList {
-
- headerPar := doc.AddParagraph()
- headerParPro := headerPar.Properties()
- headerParPro.Spacing().SetLineSpacing(measurement.Distance(1.5*fontSize*measurement.Point), wml.ST_LineSpacingRuleAuto)
-
- textAlign := getTextAlignConf(data.TextAlign)
- headerParPro.SetAlignment(textAlign)
-
- headerParPro.SetStartIndent(measurement.Distance(3 * fontSize * measurement.Point))
- headerRun := headerPar.AddRun()
- headerRunPro := headerRun.Properties()
- headerRunPro.SetBold(data.IsBold)
- headerRunPro.SetSize(measurement.Distance(fontSize * measurement.Point))
- headerRunPro.SetFontFamily("宋体")
-
- headerRun.AddText(printMap[0])
- }
- }
-
-
-
-
-
-
-
-
-
-
- err = doc.SaveToFile(wordPath)
- return
- }
- func getPrintData(data WordElement, contractDetail *contract.ContractDetail) (isPrint bool, printContent string, err error) {
- printContent = data.Content
- if data.RelationName != "" {
- switch data.RelationName {
- case "address":
- if contractDetail.Address == "" && contractDetail.Province == "" && contractDetail.City == "" {
- return
- }
- case "postcode":
- if contractDetail.Postcode == "" {
- return
- }
- case "phone":
- if contractDetail.Phone == "" {
- return
- }
- case "fax":
- if contractDetail.Fax == "" {
- return
- }
- case "remark":
- if contractDetail.Remark == "" {
- return
- }
- case "price":
-
- if contractDetail.OriginalPrice == contractDetail.Price {
- return
- }
- case "price_cn":
-
- if contractDetail.OriginalPrice == contractDetail.Price {
- return
- }
- case "pay_remark":
- if contractDetail.PayRemark == "" {
- return
- }
- case "company_name":
- if contractDetail.CompanyName == "" {
- return
- }
- }
- }
- switch data.ElementName {
- case "address":
- if contractDetail.Address == "" && contractDetail.Province == "" && contractDetail.City == "" {
- return
- }
- printContent = getContractAddress(contractDetail)
- case "postcode":
- if contractDetail.Postcode == "" {
- return
- }
- printContent = contractDetail.Postcode
- case "phone":
- if contractDetail.Phone == "" {
- return
- }
- printContent = contractDetail.Phone
- case "fax":
- if contractDetail.Fax == "" {
- return
- }
- printContent = contractDetail.Fax
- case "remark":
- if contractDetail.Remark == "" {
- return
- }
- printContent = contractDetail.Remark
- case "start_date":
- printContent = contractDetail.StartDate.Format("2006年01月02日")
- case "end_date":
- printContent = contractDetail.EndDate.Format("2006年01月02日")
- case "num_year":
-
-
-
-
-
-
-
-
-
-
-
-
-
- tmpPrintContent, tmpErr := utils.CalculationDate(contractDetail.StartDate, contractDetail.EndDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- printContent = tmpPrintContent
- case "original_price":
-
- newDecimal := decimal.NewFromFloat(contractDetail.OriginalPrice)
- printContent = newDecimal.String()
- case "original_price_cn":
-
- originalCnyPrice, cnyErr := utils.ConvertNumToCny(contractDetail.OriginalPrice)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- printContent = originalCnyPrice
- case "price":
-
- if contractDetail.OriginalPrice == contractDetail.Price {
- return
- }
- newDecimal := decimal.NewFromFloat(contractDetail.Price)
- printContent = newDecimal.String()
- case "price_cn":
-
- if contractDetail.OriginalPrice == contractDetail.Price {
- return
- }
- cnyPrice, cnyErr := utils.ConvertNumToCny(contractDetail.Price)
- if cnyErr != nil {
- err = cnyErr
- return
- }
- printContent = cnyPrice
- case "pay_remark":
- if contractDetail.PayRemark == "" {
- return
- }
- printContent = contractDetail.PayRemark
- case "company_name":
- if contractDetail.CompanyName == "" {
- return
- }
- printContent = contractDetail.CompanyName
- case "company_name_sign":
- if contractDetail.CompanyName == "" {
- return
- }
- printContent = "甲方:" + contractDetail.CompanyName
- }
- isPrint = true
- return
- }
- func addTableV2(tableDataList TableData, doc *document.Document) (err error) {
-
-
- nowParagraph := doc.AddParagraph()
-
- table := doc.InsertTableAfter(nowParagraph)
-
- tableWidth := 6.5
- table.Properties().SetWidth(measurement.Distance(tableWidth * measurement.Inch))
-
-
-
- borders := table.Properties().Borders()
-
- borders.SetAll(wml.ST_BorderSingle, color.Auto, measurement.Zero)
-
- rowList := tableDataList.List
-
- for i := 0; i < len(rowList); i++ {
-
- row := table.AddRow()
-
- row.Properties().SetHeight(30*measurement.Point, wml.ST_HeightRuleAtLeast)
-
- rowDataList := rowList[i].RowList
- if rowDataList != nil {
- for j := 0; j < len(rowDataList); j++ {
- cell := row.AddCell()
- cellPara := cell.AddParagraph()
- run := cellPara.AddRun()
-
- cellData := rowDataList[j]
-
- if cellData.ColumnSpan > 0 {
-
- cell.Properties().SetColumnSpan(cellData.ColumnSpan)
-
- }
-
- if cellData.IsMerged {
-
- var mergeVal wml.ST_Merge
- if cellData.IsFirstMerged {
- mergeVal = wml.ST_MergeRestart
- } else {
- mergeVal = wml.ST_MergeContinue
- }
- cell.Properties().SetVerticalMerge(mergeVal)
- }
-
- if cellData.Background != "" {
- cell.Properties().SetShading(wml.ST_ShdSolid, getColorConf(cellData.Background), color.Auto)
- }
-
- cell.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter)
-
- if cellData.WidthPercent > 0 {
-
-
- cellWidth := tableWidth * cellData.WidthPercent * measurement.Inch / 100
- cell.Properties().SetWidth(measurement.Distance(cellWidth))
- }
-
- if cellData.TextAlign != "" {
- cellPara.Properties().SetAlignment(getTextAlignConf(cellData.TextAlign))
-
- }
-
-
- run.Properties().SetBold(cellData.IsBold)
-
- fontSize := 10.0
- if cellData.FontSize > 0 {
- fontSize = cellData.FontSize
- }
- run.Properties().SetSize(measurement.Distance(fontSize * measurement.Point))
-
- cellPara.Properties().Spacing().SetLineSpacing(measurement.Distance(1.4*fontSize*measurement.Point), wml.ST_LineSpacingRuleAuto)
-
- cellPara.Properties().Spacing().SetBefore(measurement.Distance(0.9 * fontSize * measurement.Point))
-
- cellPara.Properties().Spacing().SetAfter(measurement.Distance(0.5 * fontSize * measurement.Point))
-
- run.Properties().SetFontFamily("宋体")
-
- run.Properties().SetVerticalAlignment(sharedTypes.ST_VerticalAlignRunBaseline)
-
- if cellData.Value != "" {
- strSlice := strings.Split(cellData.Value, "<br/>")
- for s := 0; s < len(strSlice); s++ {
- if s > 0 {
- run.AddBreak()
- }
- run.AddText(strSlice[s])
- }
- } else {
- run.AddText("")
- }
- }
- }
- }
- return
- }
- func getContractAddress(contractDetail *contract.ContractDetail) (address string) {
- ignoreStrs := []string{"北京市", "上海市", "天津市", "重庆市"}
- if strings.Contains(strings.Join(ignoreStrs, ","), contractDetail.Province) {
- address = contractDetail.City + contractDetail.Address
- } else {
- address = contractDetail.Province + contractDetail.City + contractDetail.Address
- }
- return
- }
- func FuncDocs2Pdf(command string, fileSrcPath string, fileOutDir string, converterType string) (fileOutPath string, error error) {
-
- srcFile, erByOpenSrcFile := os.Open(fileSrcPath)
- if erByOpenSrcFile != nil && os.IsNotExist(erByOpenSrcFile) {
- return "", erByOpenSrcFile
- }
-
- outFileDir, erByOpenFileOutDir := os.Open(fileOutDir)
- if erByOpenFileOutDir != nil && os.IsNotExist(erByOpenFileOutDir) {
- erByCreateFileOutDir := os.MkdirAll(fileOutDir, os.ModePerm)
- if erByCreateFileOutDir != nil {
- fmt.Println("File ouput dir create error.....", erByCreateFileOutDir.Error())
- return "", erByCreateFileOutDir
- }
- }
-
- defer func() {
- _ = srcFile.Close()
- _ = outFileDir.Close()
- }()
-
- cmd := exec.Command(command, "--invisible", "--convert-to", converterType,
- fileSrcPath, "--outdir", fileOutDir)
- byteByStat, errByCmdStart := cmd.Output()
-
- if errByCmdStart != nil {
- return "", errByCmdStart
- }
-
- fileOutPath = fileOutDir + "/" + strings.Split(path.Base(fileSrcPath), ".")[0]
- if converterType == "html" {
- fileOutPath += ".html"
- } else {
- fileOutPath += ".pdf"
- }
- fmt.Println("文件转换成功...", string(byteByStat))
- return fileOutPath, nil
- }
|